[Python] Whats the best way to get the password prompt

  • 1
  • 1
  • Question
  • Updated 2 years ago
  • Answered
  • (Edited)
I am trying to make a little python script which is able to update the switches. However I am little confused by the "Getting started Python guide". Is there any option to let the python script enter the password for the remote connection ?

Thanks :-)
Photo of Radoje Stojisic

Radoje Stojisic

  • 90 Points 75 badge 2x thumb

Posted 2 years ago

  • 1
  • 1
Photo of Drew C.

Drew C., Community Manager

  • 37,308 Points 20k badge 2x thumb
I've made this conversation "public" since there is no private information.
Photo of OscarK

OscarK, ESE

  • 7,702 Points 5k badge 2x thumb
Something like below ? 
       host=host
        user=user
        password=password
        prompt=re.compile(r'\S+\d+\s[#>]\s')
        loginprompt=r"login"
        passwordprompt=r"assword"
        failedlogin = r"Login incorrect"
        connected = True
        try:
            tn = telnetlib.Telnet(host)
            tn.set_debuglevel(0)
            tn.read_until(loginprompt,10)
            tn.write(user + "\n")
            tn.expect([passwordprompt],10)
            tn.write(password + "\n")
            loginresponse = tn.expect([prompt,failedlogin])
            if loginresponse[0] == 1:
                print "Login failed"
                connected = False
                tn.close()
            else:
                tn.write("disable clipaging\n")
                tn.expect([prompt])
        except:
            print "Could not connect to switch "+host
            connected = False
Photo of Radoje Stojisic

Radoje Stojisic

  • 90 Points 75 badge 2x thumb
No telnet allowed.

Here is what I've tried.

getImage = "scp USER@XXXX:summitX-X.X.X-patchX.X.xos summitX-X.X.X-patchX.X.xos"

exsh.clicmd(getImage)

Now the switch starts the download. How do I get the PW propmpt or even give the script a password via an argument?
(Edited)
Photo of Erik Auerswald

Erik Auerswald, Embassador

  • 12,782 Points 10k badge 2x thumb
Did you consider using SSH with key-based authentication? This way you can execute any command on the switch without the need to enter a password, as long as the private key is available to the SSH client (usually using ssh-agent).
Photo of Kevin Kim

Kevin Kim, Employee

  • 2,266 Points 2k badge 2x thumb
Another way to skin a cat. Telnet session to control VR will open although telnet is globally disabled. 

import subprocess
import time

p = subprocess.Popen(['telnet', '-v', '1', '127.0.0.1'], stdin = subprocess.PIPE)
print >> p.stdin, 'admin'
print >> p.stdin, '\n'
print >> p.stdin, 'scp2 scpuser@10.0.0.1:bd8800-15.7.2.9.xos bd8800-15.7.2.9.xos'
time.sleep(3)
print >> p.stdin, 'scppw'
time.sleep(60)
p.terminate()