-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdbShellAdapter.py
More file actions
149 lines (116 loc) · 4.94 KB
/
Copy pathAdbShellAdapter.py
File metadata and controls
149 lines (116 loc) · 4.94 KB
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 ipaddress
import subprocess
from Log import log_d, log_e
class AdbShellAdapter():
def disconnect_all(self):
try:
command_string = 'adb disconnect'
command_string_list = command_string.split(" ")
adb_output = subprocess.check_output(command_string_list)
adb_output_string = adb_output.decode("utf-8")
log_d(adb_output_string)
except subprocess.CalledProcessError as e:
log_e(str(e))
def adb_tcpip(self, device_id, port):
try:
command_string = 'adb -s {0} tcpip {1}'.format(device_id, port)
command_string_list = command_string.split(" ")
adb_output = subprocess.check_output(command_string_list)
adb_output_string = adb_output.decode("utf-8")
log_d(adb_output_string)
except subprocess.CalledProcessError as e:
log_e(str(e))
def get_device_wifi_ip(self, device_id):
ip_address = ""
try:
command = 'adb -s {0} shell ip addr show wlan0 | grep inet'.format(device_id)
command_list = command.split(" ")
adb_output = subprocess.check_output(command_list)
adb_output_string = adb_output.decode("utf-8")
adb_output_string_list = adb_output_string.split("\n")
inet_info_list = adb_output_string_list[0].lstrip().split(" ")
ip_address = inet_info_list[1].split("/")[0]
except subprocess.CalledProcessError as e:
log_e(str(e))
log_d('defice ip: {0}'.format(ip_address))
return ip_address
def connect_device_via_wifi(self, device_id, ip, port):
try:
ip_port = "{0}:{1}".format(ip, port)
command = 'adb -s {0} connect {1}'.format(device_id, ip_port)
command_list = command.split(" ")
adb_output = subprocess.check_output(command_list)
adb_output_string = adb_output.decode("utf-8")
adb_output_string_list = adb_output_string.split(" ")
result = adb_output_string_list[0]
if result == 'connected':
return True
except subprocess.CalledProcessError as e:
print(e)
return False
def adb_devices(self):
attached_device_list = []
try:
command = 'adb devices'
command_list = command.split(" ")
adb_output = subprocess.check_output(command_list)
adb_output_string = adb_output.decode("utf-8")
adb_output_string_list = adb_output_string.split("\n")
for i, val in enumerate(adb_output_string_list):
if i > 0 and len(val) > 0:
attached_device = val.split("\t")[0]
attached_device_list.append(attached_device)
except subprocess.CalledProcessError as e:
print(e)
return attached_device_list
def adb_push(self, device_id, src, des):
try:
command = 'adb -s {0} push {1} {2}'.format(device_id, src, des)
command_list = command.split(" ")
adb_output = subprocess.check_output(command_list)
adb_output_string = adb_output.decode("utf-8")
if '100%' in adb_output_string:
log_d("Successfully push {0}!".format(des))
return True
else:
return False
except subprocess.CalledProcessError as e:
print(e)
return False
def adb_install(self, device_id, path_to_device_apk):
try:
command = 'adb -s {0} shell pm install -r {1}'.format(device_id, path_to_device_apk)
command_list = command.split(" ")
adb_output = subprocess.check_output(command_list)
adb_output_string = adb_output.decode("utf-8")
if 'Success' in adb_output_string:
log_d("Successfully install {0}!".format(path_to_device_apk))
return True
else:
return False
except subprocess.CalledProcessError as e:
log_e(str(e))
return False
def adb_uninstall(self, device_id, pkg_name):
try:
command = 'adb -s {0} uninstall {1}'.format(device_id, pkg_name)
command_list = command.split(" ")
adb_output = subprocess.check_output(command_list)
adb_output_string = adb_output.decode("utf-8")
except subprocess.CalledProcessError as e:
log_e(str(e))
return False
def adb_execute(self, command):
try:
command_list = command.split(" ")
adb_output = subprocess.check_output(command_list)
adb_output_string = adb_output.decode("utf-8")
return adb_output_string
except subprocess.CalledProcessError as e:
print(e)
return None
def main():
adbShellAdapter = AdbShellAdapter()
adbShellAdapter.adb_devices()
if __name__ == '__main__':
main()