forked from yep/CHIP-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.sh
executable file
·118 lines (100 loc) · 2.94 KB
/
verify.sh
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
#!/usr/bin/env python
import io
import sys
import serial
import re
import time
#------------------------------------------------------------------
def answer_prompt(sio,prompt_to_wait_for,answer_to_write,send_cr=True):
#------------------------------------------------------------------
sio.flush()
prompt_found = False
data = ''
#if send_cr:
#sio.write(unicode('\n'))
d='something'
while not len(d)==0:
d = sio.read(2000);
data += d
time.sleep(1)
# print '-' * 50
# print ' %d bytes read' % (len(data))
# print '-' * 50
#print data
line=''
while not prompt_found:
d = sio.read(100);
data += d
# print '-' * 50
# print ' %d bytes read' % (len(data))
# print '-' * 50
# print data
# print '-' * 50
if len(data.split())>0:
line=data.split()[-1]
# print "matching [%s] against [%s]" % (line,prompt_to_wait_for)
if(re.match(prompt_to_wait_for,line,re.M)):
sio.write(unicode(answer_to_write+'\n'))
# print '-' * 50
# print ' detected [%s] ' % prompt_to_wait_for
# print '-' * 50
prompt_found = True
else:
if send_cr:
sio.write(unicode('\n'))
sio.flush()
#sys.stdin.readline()
#------------------------------------------------------------------
def scanfor(sio,regexp_to_scan_for,answer_to_write):
#------------------------------------------------------------------
prompt_found = False
data = ''
while not prompt_found:
data += sio.read(100);
# print '-' * 50
# print ' %d bytes read' % (len(data))
# print '-' * 50
# print data
if re.search(regexp_to_scan_for,data):
# print '-' * 50
# print ' detected [%s] ' % regexp_to_scan_for
# print '-' * 50
sio.write(unicode(answer_to_write+'\n'))
prompt_found = True
sio.flush()
return data
#------------------------------------------------------------------
def main():
#------------------------------------------------------------------
if( len(sys.argv)>1 ):
serial_port=sys.argv[1]
else:
serial_port='/dev/ttyACM0';
#print 'reading from %s:' % serial_port
ser = serial.Serial(serial_port,115200, timeout=1);
sio = io.TextIOWrapper(io.BufferedRWPair(ser,ser))
#login
print "login...",
sys.stdout.flush()
answer_prompt(sio,'.*login:','chip',True)
print "OK\npassword...",
sys.stdout.flush()
answer_prompt(sio,'.*Password:','chip',False)
print "OK\npoweroff...",
sys.stdout.flush()
answer_prompt(sio,'.*[\$#]','sudo poweroff')
answer_prompt(sio,'.*:','chip')
time.sleep(2)
print "OK\n",
#d=scanfor(sio,r'.*### [^#]+ ###.*','poweroff')
# if re.search(r'.*### ALL TESTS PASSED ###.*',d):
# print "---> TESTS PASSED"
# ser.close();
# return 0
ser.close();
# print "---> TESTS FAILED"
return 1
#------------------------------------------------------------------
if __name__ == "__main__":
#------------------------------------------------------------------
exit( main() )