Python Script for remote SSH without password
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
‎02-04-2015 03:31 PM
Hi,
I want to create a universal port profile which will run a python script in order connect to a remote ssh host and check some health information.
I've tried the following
import pexpectimport exshexpect
exosPrompt = '::--:--::'
p = exshexpect.exshspawn(exosPrompt, exsh.clicmd)
p.sendline('ssh vr "VR-OAM" root@192.168.140.190')
idx = p.expect([passPrompt, pexpect.EOF, pexpect.TIMEOUT])
p.sendline('mysecretpassword')
p.sendline('exit')
But i always get the prompt to enter it manually ignoring the two last commands.
Regards
Sotiris
I want to create a universal port profile which will run a python script in order connect to a remote ssh host and check some health information.
I've tried the following
import pexpectimport exshexpect
exosPrompt = '::--:--::'
p = exshexpect.exshspawn(exosPrompt, exsh.clicmd)
p.sendline('ssh vr "VR-OAM" root@192.168.140.190')
idx = p.expect([passPrompt, pexpect.EOF, pexpect.TIMEOUT])
p.sendline('mysecretpassword')
p.sendline('exit')
But i always get the prompt to enter it manually ignoring the two last commands.
Regards
Sotiris
4 REPLIES 4
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
‎02-07-2015 08:39 PM
As another thought about determining the VR number, you could includes something like this to allow VR names to be translated to numbers automaticially in your script.
Example:
def vrNameToNumber(vrName):
vrId = None
with open('/proc/net/vr','r') as f:
for l in f.readlines():
rslt = l.split()
if rslt[3] == vrName:
vrId = int(rslt[0])
break
return vrId
print vrNameToNumber('VR-OAM')
print vrNameToNumber('VR-Default')
Example:
def vrNameToNumber(vrName):
vrId = None
with open('/proc/net/vr','r') as f:
for l in f.readlines():
rslt = l.split()
if rslt[3] == vrName:
vrId = int(rslt[0])
break
return vrId
print vrNameToNumber('VR-OAM')
print vrNameToNumber('VR-Default')
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
‎02-04-2015 10:33 PM
Since you are actually trying to use the ssh program, the EXOS shell is getting in the way.
My suggestion is to invoke ssh directly and just use pexpect.
The challenge with this method is how to determine the VR number for ssh.
E.g. VR-Mgmt = 0, VR-Default=2
Try running the CLI command:
run script cli2json.py -d show virtual-router
Then look for "vrId": when name2 is VR-OAM
the vrId is the number for -r below
import pexpect
p = pexpect.spawn('/exos/bin/ssh -r 2 root@192.168.140.190')
idx = p.expect(['password', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('mysecretpassword')
.
.
.
p.sendline('exit')
My suggestion is to invoke ssh directly and just use pexpect.
The challenge with this method is how to determine the VR number for ssh.
E.g. VR-Mgmt = 0, VR-Default=2
Try running the CLI command:
run script cli2json.py -d show virtual-router
Then look for "vrId":
the vrId is the number for -r below
import pexpect
p = pexpect.spawn('/exos/bin/ssh -r 2 root@192.168.140.190')
idx = p.expect(['password', pexpect.EOF, pexpect.TIMEOUT])
p.sendline('mysecretpassword')
.
.
.
p.sendline('exit')
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
‎02-04-2015 10:33 PM
Hi everibody!
Can I run this command in the same way?
scp2 vr "VR-Default" username@172.20.1.1:config.cfg config.cfg
Can I run this command in the same way?
scp2 vr "VR-Default" username@172.20.1.1:config.cfg config.cfg
Options
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Get Direct Link
- Report Inappropriate Content
‎02-04-2015 10:33 PM
Thanks Dave ,
It worked like a charm !
Regards
Sotiris
It worked like a charm !
Regards
Sotiris
