首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >python上的exscript ssh

python上的exscript ssh
EN

Stack Overflow用户
提问于 2013-07-11 11:01:08
回答 3查看 4.8K关注 0票数 1

我对脚本有问题,我想去我的思科交换机,但我有一些麻烦

我写了两个剧本,

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

代码语言:javascript
复制
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()

但问题来了。我想让它自动,我不想输入用户和密码的手。所以我写了这个剧本

代码语言:javascript
复制
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()

但是它给了我错误输出..。

代码语言:javascript
复制
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,但是它不允许我运行多个命令。

EN

回答 3

Stack Overflow用户

发布于 2013-09-20 19:34:58

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

代码语言:javascript
复制
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()
票数 3
EN

Stack Overflow用户

发布于 2014-12-24 21:33:32

代码语言:javascript
复制
from Exscript.util.interact import read_login
from Exscript.protocols import SSH2
from Exscript import Host, Account

account1 = Account('uname','pwd')
conn = SSH2()
conn.connect('192.168.86.12')
conn.login(account1)
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()
票数 0
EN

Stack Overflow用户

发布于 2017-06-04 07:32:14

我是新来的,在我的工作中遇到很多困难,希望你们能在这方面有所帮助。

在多个节点登录时,插入一些命令来重新确认链接的状态,这应该是"txt“文件或"log”文件中的文档,以供确认。

代码语言:javascript
复制
from Exscript.protocols import SSH2    
from Exscript.util.file import get_hosts_from_file    
from Exscript import Account

accounts = [Account('myuser', 'mypassword')]    
conn = SSH2()          
hosts = get_hosts_from_file('myhosts.txt') 

def do_something(job, host, conn):
    conn.execute('sh int description | i PE')

start(hosts, accounts, do_something)    
conn.send('exit\r')    
conn.close()
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17591638

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档