Python Script for remote SSH without password

  • 0
  • 1
  • Question
  • Updated 3 years ago
  • Answered
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
Photo of Sotiris Salloumis

Sotiris Salloumis

  • 114 Points 100 badge 2x thumb

Posted 4 years ago

  • 0
  • 1
Photo of Dave Hammers

Dave Hammers, Dir SW Engineering

  • 3,532 Points 3k badge 2x thumb
Official Response
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": <number> 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')