-
Notifications
You must be signed in to change notification settings - Fork 1
/
shell_test.py
89 lines (65 loc) · 1.97 KB
/
shell_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
from atx.Shell import *
import time
# 我伪装成一个真实shell,不管你发什么,我都只返回时间
class ShellSimulator:
def send(self, cmd=''):
return time.ctime()
class TelnetAccessShell(ShellAccess):
"""
我会通过telnet连接ShellSimulator,执行任务
"""
def __init__(self):
self.__conn = ShellSimulator()
def open(self, address):
print("telnet open at ", address)
return True
def close(self):
print("telnet close")
return True
def send(self, cmd, exp_list, timeout):
print('Telnet send, recv:', self.__conn.send())
return 0, None, ''
class ComAccessShell(ShellAccess):
"""
我会通过串口连接ShellSimulator,执行任务
"""
def __init__(self):
self.__conn = ShellSimulator()
def open(self, address):
print("COM open at ", address)
return True
def close(self):
print("COM close")
return True
def send(self, cmd, exp_list, timeout):
print('COM send, recv:', self.__conn.send())
return 0, None, ''
class MyShell(Shell):
"""
我是一个具体的shell
可以通过继承啦、组合啦合成一个复杂的shell
"""
def __init__(self, acc=None):
self._acc = None
Shell.__init__(self, acc)
def open(self, address):
return self._acc.open(address)
def close(self):
return self._acc.close()
def send(self, cmd, exp_list=None, timeout=5):
return self._acc.send(cmd, exp_list, timeout)
# 可以在这里面实现更多功能
# .......
class MyShellBuilder(ShellTypeBuilder):
def build(self, ac):
sh = MyShell(ac)
return sh
ShellCreator.add('myshell', MyShellBuilder)
mysh = ShellCreator.create('myshell', TelnetAccessShell)
mysh.open(('127.0.0.1', 1000))
mysh.send('abc')
mysh.close()
mysh = ShellCreator.create('myshell', ComAccessShell)
mysh.open(('COM1', 9600))
mysh.send('abc')
mysh.close()