Putty Automation with Python Using pyAutoGUI module

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello everyone welcome to my channel so in the in today's video i will demonstrate how to automate participation the ssh session uh using python i will use the pi auto gui module here so let's look at the docs so this is the documentation of pi auto gui i will share the link in the description below so the pi auto gui module allows it works on windows mac os and linux it runs on both python 2 and 3 and it allows movement of mouse and key strokes and clicking typing everything automatically so you can you can automate pretty much everything in windows in windows or linux or mac in any operating system using prior to gy but it has some limitations that it does not support multiple screens till now so let's start so first of all i will create a folder inside my youtube directory and you can see that i have already created a folder and i have named it party auto so inside this folder i will create a script python script that is and i will name it as patio only so let's do this and inside this script i will call a few libraries so first thing i will call party auto gui if you don't have material gui you have to install that for this you can go to terminal and type pip install type this command and check what it does it is telling requirement already satisfied because i have already downloaded this so now you can start so i have to import this library i will import it as pa i will i like to use short forms because it allows me to type less keywords so i also need the sub process module and i also need the time module so first first i will need to declare the path here you have to enter the path where your executable file is the party executable file path you have to declare here so i know my path in inside c directory call program files inside program files there is a folder and inside party there is the executable file you have to type the absolute path here let me check it is correct or not okay i think it is correct so i will use the sub process module and it will open the program i have to just pass the path of the program and let's execute the script to execute i will simply type python you can see that party is opening so the next thing that we need is we need to grab this hostname field so that you can enter the value the ip address actually inside the hostname so to do this i will open the snipping tool and i will cut out this section and i will save it as let's go to desktop this is the directory and i will create a folder images or access whatever you want to call it and i will name it party text box so here is my image let's check it okay here is my image so next thing what i will do is i will do a time dot sleep because the party application needs some time to open up so my computer is slow that's why i'm giving 10 seconds if your computer is fast you can speed up this time lower this time actually so next thing that i need is i have to get the x y coordinates of the object that is the text box so there is a party function locate center on screen and i have to pass the path of the image images slash party text box okay so actually what we do it will do is it will locate the object and it will print uh it will uh store not print sorry it will store the x and y coordinates in these two variables so if we now print the x and y variables let's run the program okay so you can see the x coordinate is one zero one four and the y coordinate is four one two so you can print it like it has got those two variables uh so it has got these two values so the next thing that i need to do is i need to click on this location so to click on this location i have to pass the xy coordinate and it will click so the next thing that we need is to enter the ip address into this location so i will use the typewrite method and i will enter the ip address actually this is this ipad this from where i'm getting is i have opened a vm inside vmware it is running the rail 7 and you can see i don't know whether you can see or not the ip address of e3 ns33 is 192.168.225.244. this ip address i have to enter here so let's quickly do that i have entered the ip address and now i'll press enter and let's see what it does now okay so okay i do i think i have done it correctly let's see what it does so you should type the ipad is in this field and press enter okay it has typed and now you can see that the fingerprint screen is occurring so i will click it as no but if you want you can click it as yes but it is best practice to click it as no because the script will run you you must have been automating the script to run on different ip addresses so if you want to maintain a standard procedure you can click it as no every time you can check whether it is the box is appearing or not and tell me in the comment section that if you want to see more of this this kind of tutorial so i will do that but for now i am only doing this thing so the basically what i will do is i will click it as now so let's take a snapshot of this no button and i will save it in the no i will save it as no iphone btn and i will close these two and also this one so the next thing that i will do is i will give time dot sleep again sorry not 10 seconds now i am giving five seconds and again i have to get the x y coordinate of the image that we have copied so pa dot locate center on screen images slash no button dot png so this thing here dot click xy so you should click on that button so let's run the program again and see what it is successfully doing that or not so it will type the ip address here ap address has been typed so you should click the no button now yes no button is clicked now you can see that it is telling to login so we will repeat the same process take the login snapshot and paste it in this folder you can call it login so again we have to give some time dot strip because it takes some time to open that console again i'm giving 5 seconds so in xy okay so it is getting the coordinates of the login and now if you click on that the only thing where why i'm clicking on that is to bring the cursor there if the cursor is somewhere else and you start typing then it will not work so it's better to click every time we do an action so after clicking on that i will type i have to type the username so for this you can use typewrite method and you can pass the username in this case it is root and i have to press enter i have to press enter let's see what it does so you should type the ip address and you should click on no yes now it should type the route okay so now you can see it is asking for the password so the same process you have to repeat you have to copy the image of the password field you have to save it as not pwd password okay so let's wait i can again do some sleep so that it waits so again i will use time dot sleep and again i will keep the xy coordinates locate center on screen and i will pass the path of the image then i will type the password same thing typewrite my password is like one two three one two three so then i will press enter it should login by now let's see what it does again on the script and see what it does we should type the ip now again you should click on the no button i should type the root username you should type the password okay now you can see that it has logged in as root user now what i will do i will copy the i will cut out the prompt sign this is the prompt sign let's do one more time okay so i will save it as old prompt because i will change the prompt so now again i have to do this same thing i have to wait for some time and then i have to wait for lesser 10 seconds and let me actually close all the steps okay i have closed all the tabs and let's do it for 10 seconds then it will i then have to do the same thing i'm just copying these three lines and pasting it here you can see in the image i have to keep the old prompt and i will type write this thing export ps1 the ps1 will variable actually holds the default prompt correct me if i'm doing something wrong i'm not an expert so it will hold the prompt here so it will temporarily change the prompt if you close the session and open again the prompt will be back back back to the normal prompt that you are seeing before so let us see it will change or not yeah okay let's run it once more and check whether it's working or not so it must type the password okay it is logged in we just change the prompt now yes the prompt has been changed um so next thing is we have to type the commands for typing the commands i will create a list that will hold all the commands so for the sake of this example i am typing only few simple commands you can expand it as you want in the description i will share the blog post which will give you step by step instructions and you can comment or contact me whether whenever you want for this kind of projects automation projects so you can read the blog that i will share you in the description so for commands actually i have to use a different strategy i have to create a function named as type commands so in this type command function it will take a parameter command so basically what i'm trying to do is i'm i want to loop through all the items in the one by one in this list and i will i will pass this command here one by one it will do and so to avoid repetitive coding i am doing this and also also one thing i will use strike as block here so i will use try and accept block because i am not using time actually time.sleep here so i have to use this because the type error will occur if it doesn't find the command okay so let's do one thing let's comment it out and run once more to get the prompt actually i have not taken the snapshot of the new prompt if you type the password you should change the prompt as the prompt is changed now i have to copy i have to take a snapshot of the prompt okay i've taken okay let's check whether it is yes i've taken so i will close this so now you can uncomment this and again repeat the previous process but this time we will look at the pre new prompts that we have set because it takes only images we can only identify objects with image so it will grab this coordinate and it will click on that so one thing i have forgotten here is that once it is grabbing the location you should click but still it is running okay so we should click there and again you should click here also one thing you have noticed that i am using x and y repeatedly because we don't need the previous values of x and y's so again i will do typewrite then simply i will pass the parameter here and i will press enter so after each typing of which after each command it will press enter to execute the command and it will wait for the completion of the execution so i will accept the except that i type error because after completion it will take some time to complete the execution of the commands so the program will throw the type error and i will handle the type error by calling the function again again i'm if i told you or not i don't remember but this is not the best way to automate actually there are few different ways like the like using the p expect module and like using parameter module for automating the ssh session so yeah i will talk about those in later videos but for now i want to keep things simple so let's do this for cmd in cmds so it will look through each and every commands and it will count let's see if it is working or not so you should type the hostname i should click on the no button you should type the password i should change the prompts now yes the prompt is changed the commands are being typed you can see it is not typing commands because some previous processing error is occurring so you have to maximize the script then it will work as as it was before so all the commands are typed everything has been done so this is it for this video thank you for watching guys make sure to like share and subscribe and leave your comments about how to improve this thing and the entire tutorial step by step tutorial i have attached my blog link in the description please check out that and thanks thanks for watching bye you
Info
Channel: I am Subham
Views: 4,313
Rating: undefined out of 5
Keywords:
Id: BZeSSOVi0Is
Channel Id: undefined
Length: 28min 16sec (1696 seconds)
Published: Fri Sep 25 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.