ssh - python上的exscript ssh

标签 ssh command paramiko

我在说明问题上有问题,我想通过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/

相关文章:

ssh - 无法将VS Code连接到Linux机器进行远程开发

ubuntu - 无法通过公网 IP 访问 EC2 实例

command - '$'在命令行命令中是什么意思?

python从第三台机器连接到mysql

ruby-on-rails - Net-ssh 执行超时?

amazon-web-services - 无法连接到AWS EC2

python - 使用 python 中的 ruby​​ gem/命令

c++ - CMake 在构建之前运行自定义命令?

python-3.x - 如何使用 paramiko 或其他方式从 vps ip 获取本地 PC 上的文件?

python - paramiko 中的 ftplib retrbinary?