Files
bash_utils/devices.sh
2024-03-22 07:02:10 +01:00

275 lines
9.1 KiB
Bash

#!/bin/bash
utils="/home/spezi/Scripts/utils"
. $utils/logger.sh
. $utils/mad-instances.sh
. $utils/math.sh
location_an=""; for d in mini0{1..5} mini0{7..9} miniwue{01..15} ya01 ip01 ip02; do location_an="$location_an $d"; done
location_rot=""; for d in rot{01..12} stack{1..2}{01..12} stack3{01..06}; do location_rot="$location_rot $d"; done
#location_dacA=""; for d in dac01 dac02 dac{05..07}; do location_dacA="$location_dacA $d"; done
#location_dacB=""; for d in dac03 dac04 dac{08..10}; do location_dacB="$location_dacB $d"; done
location_dacA=""; for i in {01..10}; do location_dacA="$location_dacA dac$i"; done
location_mer="mer01"
location_mes="mes01 mes02 mes04"
devicelocations=("an rot dacA mer")
# mes")
# moved to one location early 2023
# dacB
DEVICES_TIMEOUT=5
getdeviceinstance () {
grep "location /$1" $nginxconf -A5 | grep proxy_pass | cut -d / -f3 | sed s/rgc// | sed s/pd// | head -n1
}
getdevicelocation () {
for loc in $devicelocations; do
locname="location_$loc"
content=${!locname}
# if [[ "$1" == *"$content"* ]]; then
if [[ "$content" =~ $1 ]]; then
echo $loc
return 0
fi
done
echo "None"
return 1
}
getdeviceip () {
device=$1
ip=$(cat $maddir/files/$1.ips | awk -F "," '{print $1}')
echo $ip
}
get_working_device_from_location () {
location=$1
locname="location_$location"
arr=${!locname}
for device in $arr; do
res=$(cs_result $device passthrough true)
if [[ "$res" == "OK" ]]; then
echo $device
break
fi
done
}
cs_send () {
device=$1
csport=$(getcsport $(getdeviceinstance $device))
shift
if cs_send_result=$(echo "{\"device\": \"$device\", \"command\": \"$@\"}" | timeout $DEVICES_TIMEOUT nc localhost $csport); then
cs_send_result=$(echo $cs_send_result | jq -r .result 2>/dev/null)
else
cs_send_result="None"
fi
if [[ $cs_send_result == "None" ]] || [[ $cs_send_result == "null" ]]; then
# logger "cs_send result from command <$@> is: $cs_send_result!"
echo 1
return 1
fi
}
cs_result () {
device=$1
csport=$(getcsport $(getdeviceinstance $device))
shift
if result=$(echo "{\"device\": \"$device\", \"command\": \"$@\"}" | timeout $DEVICES_TIMEOUT nc localhost $csport); then
result=$(echo $result | jq -r .result 2>/dev/null)
else
result="None"
fi
if [[ $result == "None" ]] || [[ $result == "null" ]]; then
# logger "cs_result result from command <$@> is: $result!"
echo 1
return 1
fi
echo $result
}
ptcstatus () {
cs_result $1 "passthrough curl -s -k -I https://sso.pokemon.com/sso/login -o /dev/null -w '%{http_code}'" | sed "s/\[//" | sed "s/\]//"
}
deviceip () {
cs_result $1 "passthrough curl -k -s https://ifconfig.me" | sed "s/\[//" | sed "s/\]//"
}
desktop_not_online () {
device=$(get_working_device_from_location an)
if [[ -z "$device" ]]; then
# no device found
return 1
fi
unreachable=$(cs_result $device "passthrough ping -c1 192.168.1.21 | grep -c Unreachable" | sed "s/\[//" | sed "s/\]//")
if [[ "$unreachable" -eq 0 ]]; then
return 1
else
return 0
fi
}
reconnect_rot () {
# fuck this escaping nightmare ...
device=$(get_working_device_from_location rot)
if [[ -z "$device" ]]; then
echo "no device found"
return 1
fi
xml="<?xml version='1.0' encoding='utf-8'?><s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><u:ForceTermination xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /></s:Body></s:Envelope>"
cmd='curl \"http://192.168.178.1:49000/igdupnp/control/WANIPConn1\" -H \"Content-Type: text/xml; charset=\"\"utf-8\"\"\" -H \"SoapAction: \"\"urn:schemas-upnp-org:service:WANIPConnection:1#ForceTermination\"\"\"'
cmd+=' -d \"'
cmd+=$xml
cmd+='\"'
echo "cmd to $device: $cmd"
cs_result $device "passthrough $cmd"
}
reconnect_an () {
if ! [[ "$1" == "force" ]] && ! desktop_not_online; then
# do not reconnect if my desktop is online :-) do not fuck up my home office ...
echo "nope! desktop is online"
return 1
fi
device=$(get_working_device_from_location an)
if [[ -z "$device" ]]; then
echo "no device found"
return 1
fi
# fuck this escaping nightmare ...
xml="<?xml version='1.0' encoding='utf-8'?><s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><u:ForceTermination xmlns:u='urn:schemas-upnp-org:service:WANIPConnection:1' /></s:Body></s:Envelope>"
cmd='curl \"http://192.168.1.1:49000/igdupnp/control/WANIPConn1\" -H \"Content-Type: text/xml; charset=\"\"utf-8\"\"\" -H \"SoapAction: \"\"urn:schemas-upnp-org:service:WANIPConnection:1#ForceTermination\"\"\"'
cmd+=' -d \"'
cmd+=$xml
cmd+='\"'
echo "cmd to $device: $cmd"
cs_result $device "passthrough $cmd"
}
reboot_rot () {
# another escaping nightmare
# https://knx-user-forum.de/forum/öffentlicher-bereich/knx-eib-forum/1498696-fritzbox-reboot-mit-curl?p=1498729#post1498729
# auth needed ! not working as-is
xml="<?xml version='1.0' encoding='utf-8'?><s:Envelope s:encodingStyle='http://schemas.xmlsoap.org/soap/encoding/' xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'><s:Body><u:Reboot xmlns:u='urn:dslforum-org:service:DeviceConfig:1'></u:Reboot></s:Body></s:Envelope>"
cmd='curl \"http://192.168.178.1:49000/upnp/control/deviceconfig\" -H \"Content-Type: text/xml; charset=\"utf-8\"\" -H \"SoapAction:urn:dslforum-org:service:DeviceConfig:1#Reboot\"'
cmd+=' -d \"'
cmd+=$xml
cmd+='\"'
echo "cmd to $1: $cmd"
cs_result $1 "passthrough $cmd"
}
devicetemp () {
res=$(cs_result $1 'passthrough cat /sys/class/thermal/thermal_zone0/temp' | sed "s/\[//" | sed "s/\]//")
if ! [ -z "$res" ] && [ "$res" -eq "$res" ] 2>/dev/null && [ "$res" -ne 1 ]; then
res=$(echo $res | sed "s/\[//" | sed "s/\]//")
res=$(divide $res 1000)
echo "$1: $res °C"
else
echo "$1: ?"
fi
}
ptccheck () {
if [[ $1 == "debug" ]]; then
debuglog="echo DEBUG: $@"
else
debuglog=":"
fi
for loc in ${devicelocations[@]}; do
$debuglog "check $loc"
ip=""
locname="location_$loc"
arr=(${!locname})
for device in ${arr[@]}; do
$debuglog "try $device"
if [ -z "$ip" ]; then
ip=$(deviceip $device)
fi
result=$(ptcstatus $device)
if [[ "$result" == 1 ]] || [ -z "$ip" ] || [ -z "$result" ]; then
continue
elif [[ "$result" == "200" ]]; then
echo "$loc @ $ip: 200"
break
elif [[ "$result" == "403" ]]; then
echo "$loc @ $ip: 403"
break
fi
echo "$loc @ $ip: $result"
done
done
}
tempcheck () {
for loc in ${devicelocations[@]}; do
locname="location_$loc"
arr=(${!locname})
for device in ${arr[@]}; do
devicetemp $device
done
done
}
cs_result_all () {
for loc in ${devicelocations[@]}; do
locname="location_$loc"
arr=(${!locname})
for device in ${arr[@]}; do
res=$(cs_result $device "$@")
echo $res
done
done
}
tunnel_to_device () {
device=$1
ip=$(getdeviceip $device)
loc=$(getdevicelocation $device)
working=$(get_working_device_from_location $loc)
if ! [[ -z "$working" ]]; then
tunnel="passthrough pkill shh; curl -s -k -L https://pogoan.de/dropbearmulti -o /data/local/tmp/dropbearmulti; curl -s -k -L https://pogoan.de/bearbox -o /data/local/tmp/bearbox; chmod 600 /data/local/tmp/bearbox; chmod +x /data/local/tmp/dropbearmulti; ln -s /data/local/tmp/dropbearmulti /data/local/tmp/ssh; /data/local/tmp/ssh -f -N -T -R 12345:$ip:5555 madbox@45.132.245.143 -p 2311 -i /data/local/tmp/bearbox -y"
res=$(cs_result $working "$tunnel")
echo "$working:$device"
else
return 1
fi
}
tunnel_on_device () {
device=$1
tunnel="passthrough pkill shh; curl -s -k -L https://pogoan.de/dropbearmulti -o /data/local/tmp/dropbearmulti; curl -s -k -L https://pogoan.de/bearbox -o /data/local/tmp/bearbox; chmod 600 /data/local/tmp/bearbox; chmod +x /data/local/tmp/dropbearmulti; ln -s /data/local/tmp/dropbearmulti /data/local/tmp/ssh; /data/local/tmp/ssh -f -N -T -R 12345:127.0.0.1:5555 madbox@45.132.245.143 -p 2311 -i /data/local/tmp/bearbox -y"
res=$(cs_result $device "$tunnel")
echo $res
}
ping_device () {
device=$1
ip=$(getdeviceip $device)
loc=$(getdevicelocation $device)
working=$(get_working_device_from_location $loc)
if ! [[ -z "$working" ]]; then
echo "use $working to ping $device@$ip at $loc"
res=$(cs_result $working passthrough ping -c1 $ip)
echo $res
else
return 1
fi
}
kill_tunnel () {
device=$1
res=$(cs_result $device "passthrough pkill ssh")
echo $res
}