gpt4 book ai didi

ssh - python上的exscript ssh

转载 作者:行者123 更新时间:2023-12-02 14:15:16 27 4
gpt4 key购买 nike

我在说明问题上有问题,我想通过SSH切换到我的Cisco交换机,但是我遇到了一些麻烦

我写了两个脚本,

有了这个我没有任何问题,我可以通过运行脚本并输入用户名和密码来更改默认网关:

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,但是它不允许我运行多个命令。

最佳答案

登录函数需要一个Exscript.Account对象。将您的用户名和密码加载到Account中并将其传递。

from Exscript.protocols import SSH2
from Exscript import Account

account = Account('user', 'password')
conn = SSH2()
conn.connect('192.168.86.12')
conn.login(account)
# ...
conn.close()

关于ssh - python上的exscript ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17591638/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com