Python – Script9: Device accessibility via SSH (Paramiko)

Step1: Configure file with all IP details of multiple devices
	nano myswitches
192.168.10.21
192.168.10.22
192.168.10.23
192.168.10.24
Step2: Configure file with for python script
	nano deviceaccess.py
import paramiko
import time
import getpass
 
username = raw_input(“Enter your telnet username:”)
password = getpass.getpass()
 
f = open (‘myswitches’)
 
for line in f:
   try:
      ip_address = line.strip()
      ssh_client = paramiko.SSHClient()
      ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
      ssh_client.connect(hostname=ip_address,username=username,password=password)
 
      remote_connection = ssh_client.invoke_shell()
 
      remote_connection.send(“exit\n”)
   except:
                remote_connection = (“socket.error”)
 
   result = remote_connection
 
   if result == “socket.error”:
                print ip_address, “Not Accessible”
   else:
                print ip_address, “Accessible”
raw_input(“Connectivity check done, press Enter to exit: “)

Network Architect | CCIEx3 #29824 JNCIE #2197 VCIX-NV

Leave a Comment