【发布时间】:2018-10-15 16:26:07
【问题描述】:
我一直在编写将在 python 中执行特定命令的代码
try:
portNumber = ('\nPlease pick a Port#: ')
description = str('Please add port description: ')
config_commands = ('port set port' +portNumber
'description' +description)
#Should become ['port set port' +# description +description]
print ('Accessing port') # should be accessing th eport
output = net_connect.send_command('port set port' [portNumber] [description]) # i
returns the output of the config_commands
print (output) #should show config commands
print ('Showing New Configuration')
output = net_connect.send_command_expect('port show status')
print (output) # print new config
print ('Saving Configuration...')
output = net_connect.send_command('configuration save') # returns output of
specified command
print (output)
但它总是失败
任何想法?
这似乎是一个组合的问题
[root@localhost ansible]# python show.py
File "show.py", line 37
'description' +description)
output = net_connect.send_command('port set port' [portNumber] description
[description]) # returns the output of the config_commands
TypeError: string indices must be integers, not str
【问题讨论】:
-
请澄清。如果错误发生在
'description' +description)行,或者output = net_connect.send_command('port set port' [portNumber] description行?虽然这两行都有问题,但它们不能都引发错误。 -
为什么不直接将
config_commands传递给send_command?看起来这就是你想要做的事情 -
您可能也想使用
input函数。也许通过一个教程来更好地了解python的基础知识…… -
感谢您的建议。我将研究输入函数以获得更好的主意。 @SamMason
-
@Kevin 无论我怎么写,似乎都给了我一个错误。