Python Scripting For Storage Admin Part 6 Login Through SSH Execute Command
Python Scripting For Storage Admin Part 6 Login Through SSH Execute Command |
Open a command prompt and then execute below command. This will download the module and install it in windows OS. Please note your internet connection should be active.
pip install paramiko
First line will import the module paramiko. You can import any module like this.
import paramiko
Second line will create an object called "p" which will create an SSH session.
p = paramiko.SSHClient()
Below line will add key to SSH session.
p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
In below line we have mention the IP address of the system then port number for ssh protocol. Third and fourth parameter we have mentioned username and password.
p.connect("192.168.15.173",port =22, username = "atish", password="password")
Below is the code where we need to specify the command that we need to execute. If the command is correct and successfully executed then the output will be stored in stdout if any error comes while executing command then it will stored under stderr.
stdin, stdout, stderr = p.exec_command("uname -a")
Below line will convert output which is stored in variable stdout into a list.
opt = stderr.readlines()
Below line will join the above list and convert it into areadable format.
opt ="".join(opt)
Finally below line will print the output.
print(opt)
Subscribe to You Channel and stay tuned for more tutorial video in this series.