Python with Network devices

Hi All,

I’ve written below program.
to run the show command on the router/switch.
List of device IP address are as input through the file.
When I ran this command, i’m getting error. Error detail I’ve pasted below.

from netmiko import ConnectHandler
from getpass import getpass

username = input("Enter user name: ")
password = getpass("Enter password: ")

IP_file = input("Enter device IP file name: ")
myfile = open(“C:\Users\mtmoh\OneDrive\Desktop\Pyhton-test\Python_Practice\{Device_ip_file}”.format(Device_ip_file = IP_file), “r”)

Router_Connect = [myfile.read]

for access_ssh in Router_Connect:
d_ip_file = access_ssh
device_detail = {
‘device_type’:‘cisco_ios’,
‘ip’:d_ip_file,
‘username’:username,
‘password’:password
}

ssh = ConnectHandler(**device_detail)

commands = input("Enter the command file name: ")

show_commands = ssh.send_command(commands)
output = open("C:\\Users\\mtmoh\\OneDrive\\Desktop\\Pyhton-test\\Python_Practice\\{output_file}.txt)".format(output_file = input("Enter file name:")), 'w')
output.write("All output has saved")
output.close()
print("Show commands in file exported")                  

Result and Error Detail:

ssh = ConnectHandler(**device_detail)  File "C:\Users\mtmoh\AppData\Roaming\Python\Python310\site-packages\netmiko\ssh_dispatcher.py", line 365, in ConnectHandler
return ConnectionClass(*args, **kwargs)

File “C:\Users\mtmoh\AppData\Roaming\Python\Python310\site-packages\netmiko\base_connection.py”, line 310, in init
self.host = ip.strip()
AttributeError: ‘builtin_function_or_method’ object has no attribute ‘strip’
PS C:\Users\mtmoh>

Please help me what is wrong on this script or what is missing.

Input device IP address as a file.
Commands also will be sharing through the file.
program should connect to each device IP mentioned in the device IP file and run all show commands in the show_command file name.

Please wrap code in triple backticks to preserve the formatting:

```
if True:
    print(''Hello world!')
```

You can do that by selecting the code and then clicking the </> button.

Please post the complete traceback wrapped in backticks:

```
(traceback lines)
```

You have myfile.read, but that puts the method itself in the list. It should be myfile.read() to call the method and read the file.

I’ve updated with ‘’’ backticks.

‘’’
from netmiko import ConnectHandler
from getpass import getpass

username = input("Enter user name: ")
password = getpass("Enter password: ")

IP_file = input("Enter device IP file name: ")
myfile = open(“C:\Users\mtmoh\OneDrive\Desktop\Pyhton-test\Python_Practice\{Device_ip_file}”.format(Device_ip_file = IP_file), “r”)

Router_Connect = [myfile]

for access_ssh in Router_Connect:
print("Connected to the device: " + access_ssh)
d_ip_file = access_ssh
device_detail = {
‘device_type’:‘cisco_ios’,
‘ip’:d_ip_file,
‘username’:username,
‘password’:password
}

ssh = ConnectHandler(**device_detail)

commands = input("Enter the command file name: ")

show_commands = ssh.send_command(commands)
output = open("C:\\Users\\mtmoh\\OneDrive\\Desktop\\Pyhton-test\\Python_Practice\\{output_file}.txt)".format(output_file = input("Enter file name:")), 'w')
output.write("All output has saved")
output.close()
print("Show commands in file exported")

‘’’

You wrapped the code in apostrophes ''' instead of backticks ```. It’s easier to just select the code and then click </>.

Your code now has:

Router_Connect = [myfile]

which should be:

Router_Connect = [myfile.read()]

as I said in my previous post.

No you have not.

As you can see that is not 3 backticks - my guess is there is a smart quotes conversion going on that is changing what you are typing.

Try using 3 tilda or the </> button.

~~~
quoted code here
~~~