-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsfos-ssh-connect.env
286 lines (254 loc) · 8.93 KB
/
sfos-ssh-connect.env
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
#!/bin/sh
################################################################################
#
# Copyright (C) 2023, Roberto A. Foglietta <[email protected]>
# Released under GPLv2 license terms
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
################################################################################
#
# INSTALLATION:
#
# save this file in /usr/bin/sfos-ssh-connect.env as text file not as a script
#
# add this line to the end of your ~/.bashrc for the future bash instances:
#
# source /usr/bin/sfos-ssh-connect.env
#
# and / or manually load into the current bash instance in the same way above.
#
# USAGE:
#
# tfish [command] - to use recovery telnet via USB on its default IPv4
# rfish [command] - to use recovery SSH via USB on its default IPv4
# ufish [command] - to use SSH via USB connection on its default IPv4
# wfish [command] - to use SSH via WiFi connection on its default IPv4
# afish [command] - to use the fast route IPv4 for SSH, updates IPv4 default
# sfish [command] - to use the previous route for SSH or it finds the fastest
#
# scp support:
#
# s/afish [scp] -u $remote-dest-path $local-files - secure copy in upload
# s/afish [scp] -d $local-dest-path $remote-files - secure copy in download
#
# extras:
#
# afish getip - set the fastest route IPv4 for establishing the SSH connection
# ufish tether - enable the tethering via USB when SFOS is in developer mode
#
################################################################################
# release: 0.1.8
export sfos_ipaddr=""
export ssh_opts="-4 -o ConnectTimeout=5 -o StrictHostKeyChecking=no"
export ssh_opts_recv="-o UserKnownHostsFile=/dev/null"
export sfos_ip_addr_u=${sfos_ip_addr_u:-192.168.2.15}
export sfos_ip_addr_t=${sfos_ip_addr_t:-172.28.172.1}
export sfos_ip_addr_r=${sfos_ip_addr_r:-10.42.66.66}
set -o allexport 2>/dev/null ||:
################################################################################
up() {
local pz="pigz -4ric";
$pz $1 | base64 | sfish "base64 -d | $pz -d >${2:+$2/}$(basename $1)";
}
upm() { local i dir=$1; shift; for i in "$@"; do up $i $dir; done; }
rsetclock() { afish "TZ=UTC date -s @$(TZ=UTC date +%s)"; }
cmdtrn() {
local xcmd
test -n "${1-:}" || return 1
if [ "$1" = "scp" ]; then
xcmd="$1 $ssh_opts -rp"
else
xcmd="$1 $ssh_opts -t"
fi
if [ "$sfos_ipaddr" = "$sfos_ip_addr_r" ]; then
xcmd="TERM=linux sshpass -p recovery $xcmd $ssh_opts_recv"
fi
shift
$xcmd "$@"
}
ufish() {
if [ "${1:-}" = "tether" ]; then
ufish_tether
return $?
fi
sfos_ipaddr=$sfos_ip_addr_u sfish "$@";
}
wfish() { sfos_ipaddr=$sfos_ip_addr_t sfish "$@"; }
rfish() { sfos_ipaddr=$sfos_ip_addr_r sfish "$@"; }
tfish() { TERM=linux telnet $sfos_ip_addr_r "$@"; }
sfishscp() {
if [ "x${1:-}" = "x-u" ]; then
shift
local rdest=$1
shift
cmdtrn scp "$@" root@$sfos_ipaddr:$rdest
elif [ "x${1:-}" = "x-d" ]; then
shift
local ldest=$1
shift
cmdtrn scp root@$sfos_ipaddr:"$@" "$ldest"
else
errecho "USAGE:"\
"afish scp <-u|-d> <r|ldest> <l|rfiles>\n"\
" afish scp -u <rdest> <lfiles>\n"\
" afish scp -d <ldest> <rfiles>"
fi
}
_get_check_cmd() {
local cmd=""
printf "sfos_ipaddr: auto-detection\n\\_using cmd: " >&2
if which timeout >/dev/null && which telnet; then
# telnet 0m0.050s (0m0.105s) is faster than culr
cmd='timeout 1 telnet $ip 22 2>/dev/null | grep -q "to $ip"'
elif which curl; then
# curl 0m0.095s (0m0.197s) is faster than netcat
curlcmd='curl --connect-timeout 1 -sv'
cmd=$curlcmd' telnet://$ip:22 2>&1 | grep -q "to $ip"'
elif which nc; then
# netcat 0m0.111s (0m0.200s) is faster than wget
cmd='nc -w 1 $ip 22 | grep -qi openssh'
elif which wget; then
# wget 0m0.122s (0m0.211s) is way faster than nmap
cmd='wget -t1 -T1 $ip:22 2>&1 | grep -q " $ip"'
# RAF: to slooow sometimws, 13s
# elif which nmap; then
# # nmap 0m0.256s (0m0.326s) is faster than ssh
# cmd='nmap --host-timeout 1 $ip -PN -p 22 | grep -q open'
elif which ssh; then
# ssh 0m0.541s (0m0.498s) is the slowest command
local cmd_opts="-o PasswordAuthentication=no -o ConnectTimeout=1"
cmd_opts="$cmd_opts -o StrictHostKeyChecking=no -o BatchMode=yes"
cmd='cmdtrn ssh -qn '$cmd_opts' root@$ip echo root | grep -q "^root"'
fi >&2
echo "$cmd &&"
}
_runinbg() {(set +e
echo quit | {
eval exec -a $cmd_tag "$@" echo $ip >$fifo_name &
}
disown &>/dev/null ||:
)}
afish_getip() {
local cmd="$(_get_check_cmd)"
local ip fifo_name ipaddr="" cmd_tag="sshcontest"
local iplist="$sfos_ipaddr ${sfos_ip_addr_u}
${sfos_ip_addr_t} ${sfos_ip_addr_r}"
if [ ! -z "$cmd" ]; then
fifo_name=$(mktemp -up ${TMPDIR:-/tmp} fifo.XXXXXX)
if mkfifo "$fifo_name"; then
for ip in $iplist; do
_runinbg "$cmd"
done
ip="" _runinbg "sleep 3;"
read ipaddr < "$fifo_name"
{
pkill -f $cmd_tag && \
pkill -9 -f $cmd_tag ||:
} 2>/dev/null
rm -f "$fifo_name"
fi
fi
export sfos_ipaddr=$ipaddr
echo "sfos_ipaddr: ${ipaddr:-not found}" >&2
test -n "$ipaddr"
}
# RAF: this is the function alternative to afish_getip when set -m is not work.
# However, because the background jobs are not managed by the shell, but
# giving them a tag (thread name) to kill later, this function is just for
# the development history record.
#
_alt_afish_getip() {
echo -e "\nWARNING: please, use afish_getip() instead.\n"
return 1
local ip fifo_name ipaddr="" cmd_tag="sshcontest" cmd="$(_get_check_cmd)"
local iplist="$sfos_ipaddr ${sfos_ip_addr_u}
${sfos_ip_addr_t} ${sfos_ip_addr_r}"
if [ ! -z "$cmd" ]; then
for ip in $iplist; do
echo quit | eval $cmd && break
ip=""
done
fi
sfos_ipaddr=$ip
echo sfos_ipaddr: ${ip:-not found} >&2
test -n "$ip"
}
sfish() {
if [ -n "${1:-}" ]; then
test "x${1/d/u}" = "x-u" && { sfishscp "$@"; return $?; }
test "$1" = "scp" && { shift; sfishscp "$@"; return $?; }
fi
cmdtrn ssh root@$sfos_ipaddr "$@"
}
afish() {
afish_getip || return 1
test "${1:-}" = "getip" -o "x${1:-}" = "x--ip" && return 0
sfish "$@"
}
ufish_tether() {
echo "devel USB tethering on/off switch"
local scrname='$HOME/bin/setnet_postroute.sh'
if ! ufish "$scrname -s"; then
echo
echo "ERROR: $scrname failed, abort."
echo
echo "Run rfos-suite-installer.sh, before?"
echo
return 1
fi >&2
local cmd ip="${sfos_ip_addr_u}" routecmd='sudo route $cmd default gw $ip'
if route -n | grep -qe "^[0-9.]* *$ip .* UG "; then
cmd="del"
else
cmd="add"
fi
eval echo "'=>' $routecmd" && eval sudo $routecmd
echo; route -n | sed -e "s/^/ /"; echo
return $?
}
ask_to_stop_mobile_data() {
local ret tmpfile=$(mktemp -p ${TMPDIR:-/tmp} -t ask2stmd.XXXXXX)
while true; do
echo
echo "=> Check mobile data connection on device..."
afish_getip 2>$tmpfile
sed -e "s/^/ /" $tmpfile ||:
rm -f $tmpfile
# This part cannot be interrupted # ************************************
stty -echoctl 2>/dev/null ||:; trap 'true' SIGINT
ret=0; cmdtrn ssh $ssh_opts root@$sfos_ipaddr ifconfig -a -s 2>&1 |\
grep -qe "rmnet_data[1-2] .*RU" || ret=$?
stty echoctl 2>/dev/null ||:; trap - SIGINT
# **********************************************************************
printf " \_ mobile data: "
if [ $ret -ne 0 ]; then
echo "none"
break
fi
echo "active"
echo
echo "WARNING: please, deactive mobile data on the samrtphone"
echo " restore should be use the USB connection, only"
echo
echo " Press ENTER to retry or CTRL-C to stop"
sleep 1
read
done
return 0
}
################################################################################
set +o allexport 2>/dev/null ||:
# RAF: this line is useful for creating an executable patch for Web Catalog
return $? 2>/dev/null ||: