我对脚本有问题,我想去我的思科交换机,但我有一些麻烦
我写了两个剧本,
通过这个我没有任何问题,只要运行脚本并输入用户和密码,我就可以更改默认网关:
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,但是它不允许我运行多个命令。
发布于 2013-09-20 19:34:58
登录函数需要一个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()发布于 2014-12-24 21:33:32
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()发布于 2017-06-04 07:32:14
我是新来的,在我的工作中遇到很多困难,希望你们能在这方面有所帮助。
在多个节点登录时,插入一些命令来重新确认链接的状态,这应该是"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()https://stackoverflow.com/questions/17591638
复制相似问题