Python Scripting For Storage Admin Part 6 Login Through SSH Execute Command

Python Scripting For Storage Admin Part 6 Login Through SSH Execute Command

Python Scripting For Storage Admin Part 6 Login Through SSH Execute Command
Python Scripting For Storage Admin Part 6 Login Through SSH Execute Command


In this video I have explained how we can login to a device using ssh protocol, execute a command and then print the output on the screen by using python script. We will use a module called paramiko to achieve this. Paramiko module needs to be installed before.

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.


                   

No comments:

Post a Comment

Zoning In Cisco MDS SAN Switch In Command Line

Zoning In Cisco MDS SAN Switch In Command Line Zoning is a process of grouping initiator and target ports WWPN which is performed in SAN ...