-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
149 lines (133 loc) · 4.59 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
import os
from time import sleep
import time
import sys
import re
try:
from pexpect import pxssh as pxssh
except:
print("pxssh is not installled")
class SshConnection(object):
def __init__(self, hostname, username, password):
self.hostname = hostname
self.username = username
self.password = password
try:
self.ssh_conn = self.ssh_conn(self.hostname, self.username, self.password)
self.ssh_conn.PROMPT = "[#>$]"
self.ssh_conn.timeout = 5000
except Exception as err:
print("Connection to %s failed" % self.hostname)
def ssh_conn(self, hostname, username, password):
try:
print("Connecting to ", hostname)
self.ssh = pxssh.pxssh()
print(hostname, username, password)
self.ssh.login(hostname, username, password, original_prompt="[#>$]", login_timeout=5000, auto_prompt_reset=False)
print("Connected")
except pxssh.ExceptionPxssh as error:
print("Login failed")
print(str(error))
self.ssh = False
return self.ssh
def cmd(self, command, allowPrint=True, sleep=False, killApptest=False):
prompt_repeat = 0
retries = self.ssh_conn.timeout
tmpbuf = ''
self.ssh_conn.buffer = ''
self.ssh_conn.sendline(command)
time.sleep(0.02)
if command.find("exit") != -1:
return self.ssh_conn.before
if sleep:
time.sleep(20)
if not self.ssh_conn.prompt(timeout=1):
while self.ssh_conn.prompt(timeout=1) is not True and prompt_repeat < retries:
if (self.ssh_conn.buffer != None):
tmpbuf = tmpbuf + self.ssh_conn.buffer
self.ssh_conn.buffer = ''
prompt_repeat += 1
if prompt_repeat == retries:
print("Failed to detect prompt %s for %s times" % (self.ssh_conn.PROMPT, retries))
print(tmpbuf)
if killApptest:
print("=" * 100)
print(
"Failed to detect prompt %s for %s times for %s cmd" % (self.ssh_conn.PROMPT, retries, command))
print("=" * 100)
cleanup()
sys.exit()
return False
else:
#print(prompt_repeat)
#print("return from else: prompt_repeat == retries")
return tmpbuf
else:
# if(debug == "ON"):
tmpbuf = self.ssh_conn.before
if allowPrint:
print(tmpbuf)
return tmpbuf
def close(self):
try:
self.ssh_conn.close()
print("connection to %s closed" % self.hostname)
except Exception as err:
print(err)
def usage():
print("python test.py <ip> <username> <passwd> <xdk_path> <type> <testname or command> <enableLog>")
print(" command = ls / ... (shell command)")
print(" type = input is testname or command, 0 = testname, 1 = command")
print(" enableLog = 0 or 1")
cleanup()
sys.exit()
def main():
argmnts = sys.argv[1:] # ip username passwd xdk_path type testname/command enableLog
if len(argmnts) == 0:
usage()
elif argmnts[0] in "help":
usage()
elif len(argmnts) < 7:
usage
else:
global testName
global testType
global pathToXdk
global tmpbuf
global enableLog
global command
LOGS_DIR = os.path.join("TestResults")
print("Run results location: {}".format(os.path.abspath(LOGS_DIR)))
print(argmnts)
ip = argmnts[0]
username = argmnts[1]
passwd = argmnts[2]
connection = SshConnection(ip, username, passwd)
pathToXdk = argmnts[3]
testType = argmnts[4]
if argmnts[4] == '0':
testName = argmnts[5]
print("type is 0")
elif argmnts[4] == '1':
command = str(argmnts[5])
print("type is 1")
if argmnts[6] == '0':
enableLog = False
print("enableLog is False")
elif argmnts[6] == '1':
enableLog = True
print("enableLog is True")
print("command: " + command)
connection.cmd(command, enableLog)
# print(connection.cmd(command, enableLog))
def cleanup():
# LOGS_DIR = os.path.join("TestResults")
# CMD = "chmod -R a+rwx " + LOGS_DIR
# os.system(CMD)
return
if __name__ == "__main__":
try:
main()
cleanup()
except:
cleanup()