- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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.
Accepted Solutions

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 10:17 PM
cmd = c.read().splitlines()
Which essentially splits a string into a list. And then you are trying -
output = net_connect.send_command(cmd)
cmd is a list and not a individual command and so the error. Ideally, you should iterate over the items -
for command in cmd:
print(net_connect.send_command(command))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-14-2020 10:17 PM
cmd = c.read().splitlines()
Which essentially splits a string into a list. And then you are trying -
output = net_connect.send_command(cmd)
cmd is a list and not a individual command and so the error. Ideally, you should iterate over the items -
for command in cmd:
print(net_connect.send_command(command))
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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)
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-15-2020 12:15 AM
Both solution works! Thanks. I wish i can accept both answers.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
