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:
JavaScript
x
51
51
1
import netmiko
2
import csv
3
import datetime
4
import csv
5
import threading
6
from datetime import datetime
7
8
nodenum=1
9
f=open('routers.csv', 'r') #Hostname file
10
c=f.read()
11
file_as_list = c.splitlines()
12
13
14
with open('Output.csv','w') as f: #Output file
15
write = csv.writer(f)
16
write.writerow(['Hostname', 'Cellular'])
17
18
logf = open("error.csv", "a") #Failed Connection file
19
loga = csv.writer(logf)
20
loga.writerow(["Hostname"])
21
22
for i in file_as_list :
23
print ("Node", nodenum, "...Checking IP Address...", i)
24
try:
25
Connection = netmiko.ConnectHandler(ip=i, device_type="cisco_ios" , username="x", password="y", verbose=False)
26
except:
27
try:
28
print("Cannot connect via SSH. Trying Telnet")
29
Connection = netmiko.ConnectHandler(ip=i, device_type="cisco_ios_telnet" , username="x", password="y", verbose=False)
30
31
except:
32
print("SSH and Telnet Failed")
33
print("")
34
now = str(datetime.now().strftime("%Y-%m-%d %H:%M:%S"))
35
loga.writerow([i])
36
nodenum = nodenum+1
37
continue
38
39
hostname = (Connection.send_command("show run | include hostname"))
40
cellular = (Connection.send_command("sh ip int brief"))
41
Connection.disconnect
42
43
44
45
write.writerow([hostname,cellular])
46
nodenum = nodenum +1
47
48
logf.close()
49
f.close()
50
51
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
JavaScript
1
5
1
Router
2
192.168.1.1
3
192.168.1.2
4
192.168.1.3
5
I think this is what you need
JavaScript
1
34
34
1
import csv
2
3
from netmiko import ConnectHandler
4
5
with open("routers.csv", "r", newline="") as csvfile:
6
spamreader = csv.reader(csvfile, delimiter="n")
7
next(spamreader) # skip header line in csv file
8
routers = [router[0] for router in spamreader]
9
10
with open("output.csv", "w", newline="n") as f: # Output file
11
writer = csv.writer(f)
12
writer.writerow(["Hostname", "Output"])
13
14
for nodenum, router in enumerate(routers, start=1):
15
print(f"Node: {nodenum}. Checking IP Address {router}...")
16
try:
17
with ConnectHandler(
18
device_type="cisco_ios_telnet",
19
ip=router,
20
username="x",
21
password="y",
22
) as conn:
23
hostname = conn.find_prompt()[:-1] # get hostname
24
# the command you want to get
25
intf_brief = conn.send_command("show ip interface brief")
26
27
writer.writerow([hostname, intf_brief])
28
except:
29
print(f"Failed to connect to {router}")
30
with open("error.csv", "w", newline="n") as errlog:
31
loga = csv.writer(errlog)
32
loga.writerow(["Router IP"])
33
loga.writerow([router])
34
EDIT:
Updated Python script to read csv file using
csv.reader()
output.csv
file
JavaScript
1
11
11
1
Hostname,Output
2
R1,"Interface IP-Address OK? Method Status Protocol
3
GigabitEthernet1 10.10.10.48 YES NVRAM up up
4
GigabitEthernet2 unassigned YES NVRAM administratively down down
5
GigabitEthernet3 unassigned YES NVRAM administratively down down "
6
R2,"Interface IP-Address OK? Method Status Protocol
7
GigabitEthernet1 10.10.10.48 YES other up up
8
GigabitEthernet2 10.255.255.1 YES other up up
9
GigabitEthernet3 unassigned YES NVRAM down down
10
"
11