cancel
Showing results for 
Search instead for 
Did you mean: 

Python Script for remote SSH without password

Python Script for remote SSH without password

Sotiris_Salloum
New Contributor
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

4 REPLIES 4

Dave_Hammers
Extreme Employee
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')

Dave_Hammers
Extreme Employee
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')

eyeV
New Contributor III
Hi everibody!
Can I run this command in the same way?
scp2 vr "VR-Default" username@172.20.1.1:config.cfg config.cfg

Thanks Dave ,

It worked like a charm !

Regards
Sotiris
GTM-P2G8KFN