radmanesh1982 / Siavash

0 stars 0 forks source link

Hi, I just created a python code for cisco switches but i receive errors when i run it... #1

Open radmanesh1982 opened 10 months ago

radmanesh1982 commented 10 months ago

import netmiko import csv import datetime

nodenum = 1 f=open('nodeips.csv', 'r') c=f.readlines()

with open('nativevlans.csv', 'w', newline='') as f: write = csv.writer(f) write.writerow(['Hostname', 'IP address', 'Trunks'])

for i in c :
    print ("node", nodenum, " - Checking IP Address ", i)
    try:
        connection = netmiko.ConnectHandler(ip=i, device_type="cisco_ios", username="myuser", password="mypass")
    except:

        try:
            print('Cannot connect via SSH, Trying Telnet')
            connection = netmiko.ConnectHandler(ip=i, device_type="cisco_ios_telnet", username="myuser", password="mypass")

        except:
            print('Cannot connect via SSH, or Telnet, node may be down or not using standard credentials, logging to error.log')
            print("")
            now = str(datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
            nodenum = nodenum + 1
            logf = open("error.log", "a")
            logf.write(now)
            logf.write("- Cannot connect via SSH or Telnet to ")
            logf.write(i)
            logf.write()
            logf.close()                
            continue

        hostname = (connection.send_command("sh run | include hostname"))
        trunk = (connection.send_command("sh int trunk"))
        connection.disconnect

        print(hostname)
        print(trunk)
        print("")

        write.writerow([hostname, i, trunk])
        nodenum = nodenum + 1
        print("\n\n")

f.close()        
print("FINISH")
radmanesh1982 commented 10 months ago

I got an error about "hostname" and when credentials of switches is incorrect, the program will brake...