我的脚本有问题,我想通过 ssh 连接到我的 cisco 交换机,但我遇到了一些问题
我写了2个脚本,
有了这个我没有任何问题,我可以通过运行脚本并输入用户和密码来更改默认网关:
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
account = read_login()
enter code here`conn = SSH2()
conn.connect('192.168.86.12')
conn.login(account)
conn.execute('conf t')
conn.execute('no ip default-gateway')
conn.execute('ip default-gateway 192.168.68.10')
print "Response was:", repr(conn.response)
conn.send('exit\r')
conn.close()
但是问题来了。我想让它自动,我不想手动输入用户名和密码。所以我写了这个脚本,
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
#account = read_login()
conn = SSH2()
conn.connect('192.168.86.12')
conn.login('user','password')
conn.execute('conf t')
conn.execute('no ip default-gateway')
conn.execute('ip default-gateway 192.168.68.10')
print "Response was:", repr(conn.response)
conn.send('exit\r')
conn.close()
但它给了我这个错误输出..
Traceback (most recent call last):
File "nn.py", line 7, in <module>
conn.login('user','password')
File "/usr/local/lib/python2.7/dist-packages/Exscript-DEVELOPMENT-py2.7.egg/Exscript/protocols/Protocol.py", line 591, in login
with self._get_account(account) as account:
File "/usr/local/lib/python2.7/dist-packages/Exscript-DEVELOPMENT-py2.7.egg/Exscript/protocols/Protocol.py", line 567, in _get_account
account.__enter__()
AttributeError: 'str' object has no attribute '__enter__'
ps:我也尝试过使用 paramiko,但它不允许我运行多个命令。