04-14-2020 09:30 PM
This is my code
from netmiko import ConnectHandler cisco_device = { 'device_type': 'cisco_ios', 'ip': 'R1', 'username': 'u', 'password': 'p' } with open('command.txt') as c: cmd = c.read().splitlines() net_connect = ConnectHandler(**cisco_device) output = net_connect.send_command(cmd) print(output)
command.txt
show clock show version | include IOS
output
user@linux:~$ python script.py Traceback (most recent call last): File "script.py", line 14, in <module> output = net_connect.send_command(cmd) File "/home/user/.local/lib/python3.7/site-packages/netmiko/utilities.py", line 347, in wrapper_decorator return func(self, *args, **kwargs) File "/home/user/.local/lib/python3.7/site-packages/netmiko/base_connection.py", line 1378, in send_command command_string = self.normalize_cmd(command_string) File "/home/user/.local/lib/python3.7/site-packages/netmiko/base_connection.py", line 1532, in normalize_cmd command = command.rstrip() AttributeError: 'list' object has no attribute 'rstrip' user@linux:~$
What should I do to fix this problem?
I tried to change `splitlines` to `split`, but it didn't change anything. Still getting the same error.
Solved! Go to Solution.
04-14-2020 10:17 PM
04-14-2020 11:32 PM - edited 04-14-2020 11:34 PM
with open('command.txt') as c: cmd = c.readlines() # print cmd to see the output print(cmd) for c in cmd: # use strip on a single command before send to the device output = net_conn.send_command(c.strip('\r\n')) print(output)
04-14-2020 10:17 PM
04-14-2020 11:32 PM - edited 04-14-2020 11:34 PM
with open('command.txt') as c: cmd = c.readlines() # print cmd to see the output print(cmd) for c in cmd: # use strip on a single command before send to the device output = net_conn.send_command(c.strip('\r\n')) print(output)
04-15-2020 12:15 AM
Both solution works! Thanks. I wish i can accept both answers.
01-06-2021 09:06 AM
Your Code:
net_connect = ConnectHandler(**cisco_device) output = net_connect.send_command(cmd) print(output)
Another solution:
net_connect = ConnectHandler(**cisco_device) output = net_connect.send_config_set(cmd) print(output)
It work, at least in my case.
Regards
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide