Compare commits

...

2 Commits

Author SHA1 Message Date
7de4aa3e8e Merge branch 'main' 2024-03-22 07:02:43 +01:00
8baec552ac initial commit 2024-03-22 07:02:10 +01:00
13 changed files with 623 additions and 0 deletions

21
colors.sh Normal file
View File

@@ -0,0 +1,21 @@
#!/bin/bash
echoc() {
command echo -e "$@${NC}"
}
# colors!
# usage: ${RED}
# https://stackoverflow.com/questions/5947742/how-to-change-the-output-color-of-echo-in-linux
RED='\033[0;31m'
LRED='\033[1;31m'
YELLOW='\033[0;33m'
GREEN='\033[0;32m'
BLUE='\033[0;34m'
LBLUE='\033[0;36m'
GRAY='\033[1;30m'
GREY='\033[1;30m'
NC='\033[0m' # No Color
allcolors=("RED" "LRED" "YELLOW" "GREEN" "BLUE" "LBLUE" "GRAY" "NC")

274
devices.sh Normal file
View File

@@ -0,0 +1,274 @@
#!/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
}

32
logger.sh Normal file
View File

@@ -0,0 +1,32 @@
#!/bin/bash
. /home/spezi/Scripts/utils/colors.sh
logger () {
echo -e "${NC}$(date) - [$BASHPID] - [${0##*/}] - $@${NC}"
}
loggerseparator () {
echo -e "${NC}$(date) - [$BASHPID] - ====================================="
}
saferun () {
allcommands=""
for var in "$@"; do
allcommands="$allcommands$var /// "
done
logger "Enter to run the following command(s): $allcommands"
read ok
ret=0
for var in "$@"; do
if [[ $ret -eq 0 ]]; then
$var
ret=$?
else
logger "command $var aborted because previous command exited non-zero!"
fi
done
loggerseparator
logger "Command(s) done!"
loggerseparator
}

6
mad-dbs.sh Normal file
View File

@@ -0,0 +1,6 @@
#!/bin/bash
maddbs=("rocketmapdb" "wuedb" "dacdb" "kgdb" "hofdb" "parisdb" "ipdb" "ambdb")
pmsfdbs=("pmsfan" "pmsfwue" "pmsfdah" "pmsfkg" "pmsfhof" "pmsfip" "pmsdamb")
poracledbs=("poraclenew" "poraclewue")
shopdbs=("shopan" "shopdah")

58
mad-instances.sh Normal file
View File

@@ -0,0 +1,58 @@
#!/bin/bash
maddir="/home/spezi/git/MAD-asyncio"
madinstances=("mad" "mad-wue" "mad-dac" "mad-kg" "mad-hof" "mad-amb" "mad-ip")
# "mad-ip"
# "mad-paris")
madinstances_short=("an" "wue" "dac" "kg" "hof" "amb" "ip")
# "ip"
#"paris")
nginxconf="/etc/nginx/sites-enabled/pogoan.de"
getmadminport () {
grep madmin_port $maddir/configs/scanner_$1.ini | awk '{print $2}'
}
getcsport () {
grep "\[$1\]" $maddir/plugins/commandSocket/plugin.ini -A2 | grep port | cut -d = -f2 | xargs
}
function getlatestold () {
dir="$maddir/logs"
name=$1
# http://mywiki.wooledge.org/BashFAQ/003
unset -v latest
for file in "$dir"/*$name*; do
[[ $file -nt $latest ]] && latest=$file
done
echo $latest
}
function getlatest () {
# $1 = required: instance name
# $2 = optional: number of minutes to look back
dir="$maddir/logs"
name=$1
files=""
if ! [ -z $2 ]; then
i=$2
else
i=0
fi
while [ -z "${files}" ]; do
((i++))
# find all files that contain the supplied name and have bin edited within the given number of minutes - 0 means same minute
files=$(find $dir/ -mmin -$i -type f -name "*_$name*" | sort -r)
done
for file in ${files[@]}; do
# these loglines are characteristical for main start.py instances - filtering out mitmreceiver instances etc.
if grep -e "pluginBase" -e "aiting for data after" -e "screenPath" $file >/dev/null; then
latest=$file
echo $latest
break
fi
done
}

14
madmail.sh Normal file
View File

@@ -0,0 +1,14 @@
#!/bin/bash
mailer () {
if ! [[ -z "$3" ]]; then
mailfrom="From: $3 <mad@pogoan.de>"
fi
if ! [[ -z "$4" ]]; then
mailto="$4"
elif [[ -z "$mailto" ]]; then
mailto="christian@betz.cc"
fi
echo -e "$1" | mailx -s "$2" --append="$mailfrom" $mailto &>/dev/null
}

73
math.sh Normal file
View File

@@ -0,0 +1,73 @@
#!/bin/bash
DIRECTORY=$(cd `dirname $0` && pwd)
. /home/spezi/Scripts/utils/utils.sh
# https://unix.stackexchange.com/questions/66766/float-operation-with-bc
# https://unix.stackexchange.com/questions/89712/how-to-convert-floating-point-number-to-integer
# xargs: https://stackoverflow.com/questions/19148370/piping-seq-to-printf-for-number-formatting
divideint () {
bc <<< "scale=$scale; $1/$2" | xargs printf "%.0f\n"
}
multiint () {
bc <<< "scale=2; $1*$2" | xargs printf "%.0f\n"
}
divide () {
if [ -z $3 ]; then
scale=2
else
scale=$3
fi
bc <<< "scale=$scale; $1/$2"
}
multiply () {
if [ -z $3 ]; then
scale=2
else
scale=$3
fi
bc <<< "scale=$scale; $1*$2"
}
avgoftwo () {
sum=$(bc <<< "$1+$2")
divideint $sum 2
}
max () {
declare -a allnumbers
for arg in "$@"; do
if isarray $arg; then
for elem in ${arg[@]}; do
[[ $elem -eq $elem ]] && allnumbers+=($elem)
done
else
[[ $arg -eq $arg ]] && allnumbers+=($arg)
fi
done
max=${allnumbers[0]}
for n in "${allnumbers[@]}"; do
((n > max)) && max=$n
done
echo $max
}
min () {
declare -a allnumbers
for arg in "$@"; do
if isarray $arg; then
for elem in ${arg[@]}; do
[[ $elem -eq $elem ]] && allnumbers+=($elem)
done
else
[[ $arg -eq $arg ]] && allnumbers+=($arg)
fi
done
min=${allnumbers[0]}
for n in "${allnumbers[@]}"; do
((n < min)) && min=$n
done
echo $min
}

13
need-root.sh Normal file
View File

@@ -0,0 +1,13 @@
#!/bin/bash
if ! [ $(id -u) = 0 ]; then
echo "The script needs to be run as root." >&2
exit 1
fi
if [ $SUDO_USER ]; then
real_user=$SUDO_USER
else
real_user=$(whoami)
fi

94
powercontrol.sh Normal file
View File

@@ -0,0 +1,94 @@
#!/bin/bash
. /home/spezi/Scripts/utils/logger.sh
function stripoff { i=0; while [[ $i -lt 1 ]]; do echo "$1 off"; timeout 3 curl -D - -s --socks5 127.0.0.1:9998 -X GET -i "http://192.168.1.6:3000/api/v1/switch/10101/$1/off"; ((i++)); done; } #&>/dev/null
function stripon { i=0; while [[ $i -lt 1 ]]; do echo "$1 on"; timeout 3 curl -D - -s --socks5 127.0.0.1:9998 -X GET -i "http://192.168.1.6:3000/api/v1/switch/10101/$1/on"; ((i++)); done; }
function deviceon { i=0; while [[ $i -lt 1 ]]; do echo "$1 on"; timeout 10 curl -D - -s --socks5 127.0.0.1:9998 -X GET -i "http://192.168.1.6:7777/$1/on"; ((i++)); done; }
function deviceoff { i=0; while [[ $i -lt 1 ]]; do echo "$1 off"; timeout 10 curl -D - -s --socks5 127.0.0.1:9998 -X GET -i "http://192.168.1.6:7777/$1/off"; ((i++)); done; }
function stripcontrol () {
strip=$1
mode=$2
if [ -z "$3" ]; then
count=2
else
count=$3
fi
if [ -z "$4" ]; then
device=""
else
device=$4
fi
# Rothenburg special
if [[ "$strip" -eq "99" ]] || [[ "$strip" -eq "98" ]] || ( [[ "$strip" -gt "910" ]] && [[ "$strip" -lt "942" ]] ); then
case "$strip" in
98)
iftttname="pogoan_rot07-12_"
logger "calling ROT 07-12";;
99)
iftttname="pogoan_rot01-06_"
logger "calling ROT 01-06";;
"911")
iftttname="pogoan_stack101-106_"
logger "calling ROT stack 1 part 1";;
"912")
iftttname="pogoan_stack107-112_"
logger "calling ROT stack 1 part 2";;
"921")
iftttname="pogoan_stack201-206_"
logger "calling ROT stack 2 part 1";;
"922")
iftttname="pogoan_stack207-212_"
logger "calling ROT stack 2 part 2";;
"931")
iftttname="pogoan_stack301-306_"
logger "calling ROT stack 3 part 1";;
"932")
iftttname="pogoan_stack307-312_"
logger "calling ROT stack 3 part 2";;
"941")
iftttname="pogoan_stack401-406_"
logger "calling ROT stack 4 part 1";;
"942")
iftttname="pogoan_stack407-412_"
logger "calling ROT stack 4 part 2";;
esac
i=0
while [[ $i -lt $count ]]; do
((i++))
url="https://maker.ifttt.com/trigger/${iftttname}${mode}/with/key/k8H7hcXwpdmjP-ff-KshNiEoh5YxzJtTOunQ78rf_qJ"
timeout 10 curl "$url"
logger "turned strip $strip $mode, url: $url ($i)"
sleep 1
done
# in house strips
elif [[ "$strip" -gt "70" ]] && [[ "$strip" -lt "81" ]]; then
num="$(($strip - 70))"
if [[ "$mode" == "off" ]]; then
deviceoff $num
logger "turned device $num off"
elif [[ "$mode" == "on" ]]; then
deviceon $num
logger "turned device $num on"
fi
else
if [[ "$mode" == "off" ]]; then
stripoff $strip
logger "turned strip $strip off"
elif [[ "$mode" == "on" ]]; then
stripon $strip
logger "turned strip $strip on"
fi
fi
}

1
status.json Normal file

File diff suppressed because one or more lines are too long

15
timing.sh Executable file
View File

@@ -0,0 +1,15 @@
#!/bin/bash
timing() {
local start=$(($(date +%s%N)/1000000))
result=$($@)
local exit_code=$?
local end=$(($(date +%s%N)/1000000))
tookms=$((${end}-${start}))
# tooks=$(bc <<< "scale=3; $tookms/1000")
tooks=$(echo "scale=3; $tookms/1000" | bc | sed 's/^\./0./')
# echo >&2 "took ~${tookms}ms. exited with ${exit_code}"
echo $tooks $result
return $exit_code
}

11
utils.sh Normal file
View File

@@ -0,0 +1,11 @@
#!/bin/bash
isarray() {
var=$1
declare -p $1 2> /dev/null | grep -q '^declare \-a' && return 0
[[ ${var[0]} -eq ${var[0]} ]] && [[ ${var[1]} -eq ${var[1]} ]] && return 0 || return 1
}
gratlas() {
grep --line-buffered -i -v -e screenPath -e "no walker set" -e "nopstrategy" -e "StrategyFactory:117" -e "Worker:204" -e "Worker:150"
}

11
valid-ip.sh Normal file
View File

@@ -0,0 +1,11 @@
#!/bin/bash
# https://stackoverflow.com/questions/13777387/check-for-ip-validity
function validip () {
ip=$1
if [ -z $ip ] || ! [[ $ip =~ ^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
return 1
else
return 0
fi
}