1

我的脚本有问题,我想通过 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,但它不允许我运行多个命令。

4

3 回答 3

3

login 函数需要一个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()
于 2013-09-20T19:34:58.003 回答
0
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()
于 2014-12-24T21:33:32.370 回答
0

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

在多节点登录时插入某些命令以重新确认链接的状态,这应该是“txt”文件或“log”文件中的文档以进行确认,直到下面

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()
于 2017-06-04T07:32:14.407 回答