exthon脚本在python上

时间:2013-07-11 11:01:08

标签: ssh command paramiko

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

3 个答案:

答案 0 :(得分:3)

登录功能需要一个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()

答案 1 :(得分: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()

答案 2 :(得分:0)

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

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

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