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.


                   

Python Scripting For Storage Admin Part 5 - If Else For Loop

Python Scripting For Storage Admin Part 5 - If Else For Loop

In this blog I have explained how we can use if else and for loop in python scripting. These two concept in python scripting is very important because once you know how to implement these in scripting you can convert any task into scripting and automate it.

Python Scripting For Storage Admin Part 5 - If Else For Loop
Python Scripting For Storage Admin Part 5 - If Else For Loop

Below three line we have created two list and and one variable. We will use these as example while using for loop and if else.

ls = ["a","b","c","d"]
ls1 = [1,2,3,4]
line = "i am a list"

In below line we have created a list from the variable "line" using split function. A space character is used within double quotes to create the list.

conv_list = line.split(" ")
print(conv_list)

Below is the output of above print function.

['i', 'am', 'a', 'list']

Now in below line we will use for loop to print all element of the list "conv_list". For loop will iterate over each element of the list and assign the value of the element to a temporary variable "i" and in next line it will print it.

for i in conv_list:
    print(i)

Output of above for loop is as below
i
am
a
list

Below is an example of if and else statement. If the specified condition under "if" statement is true then the block under "if" will get executed other wise the block under "else" will get executed.

if "c" in ls:
    print(ls)
else:
    print("Not found")

Output of above if else code is as below.

['a', 'b', 'c', 'd']

Watch below video for live demo of this tutorial. Dont forget to share and subscribe to the YouTube Channel for more videos on this series.

                    

Python Scripting How To Open Read & Write In File

Python Scripting For Storage Admin - How To Read & Write In File. 

Python Scripting How To Open Read & Write In File
Python Scripting How To Open Read & Write In File

In this videos we will see how we can open file using python script then we will write some content in the file and then save it using python script. This is also called as file handling in python script.

Below is the code which is explained in above video.

Below line will open a file for writing purpose. You can use "r" for read and "a" for append.

s=open("file.txt","w")

This line will write some content in the file. \n is the newline character which writes data in a new line.

s.write("this is a file2\n")

Below line will save the data and close the file. If you miss below line then data will not get saved.

s.close()

This line will read the entire content of file and store it in a variable read_file.

read_file=open("file.txt","r").read()

Below line will print the content which is stored in variable read_file.

print(read_file)      

For more details and live demo please refer to the below video. Subscribe to youtube channel as well and stay tuned for more videos in this video series.

                

Python Scripting For Storage Admin - Why Python Scripting

Python Scripting For Storage Admin 

Why system admin Should Learn Python Scripting. In this video i have explained few reason to start learning scripting. You can learn any scripting language but choosing python will will give you many advantage as below.

Python Scripting for Storage Admin
Python Scripting for Storage Admin


1.What is scripting ?

It is a bunch of code written in any programming language which perform certain task in specific sequence.

2.Why Storage Admin Learn Scripting

To make your life easy. If you have lot of server or storage devices which support ssh and regularly login to those device to check health status and it killing your time then learn scripting and automate it. Also if you want to start career in DevOps then you must a scripting language.

3.Why Python

Its a very easy language and platform independent. Its a very powerful language which is widely being used in many popular website.

4.Popular Websites Which Uses Python

nasa.gov
Google Search Engine
InstaGram
Quora

5.What Would be In this Series?

First Couple of video we will discuss about basic in python then we will start write a code which will login to the system remotely and execute some command to get some output.

             

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 ...