use pxssh class for ssh
replace expect method by expect_exact: much faster execution git-svn-id: http://svn.eurecom.fr/openair4G/trunk@6181 818b1a75-f10b-46b9-bf7c-635c3b92a50f
This commit is contained in:
@@ -45,7 +45,7 @@ def execute(oai, user, pw, host, logfile,logdir,debug):
|
|||||||
|
|
||||||
case = '01'
|
case = '01'
|
||||||
rv = 1
|
rv = 1
|
||||||
oai.send('cd $OPENAIR_TARGETS;')
|
oai.send_recv('cd $OPENAIR_TARGETS;')
|
||||||
|
|
||||||
try:
|
try:
|
||||||
test = '00'
|
test = '00'
|
||||||
|
|||||||
@@ -143,17 +143,17 @@ class core:
|
|||||||
self.oai.send(command)
|
self.oai.send(command)
|
||||||
|
|
||||||
if self.expect_echo:
|
if self.expect_echo:
|
||||||
cmd = self.oai.expect([re.escape(command), pexpect.TIMEOUT], timeout=timeout);
|
cmd = self.oai.expect_exact([command, pexpect.TIMEOUT], timeout=timeout);
|
||||||
if cmd != 0:
|
if cmd != 0:
|
||||||
raise log.err(self.failed(command, command,debug))
|
raise log.err(self.failed(command, command,debug))
|
||||||
|
|
||||||
if self.expect_response:
|
if self.expect_response:
|
||||||
index = self.oai.expect([re.escape(rsp1), re.escape(rsp2),'%', pexpect.TIMEOUT], timeout=timeout)
|
index = self.oai.expect_exact([rsp1, rsp2, pexpect.TIMEOUT], timeout=timeout)
|
||||||
|
|
||||||
if index == 0 or index == 1:
|
if index == 0 or index == 1:
|
||||||
return 'OK'
|
return 'OK'
|
||||||
elif index == 2:
|
elif index == 2:
|
||||||
self.oai.expect([re.escape(rsp1), re.escape(rsp2), pexpect.TIMEOUT], timeout=timeout)
|
self.oai.expect_exact([rsp1, rsp2, pexpect.TIMEOUT], timeout=timeout)
|
||||||
if self.flag_errors:
|
if self.flag_errors:
|
||||||
raise log.err(self.err(command))
|
raise log.err(self.err(command))
|
||||||
else:
|
else:
|
||||||
@@ -177,7 +177,7 @@ class core:
|
|||||||
self.oai.sendline(command)
|
self.oai.sendline(command)
|
||||||
else:
|
else:
|
||||||
self.oai.send(command)
|
self.oai.send(command)
|
||||||
index = self.oai.expect([re.escape(rsp1), re.escape(rsp2), pexpect.TIMEOUT], timeout=timeout);
|
index = self.oai.expect_exact([rsp1, rsp2, pexpect.TIMEOUT], timeout=timeout);
|
||||||
if index == 0 or index == 1:
|
if index == 0 or index == 1:
|
||||||
return self.oai.before
|
return self.oai.before
|
||||||
else:
|
else:
|
||||||
|
|||||||
+24
-50
@@ -35,6 +35,7 @@
|
|||||||
# @ingroup _test
|
# @ingroup _test
|
||||||
|
|
||||||
import pexpect
|
import pexpect
|
||||||
|
import pxssh
|
||||||
import time
|
import time
|
||||||
import os
|
import os
|
||||||
import array
|
import array
|
||||||
@@ -51,22 +52,8 @@ class openair(core):
|
|||||||
self.hostname = hostname
|
self.hostname = hostname
|
||||||
self.address = address
|
self.address = address
|
||||||
self.localhost = None
|
self.localhost = None
|
||||||
self.shell_prompt = '$'
|
|
||||||
core.__init__(self)
|
core.__init__(self)
|
||||||
|
|
||||||
def get_shell(self):
|
|
||||||
print 'get the bash \n'
|
|
||||||
self.prompt1 = self.shell_prompt
|
|
||||||
self.prompt2 = prompt
|
|
||||||
try:
|
|
||||||
self.sh = pexpect.spawn(SHELL)
|
|
||||||
index = self.sh.expect([self.prompt1, pexpect.TIMEOUT], timeout=10)
|
|
||||||
if index != 0:
|
|
||||||
print 'unable to spawn shell'
|
|
||||||
|
|
||||||
except Exception, val:
|
|
||||||
print "Error:", val
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def localhost(self):
|
def localhost(self):
|
||||||
if self.localhost :
|
if self.localhost :
|
||||||
@@ -91,40 +78,28 @@ class openair(core):
|
|||||||
stdout, stderr = proc.communicate()
|
stdout, stderr = proc.communicate()
|
||||||
return (stdout, stderr)
|
return (stdout, stderr)
|
||||||
|
|
||||||
def connect(self, username, password, prompt):
|
def connect(self, username, password, prompt='PEXPECT_OAI'):
|
||||||
self.prompt2 = self.shell_prompt
|
self.prompt1 = prompt
|
||||||
if not prompt :
|
self.prompt2 = prompt
|
||||||
self.prompt1 = self.prompt2
|
|
||||||
else :
|
|
||||||
self.prompt1 = prompt
|
|
||||||
|
|
||||||
while 1:
|
try:
|
||||||
try:
|
if not username:
|
||||||
if not username:
|
username = root
|
||||||
username = root
|
if not password:
|
||||||
if not password:
|
password = username
|
||||||
password = username
|
self.oai = pxssh.pxssh()
|
||||||
|
self.oai.login(self.address,username,password)
|
||||||
|
self.oai.sendline('PS1='+self.prompt1)
|
||||||
|
self.oai.PROMPT='PEXPECT_OAI'
|
||||||
|
# need to look for twice the string of the prompt
|
||||||
|
self.oai.prompt()
|
||||||
|
self.oai.prompt()
|
||||||
|
self.oai.sendline('uptime')
|
||||||
|
self.oai.prompt()
|
||||||
|
print self.oai.before
|
||||||
|
|
||||||
self.oai = pexpect.spawn('ssh -o "UserKnownHostsFile=/dev/null" -o "StrictHostKeyChecking=no" -o "ConnectionAttempts=1" ' \
|
except Error, val :
|
||||||
+ username + '@' + self.address)
|
print "Error: can't connect to"+username+"@"+self.address
|
||||||
|
|
||||||
index = self.oai.expect([re.escape(self.prompt1), re.escape(self.prompt2), pexpect.TIMEOUT], timeout=40)
|
|
||||||
if index == 0 :
|
|
||||||
return 'Ok'
|
|
||||||
else :
|
|
||||||
index = self.oai.expect(['password:', pexpect.TIMEOUT], timeout=40)
|
|
||||||
if index == 0 :
|
|
||||||
self.oai.sendline(password)
|
|
||||||
index = self.oai.expect([re.escape(self.prompt1), re.escape(self.prompt2), pexpect.TIMEOUT], timeout=10)
|
|
||||||
if index != 0:
|
|
||||||
print 'ERROR! could not login with SSH.'
|
|
||||||
print 'Expected ' + self.prompt1 + ', received >>>>' + self.oai.before + '<<<<'
|
|
||||||
sys.exit(1)
|
|
||||||
return 'Ok'
|
|
||||||
|
|
||||||
except Exception, val:
|
|
||||||
time.sleep(5)
|
|
||||||
print "Error:", val
|
|
||||||
|
|
||||||
|
|
||||||
def disconnect(self):
|
def disconnect(self):
|
||||||
@@ -153,8 +128,8 @@ class openair(core):
|
|||||||
#oai.send_nowait('rmmod nasmesh;')
|
#oai.send_nowait('rmmod nasmesh;')
|
||||||
os.system('rmmod nasmesh;')
|
os.system('rmmod nasmesh;')
|
||||||
else :
|
else :
|
||||||
#oai.send_nowait('echo '+pw+ ' | sudo -S rmmod nasmesh;')
|
oai.send_nowait('echo '+pw+ ' | sudo -S rmmod nasmesh;')
|
||||||
os.system('echo '+pw+ ' | sudo -S rmmod nasmesh;')
|
#os.system('echo '+pw+ ' | sudo -S rmmod nasmesh;')
|
||||||
except Error, val:
|
except Error, val:
|
||||||
print "Error removing oai network driver module:", val
|
print "Error removing oai network driver module:", val
|
||||||
|
|
||||||
@@ -166,9 +141,8 @@ class openair(core):
|
|||||||
if user == 'root' :
|
if user == 'root' :
|
||||||
oai.send_nowait('insmod ./nasmesh.ko;')
|
oai.send_nowait('insmod ./nasmesh.ko;')
|
||||||
else :
|
else :
|
||||||
oai.send_nowait('echo '+pw+ ' | sudo -S insmod ./nasmesh.ko;')
|
oai.send('echo '+pw+ ' | sudo -S insmod ./nasmesh.ko;')
|
||||||
|
|
||||||
oai.send_nowait('cd '+ pwd)
|
|
||||||
except Error, val:
|
except Error, val:
|
||||||
print "Error inserting oai network driver module:", val
|
print "Error inserting oai network driver module:", val
|
||||||
|
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
#! /usr/bin/python
|
||||||
#******************************************************************************
|
#******************************************************************************
|
||||||
|
|
||||||
# Eurecom OpenAirInterface
|
# Eurecom OpenAirInterface
|
||||||
@@ -53,7 +54,6 @@ import case05
|
|||||||
from openair import *
|
from openair import *
|
||||||
|
|
||||||
debug = 0
|
debug = 0
|
||||||
prompt2 = '$'
|
|
||||||
pw =''
|
pw =''
|
||||||
i = 0
|
i = 0
|
||||||
dlsim=0
|
dlsim=0
|
||||||
@@ -104,7 +104,6 @@ try:
|
|||||||
print '\n******* Note that the user <'+user+'> should be a sudoer *******\n'
|
print '\n******* Note that the user <'+user+'> should be a sudoer *******\n'
|
||||||
print '******* Connecting to the localhost to perform the test *******\n'
|
print '******* Connecting to the localhost to perform the test *******\n'
|
||||||
|
|
||||||
|
|
||||||
if not pw :
|
if not pw :
|
||||||
print "username: " + user
|
print "username: " + user
|
||||||
pw = getpass.getpass()
|
pw = getpass.getpass()
|
||||||
@@ -112,14 +111,7 @@ try:
|
|||||||
print "username: " + user
|
print "username: " + user
|
||||||
#print "password: " + pw
|
#print "password: " + pw
|
||||||
|
|
||||||
try:
|
oai.connect(user,pw)
|
||||||
prompt = os.getenv("PS1")[-2]
|
|
||||||
except :
|
|
||||||
#prompt = input('set your shell prompt: ')
|
|
||||||
prompt = '$'
|
|
||||||
print "your prompt is: " + prompt
|
|
||||||
|
|
||||||
oai.connect(user,pw,prompt)
|
|
||||||
#oai.get_shell()
|
#oai.get_shell()
|
||||||
except :
|
except :
|
||||||
print 'Fail to connect to the local host'
|
print 'Fail to connect to the local host'
|
||||||
|
|||||||
Reference in New Issue
Block a user