Automatically execute commands using List and for statements

Asked 2 years ago, Updated 2 years ago, 37 views

In Python 3.6.7, we have written code for the automatic execution of Cisco commands.

[Purpose]

Add/remove commands to the list and list them in a subsequent for statement.  It will process the commands on the list one after another.

Actual Code

 command = ['terlen 0', 'show clock', 'show version', 'sh inventory', 'exit' ]
comlist=", ".join(command+'\r\n')
for com_set in list:
    com_set=str.encode(com_set)
    ser.write(comlist)

[Error Contents]

Traceback (most recent call last):
  File "l:/python/coltpython/prompt.py", line 115, in<module>
    comlist=", ".join(command+'\r\n')
TypeError: can only concatenate list(not "str") to list
PSL:\python>  

So I have a question.
Would it be possible to arrange the list in str and process it in for statements one after another?

Thank you very much for letting me know.

python python3

2022-09-30 19:17

1 Answers

You don't have to go out of your way to string the elements in a list.

 commands=['terlen 0', 'show clock', 'show version', 'sh inventory', 'exit']
for command in commands:
    # use the command here


2022-09-30 19:17

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.