Header Only - DO NOT REMOVE - Extreme Networks

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


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 🙂

6 replies

Userlevel 7
I've made this conversation "public" since there is no private information.
Drew C. wrote:

I've made this conversation "public" since there is no private information.

Thanks!
Userlevel 6
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
OscarK wrote:

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

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?
Userlevel 4
instead of exsh, I would consider using pexpect. e.g.,

import pexpect

p = pexpect.spawn('/exos/bin/ssh -r 2 admin@10.0.0.202')

idx = p.expect(['password', pexpect.EOF, pexpect.TIMEOUT])

p.sendline('password')

http://pexpect.sourceforge.net/pexpect.html
Userlevel 7
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).
Userlevel 4
I prefer to open another local telnet session using telnetlib. I think there is an easier way to do so.

import telnetlib

username = 'admin'
password = ''

CMD = 'scp user@X.X.X.X:summitX-X.X.X-patchX.X.xos summitX-X.X.X-patchX.X.xos
PW = 'password'

tn = telnetlib.Telnet('127.0.0.1', timeout=3)

tn.expect([r'\s*?telnet\s+?session.*?login:\s*'], 3)
tn.write(username + '\n')
tn.expect([r'\s*?password:Ws*'], 3)
tn.write(password + '\n')

tn.expect([r'\S+\.[0-9]{1,2} #'], 3)
tn.write(CMD + '\n')
tn.expect([r'\s*?password:Ws*'], 3)
tn.write(PW + '\n')
tn.expect([r'\S+\.[0-9]{1,2} #'], 60)

tn.close()
Userlevel 4
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()

Reply