I'm learning this, unsure of what the problem is. Please assist. Thank you.
import getpass
import sys
import threading
import socket
from multiprocessing import Queue
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoAuthenticationException
# import time
from netmiko.ssh_exception import NetMikoTimeoutException
from paramiko.ssh_exception import SSHException
routers = [open('18_routers')]
ssh_username = input('Enter Username: ')
ssh_password = getpass.getpass('Enter Your Password: ')
# Define Switches IPs
# routers = []
with open('18_routers') as f:
for line in f:
line = line.strip()
try:
socket.inet_aton(line)
routers.append(line)
except socket.error:
print("Invalid IP address " + line)
def ssh_session(router, threads):
threads_dict = {}
# for IP in IP_LIST:
router = {
'device_type': 'cisco_ios',
'ip': router,
'username': ssh_username,
'password': ssh_password,
}
try:
net_connect = ConnectHandler(**router)
except NetMikoTimeoutException:
print('Device not reachable')
pass
except NetMikoAuthenticationException:
print('Authentication Failure')
pass
except SSHException:
print('Make sure Username & Password is Correct or SSH is enabled \n')
pass
else:
# Configure Router based on configuration file
output = net_connect.send_config_from_file(config_file='18_router_config')
threads.put(threads_dict)
print(output)
# Save Router Configuration
print('\n Saving the Router configuration \n')
output = net_connect.save_config()
threads.put(threads_dict)
print(output)
# time.sleep(1)
# Show Hostname using the Show Run command and Include variable
output1 = net_connect.send_command('show run | inc hostname').replace('hostname', '')
threads.put(threads_dict)
print(output1)
# time.sleep(1)
# Show IP Interface Brief
output2 = net_connect.send_command('show run')
orig_stdout = sys.stdout
sys.stdout = open((output1.replace("\n", '')) + "_config" + ".txt", "w+")
threads.put(threads_dict)
# time.sleep(1)
# Print Hostname and IP Interface Brief to be saved to File
print(output1, "\r", output2)
sys.stdout.close()
sys.stdout = orig_stdout
threads.put(threads_dict)
print('\n Looks Like You Are Done, I Am Now Closing The Connection \n')
if __name__ == "__main__":
threads = Queue()
for router in routers:
th = threading.Thread(target=ssh_session, args=(router, threads))
# threads.append(th)
# for th in threads:
th.start()
# for th in threads:
# th.join()
# Wait for all threads to complete
main_thread = threading.currentThread()
for some_thread in threading.enumerate():
if some_thread != main_thread:
some_thread.join()
# Retrieve everything off the queue - k is the router IP, v is output
# You could also write this to a file, or create a file for each router
# while not threads.empty():
# my_dict = threads.get()
# for k, val in my_dict():
# print(k)
# print(val)
# my_dict.join()
Error Message:
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\Program Files\Python39\lib\threading.py", line 950, in _bootstrap_inner
self.run()
File "C:\Program Files\Python39\lib\threading.py", line 888, in run
self._target(self._args, self._kwargs)
File "C:\Users\nicholas\Google Drive\pynet-script\netmiko_cisco\netmikocisco_mulit-thread", line 40, in ssh_session
net_connect = ConnectHandler(router)
File "C:\Users\nicholas\PycharmProjects\NetworkAutomation_Scripts\venv\lib\site-packages\netmiko\ssh_dispatcher.py", line 312, in ConnectHandler
return ConnectionClass(args, *kwargs)
File "C:\Users\nicholas\PycharmProjects\NetworkAutomation_Scripts\venv\lib\site-packages\netmiko\cisco\cisco_ios.py", line 17, in __init__
return super().__init__(args, **kwargs)
File "C:\Users\nicholas\PycharmProjects\NetworkAutomation_Scripts\venv\lib\site-packages\netmiko\base_connection.py", line 236, in __init__
self.host = ip.strip()
AttributeError: '_io.TextIOWrapper' object has no attribute 'strip'
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R1(config)#terminal length 0
^
% Invalid input detected at '^' marker.
R1(config)#interface lo21
R1(config-if)#ip add 2.2.2.2 255.255.255.255
R1(config-if)#no shut
R1(config-if)#end
R1#configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R3(config)#terminal length 0
^
% Invalid input detected at '^' marker.
R3(config)#interface lo21
R3(config-if)#ip add 2.2.2.2 255.255.255.255
R3(config-if)#no shut
R3(config-if)#end
R3#
Saving the Router configuration
Saving the Router configuration
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R4(config)#terminal length 0
^
% Invalid input detected at '^' marker.
R4(config)#interface lo21
R4(config-if)#ip add 2.2.2.2 255.255.255.255
R4(config-if)#no shut
R4(config-if)#end
R4#
Saving the Router configuration
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R2(config)#terminal length 0
^
% Invalid input detected at '^' marker.
R2(config)#interface lo21
R2(config-if)#ip add 2.2.2.2 255.255.255.255
R2(config-if)#no shut
R2(config-if)#end
R2#
Saving the Router configuration
configure terminal
Enter configuration commands, one per line. End with CNTL/Z.
R5(config)#terminal length 0
^
% Invalid input detected at '^' marker.
R5(config)#interface lo21
R5(config-if)#ip add 2.2.2.2 255.255.255.255
R5(config-if)#no shut
R5(config-if)#end
R5#
Saving the Router configuration
write mem
Building configuration...
[OK]
R4#
write mem
Building configuration...
[OK]
R3#
write mem
Building configuration...
[OK]
R2#
write mem
Building configuration...
[OK]
R1#
write mem
Building configuration...
[OK]
R5#
R4 R3
R2
R5
R1
Looks Like You Are Done, I Am Now Closing The Connection
Looks Like You Are Done, I Am Now Closing The Connection
Looks Like You Are Done, I Am Now Closing The Connection
Looks Like You Are Done, I Am Now Closing The Connection
Looks Like You Are Done, I Am Now Closing The Connection
Process finished with exit code 0
You are setting the "routers" variable to include an open filehandle:
routers = [open('18_routers')]
And then you are later trying to use that list that contains that file handle reference.
self.host = ip.strip()
AttributeError: '_io.TextIOWrapper' object has no attribute 'strip'
You are setting the "routers" variable to include an open filehandle:
routers = [open('18_routers')]And then you are later trying to use that list that contains that file handle reference.
self.host = ip.strip() AttributeError: '_io.TextIOWrapper' object has no attribute 'strip'
Any suggestions on how to apply the fix? I see what you are saying, but I'm lost on the exact solution. Thanks by the way for the quick response.
Replace:
routers = [open('18_routers')]
with
routers = []
Replace:
routers = [open('18_routers')]with
routers = []
Amazing! That worked. Thanks for coming to the rescue!!