问题
Im having problem with the exscript, i want to ssh to my cisco switches, but im having some trouble
I wrote 2 scripts,
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: I have tried also with paramiko, but it doesnt allow me to run multiple commands.
回答1:
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()
回答2:
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()
回答3:
I am new in this stuff and getting lot of hard time in my job,hope you guys help in this.
While login in multi nodes insert a certain commands to reconfirm the status of links and this should be document in a "txt" file or "log" file for confirmation I reached till below
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/exscript-ssh-on-python