str对象在脚本上不可调用

时间:2015-03-31 15:17:57

标签: python networking switch-statement

我正在网络交换机上运行脚本:

import exsh

def main():

        #result='0'
        #print result
        result=exsh.clicmd('show ver', capture=True)
        print result
        result=exsh.clicmd('sh switch', capture=True)
        print result
        result=exsh.clicmd('sh vlan', capture=True)
        print result
        exsh.clicmd('create vlan vlan10 tag 10')
        result=exsh.clicmd('sh vlan10', capture=True)
        print result
        exsh.clicmd=('del vlan10')
        result=exsh.clicmd('sh vlan', capture=True)
        print result

if True: main()

当我运行它时,我得到预期的输出,直到它到达第18行:

Traceback (most recent call last):
  File "/config/test.py", line 23, in <module>
    if True: main()
  File "/config/test.py", line 18, in main
    result=exsh.clicmd('sh vlan', capture=True)
TypeError: 'str' object is not callable

更令人意想不到的是,如果我之后立即再次运行,现在错误发生在第8行:

* X460-24p.2 # run script test.py
Traceback (most recent call last):
  File "/config/test.py", line 23, in <module>
    if True: main()
  File "/config/test.py", line 8, in main
    result=exsh.clicmd('show ver', capture=True)
TypeError: 'str' object is not callable

不确定如何追踪问题。

1 个答案:

答案 0 :(得分:2)

您设置exsh.clicmd=('del vlan10')使exsh.clicmd为字符串,然后尝试使用result=exsh.clicmd('sh vlan', capture=True)调用它:

In [1]: foo = ('del vlan10')

In [2]: type(foo)
Out[2]: str

In [3]: foo()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-3-624891b0d01a> in <module>()
----> 1 foo()

TypeError: 'str' object is not callable