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")
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'])