I have a script that runs through a list of hostnames/IPs and outputs to a csv “hostname, output”, in the same cell.
I need that the Hostname stays in cell A1, and the Output stays on Cell B1 (and so on – A2 B2 etc)
Can you help me?
The code is this:
import netmiko import csv import datetime import csv import threading from datetime import datetime nodenum=1 f=open('routers.csv', 'r') #Hostname file c=f.read() file_as_list = c.splitlines() with open('Output.csv','w') as f: #Output file write = csv.writer(f) write.writerow(['Hostname', 'Cellular']) logf = open("error.csv", "a") #Failed Connection file loga = csv.writer(logf) loga.writerow(["Hostname"]) for i in file_as_list : print ("Node", nodenum, "...Checking IP Address...", i) try: Connection = netmiko.ConnectHandler(ip=i, device_type="cisco_ios" , username="x", password="y", verbose=False) except: try: print("Cannot connect via SSH. Trying Telnet") Connection = netmiko.ConnectHandler(ip=i, device_type="cisco_ios_telnet" , username="x", password="y", verbose=False) except: print("SSH and Telnet Failed") print("") now = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S")) loga.writerow([i]) nodenum = nodenum+1 continue hostname = (Connection.send_command("show run | include hostname")) cellular = (Connection.send_command("sh ip int brief")) Connection.disconnect write.writerow([hostname,cellular]) nodenum = nodenum +1 logf.close() f.close()
Advertisement
Answer
I need that the Hostname stays in cell A1, and the Output stays on Cell B1 (and so on – A2 B2 etc)
routers.csv
file
Router 192.168.1.1 192.168.1.2 192.168.1.3
I think this is what you need
import csv from netmiko import ConnectHandler with open("routers.csv", "r", newline="") as csvfile: spamreader = csv.reader(csvfile, delimiter="n") next(spamreader) # skip header line in csv file routers = [router[0] for router in spamreader] with open("output.csv", "w", newline="n") as f: # Output file writer = csv.writer(f) writer.writerow(["Hostname", "Output"]) for nodenum, router in enumerate(routers, start=1): print(f"Node: {nodenum}. Checking IP Address {router}...") try: with ConnectHandler( device_type="cisco_ios_telnet", ip=router, username="x", password="y", ) as conn: hostname = conn.find_prompt()[:-1] # get hostname # the command you want to get intf_brief = conn.send_command("show ip interface brief") writer.writerow([hostname, intf_brief]) except: print(f"Failed to connect to {router}") with open("error.csv", "w", newline="n") as errlog: loga = csv.writer(errlog) loga.writerow(["Router IP"]) loga.writerow([router])
EDIT:
Updated Python script to read csv file using
csv.reader()
output.csv
file
Hostname,Output R1,"Interface IP-Address OK? Method Status Protocol GigabitEthernet1 10.10.10.48 YES NVRAM up up GigabitEthernet2 unassigned YES NVRAM administratively down down GigabitEthernet3 unassigned YES NVRAM administratively down down " R2,"Interface IP-Address OK? Method Status Protocol GigabitEthernet1 10.10.10.48 YES other up up GigabitEthernet2 10.255.255.1 YES other up up GigabitEthernet3 unassigned YES NVRAM down down "