Error Handling In Python Try And Except

Error Handling In Python Try And Except 


In this script we will see how we can implement error handling in python scripting by using try and except method. Sometime is the there is an error in script or in the remote device where you are trying to connect then the script will break and stop working. Error handling using try and except method deals with these kind of situation.

Error Handling In Python Try And Except
Error Handling In Python Try And Except

Below is the script which we have used error handling. Watch the video for full explanation and subscribe to the channel for more such video.

import paramiko
p = paramiko.SSHClient()
cred = open("cred.csv","r")
for i in cred.readlines():
    try:
        line=i.strip()
        ls =line.split(",")
        p.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        p.connect("%s"%ls[0],port =22, username = "%s"%ls[1], password="%s"%ls[2])
        stdin, stdout, stderr = p.exec_command("uname -a")
        opt = stdout.readlines()
        opt ="".join(opt)
        temp=open("%s.txt"%ls[0],"w")
        temp.write(opt)
        temp.close()
    except Exception as error:
        print(error)
cred.close()


                  

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