cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
13255
Views
10
Helpful
4
Replies

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

write_erase
Level 1
Level 1

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

Manoj Papisetty
Cisco Employee
Cisco Employee
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

omz
VIP Alumni
VIP Alumni
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

4 Replies 4

Manoj Papisetty
Cisco Employee
Cisco Employee
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))

omz
VIP Alumni
VIP Alumni
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

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

dominbajo07
Level 1
Level 1

 

 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