couldn't get output from Netmiko Script

147 Views Asked by At

I am a little bit new in python programing i have a task to get info from a lot of Cisco Devices , According to the below Script i am trying to SSH or telnet to the devices but i cannot get any code output , i have testing the code with file containing 4 IP addresses only. Seems there is a problem in accessing the devices

from netmiko import ConnectHandler
import telnetlib
import os

cwd = os.getcwd()  # Get the current working directory (cwd)
files = os.listdir(cwd)  # Get all the files in that directory
print("Files in %r: %s" % (cwd, files))

with open ('/home/user1/Ahmed_Yousry/telnet_ip.txt') as f:
    Devices= f.read().splitlines()

username=[]
password=[]

for device in Devices:
    print('connecting to device ' + device)
    IP_Address_of_device= device
    HOST = device
####### Try Different Username and Password
    try:
        username="xxx"
        password="xxx"
    except:
        try:
            username="xxx"
            password="xxx"
        except:
            try:
                username="xxx"
                password="xxx"
            except:
                try:
                    username="xxx"
                    password="xxx"
                except:
                    try:
                        username="xxx"
                        password="xxx"
                    except:
                        try:
                            username="xxx"
                            password="xxx"
                        except:
                            try:
                                username="xxx"
                                password="xxx"
                            except:
                                try:
                                    username="xxx"
                                    password="xxx"
                                except:
                                    try:
                                        username="xxx"
                                        password="xxx"
                                    except:
                                        try:
                                            username="xxx"
                                            passwo="xxx"
                                        except:
                                            try:
                                                username="xxx"
                                                password="xxx"
                                            except:
                                                print('Unable to connect!' + device)
                                            continue

    try:
#### Connection to ssh
        IOS_device= {
                     'device_type':'cisco_ios_ssh',
                     'host':IP_Address_of_device,
                     'username':username,
                     'password':password,
                     'global_delay_factor':30}
        net_connect = ConnectHandler(**IOS_device)
        output = net_connect.send_command('show run | i username','show privilege')
        Hostname= net_connect.send_command ('show run | i hostname')
        security_Check= print ('output' + 'Hostname')
    except:
            try:
###### Connection to telnet
                    tn = telnetlib.Telnet(HOST)
                    tn.read_until("username: ")
                    tn.write(username + "\n")
                    if password:
                        tn.read_until("Password: ")
                        tn.write(password + "\n")
                    tn.write(b"show run | i hostname\n")
                    tn.write(b"show run | i username\n")
                    tn.write(b"show privilege\n")
                    security_Check= tn.read_all()
            except:
                    print('Unable to telnet or ssh ')
            continue
    file = open ("security_Check.txt", 'w')
    file.write (str(security_Check))
    file.close ()

** code Output :**

connecting to device x.x.x.x Unable to telnet or ssh connecting to device x.x.x.x Unable to telnet or ssh connecting to device x.x.x.x Unable to telnet or ssh connecting to device x.x.x.x Unable to telnet or ssh

0

There are 0 best solutions below