cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
185
Views
10
Helpful
3
Replies
Highlighted
Beginner

AttributeError: 'list' object has no attribute 'rstrip'

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.

2 ACCEPTED SOLUTIONS

Accepted Solutions
Highlighted
Cisco Employee

Re: AttributeError: 'list' object has no attribute 'rstrip'

The first operation is -
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))

View solution in original post

Highlighted
Rising star
Rising star

Re: AttributeError: 'list' object has no attribute 'rstrip'

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)

Screenshot 2020-04-15 at 07.31.56.png

View solution in original post

3 REPLIES 3
Highlighted
Cisco Employee

Re: AttributeError: 'list' object has no attribute 'rstrip'

The first operation is -
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))

View solution in original post

Highlighted
Rising star
Rising star

Re: AttributeError: 'list' object has no attribute 'rstrip'

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)

Screenshot 2020-04-15 at 07.31.56.png

View solution in original post

Highlighted
Beginner

Re: AttributeError: 'list' object has no attribute 'rstrip'

Both solution works! Thanks. I wish i can accept both answers.