forked from AdrianDC/advanced_development_shell_tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
android_adb_tools.rc
349 lines (292 loc) · 7.95 KB
/
android_adb_tools.rc
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/bin/bash
#
# Copyright 2015-2017 Adrian DC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# === Standalone Source Helper ===
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_linux.rc)
# source <(curl -Ls https://github.com/AdrianDC/android_development_shell_tools/raw/master/android_adb_tools.rc)
# === ADB Screenshot ===
function adbscr()
{
# Usage: adbscr (Take a screenshot from connected device)
# Take a screenshot from connected device
mkdir -p ./screenshots;
adb shell screencap /sdcard/screenshot.png;
adb pull /sdcard/screenshot.png "./screenshots/screenshot-$(date +%Y%m%d-%H%M%S).png";
adb shell rm -f /sdcard/screenshot.png;
}
# === ADB Root Mounter ===
function adbr()
{
# Usage: adbr (adb root and remount rw system)
# Variables
local adb_timeout;
# Get root access and rw system
adb_timeout="timeout 5 $(adbcmd)";
adbwait;
${adb_timeout} 'root';
sleep 2;
adbwait;
${adb_timeout} 'remount';
sleep 1;
adbwait;
}
# === ADB Root Checked Mounter ===
function adbro()
{
# Usage: adbro (Verified adb root and remount rw system)
# Variables
local adb_cmd;
local adb_timeout;
local rootaccess;
local systemaccess;
local systemmount;
# Prepare adb access
adb_cmd=$(adbcmd);
adb_timeout="timeout 5 ${adb_cmd}";
# Verify faulty adb server
if ${adb_cmd} shell 'echo -n "";' 2>&1 | grep -q 'protocol fault'; then
# Kill current running adb server
${adb_timeout} kill-server;
sudo killall 'adb' 2>/dev/null;
fi;
# Verify sudo requirements
if ${adb_cmd} shell 'echo -n "";' 2>&1 | grep -q 'insufficient permissions\|no permissions'; then
# Run adbsudo if permission denied
adbsudo;
adb_timeout="timeout 5 $(adbcmd)";
fi;
# Get root access
rootaccess=$(${adb_timeout} shell whoami);
if [ "${rootaccess:0:4}" != 'root' ] && [ "${rootaccess:16:5}" != 'uid 0' ]; then
adbwait;
${adb_timeout} 'root';
sleep 2;
adbwait;
fi;
# Get rw system
systemmount=$(${adb_timeout} shell mount | grep '/system ');
systemaccess=$(echo "${systemmount}" | grep rw);
if [ ! -z "${systemmount}" ] && [ -z "${systemaccess}" ]; then
adbwait;
${adb_timeout} 'remount';
sleep 1;
adbwait;
fi;
}
# === ADB Root Access ===
alias adbroot='adb kill-server; sudo adb start-server; sudo adb root; sudo adb wait-for-device; sudo adb remount';
# === ADB Sudo Switcher ===
function adbsudo()
{
# Usage: adbsudo (ADB sudo toggle helper)
# Enable adb sudo
if ! alias adb &> /dev/null; then
alias adb='sudo adb';
sudo echo -n '';
# Disable adb sudo
else
unalias adb;
fi;
# Kill adb server
timeout 5 sudo adb kill-server;
sudo killall 'adb' 2>/dev/null;
# Reload scripts
shtoolsup &> /dev/null;
}
# === ADB Command ===
function adbcmd()
{
# Usage: adbcmd (Return adb command based on alias)
# Use regular adb
if ! alias adb &> /dev/null; then
echo 'adb';
# Use aliased adb
else
alias adb | sed "s/^alias adb='\(.*\)'$/\1/";
fi;
}
# === ADB Root Stock ===
function adbrstock()
{
# Usage: adbrstock (Stock ROM adb root access)
# Stock ROM adb root access
adb shell su -c 'setprop ro.secure 0; setprop ro.debuggable 1; setprop persist.service.adb.enable 1';
adb shell getprop ro.secure;
}
# === ADB Wait For Device ===
function adbwait()
{
# Usage: adbwait [delay_secs] (adb wait for device)
# Variables
local adb_cmd;
local delay=${1:-10};
local time_start;
# Initialize variables
adb_cmd=$(adbcmd);
time_start=$(date +%s);
# Wait for device until available
while [ $(($(date +%s) - time_start)) -le "${delay}" ] &&
! ${adb_cmd} devices | grep -v 'List of devices attached' | grep -q 'device\|recovery\|sideload'; do
true;
done;
}
# === ADB Shell Root Without Remounts ===
function adbsu()
{
# Usage
if [ -z "${1}" ]; then
>&2 echo '';
>&2 echo ' Usage: adbsu <command...> (Run on root adb shell without remounts)';
>&2 echo '';
return;
fi;
# Variables
local cmd=${*};
# Run as adb root shell
if adb shell type su 2>&1 | grep -q 'not found' ||
adb shell 'su -c echo "" || echo "su: invalid";' 2>&1 | grep -q 'su: invalid'; then
adb root >/dev/null 2>&1;
>&2 adbwait;
adb shell "${cmd}";
# Run on root adb shell
else
adb shell "su -c '${cmd}'";
fi;
}
# === ADB Shell Root With Remounts ===
function adbsur()
{
# Usage
if [ -z "${1}" ]; then
echo '';
echo ' Usage: adbsur <command...> (Run on root adb shell with remounts)';
echo '';
return;
fi;
# Variables
local cmd=${*};
# Run as adb root shell
>&2 adb shell 'echo -n "";';
if adb shell type su 2>&1 | grep -q 'not found' ||
adb shell 'su -c echo "";' 2>&1 | grep -q 'su: invalid'; then
>&2 adbro;
adb shell "${cmd}";
# Run on root adb shell
else
if ! adb shell 'su -c mount' 2>&1 | grep '/system ' | grep -q rw; then
>&2 adb shell "su -c 'mount -o rw,remount /system'";
fi;
adb shell "su -c '${cmd}'";
fi;
}
# === ADB IP Connector ===
function adbco()
{
# Relaunch handling
echo '';
if [ ! -z "${1}" ]; then
export ADB_IP_ADDRESS=${1};
fi;
# Usage
if [ -z "${ADB_IP_ADDRESS}" ]; then
echo '';
echo ' Usage: adbco [ipaddress_once] (Helper for adb network access)';
echo '';
return;
fi;
# Variables
local adb_timeout;
# Connect and acquire adb root access
adb_timeout="timeout 5 $(adbcmd)";
export ADB_IP_PORT=5555;
echo -e "\e[1;36mConnecting to ${ADB_IP_ADDRESS}:${ADB_IP_PORT}...\e[0m";
${adb_timeout} connect "${ADB_IP_ADDRESS}:${ADB_IP_PORT}";
echo -e '\e[1;36mConnected...\e[0m';
adbwait 20;
echo -e '\e[1;36mGetting root access...\e[0m';
${adb_timeout} root;
sleep 2;
echo -e '\e[1;36mReconnecting...\e[0m';
${adb_timeout} connect "${ADB_IP_ADDRESS}:${ADB_IP_PORT}";
echo -e '\e[1;36mConnected...\e[0m';
adbwait 20;
echo -e '\e[1;36mMounting as root...\e[0m';
${adb_timeout} remount;
sleep 1;
adbwait 20;
echo -e '\e[1;36mReady...\e[0m';
echo '';
}
# === ADB File Editor ===
function adbedit()
{
# Usage: adbedit [file_path] (Edit adb file, default on /system/build.prop)
# Variables
local name;
local path;
# Initialize variables
path=${1:-/system/build.prop};
name=$(basename "${path}");
# Pull file as root
adbro;
rm -f "${name}";
adb pull "${path}";
# Check local file permissions
if [ ! -r "${name}" ] || [ ! -w "${name}" ]; then
sudo chown "$(whoami):$(whoami)" "${name}";
chmod 666 "${name}";
fi;
# Edit local file
fileedit "${name}";
echo -n ' Ok ? [ENTER] ';
read -r;
# Push back file to device
adb push "${name}" "${path}";
rm "${name}";
}
# === ADB Manifest.xml Editor ===
function adbeditmanifest()
{
# Usage: adbeditmanifest (Edit adb /system/vendor/manifest.xml file)
# Edit adb /system/vendor/manifest.xml file
adbedit /system/vendor/manifest.xml;
}
# === ADB Inputs Reader ===
function adbinputs()
{
# Usage: adbinputs (Dump all input devices)
#grep . /sys/class/input/event*/device/name
#adbs hexdump -C /dev/input/event7
command='cat /proc/bus/input/devices';
echo '';
echo "adb shell ${command}";
echo '';
adb shell "${command}";
}
# === ADB Events Reader ===
function adbreadevents()
{
# Usage
if [ -z "${1}" ]; then
echo '';
echo ' Usage: adbreadevents <event_number> (Read input events)';
echo '';
return;
fi;
# Read input events
adb shell od -x "/dev/input/${1}" \
| awk -Wposix '{ if ($7) printf(" Key %4d - State %4d\n", "0x" $7, "0x" $8) }';
}