python上的exscript ssh [英] exscript ssh on python

查看:135
本文介绍了python上的exscript ssh的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对解说词有疑问,我想通过SSH切换到我的cisco开关,但是我遇到了一些麻烦

Im having problem with the exscript, i want to ssh to my cisco switches, but im having some trouble

我写了2个脚本,

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

with this one I dont have any problem, I can change the default gateway just by running the script and entering the user and password:

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()






但是问题出在这里。我想使其自动运行,我不想手动输入用户名和密码。
所以我写了这个脚本,


But the problem comes here. I want to make it automatic, I dont want to enter the user and password by hand. So I wrote this script,

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()






但是给我这个错误输出。.


But it gives me this error output..

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

ps: I have tried also with paramiko, but it doesnt allow me to run multiple commands.

推荐答案

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

The login function expects an Exscript.Account object. Load your username and password into an Account and pass that in.

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()

这篇关于python上的exscript ssh的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆