From 2964dc140418dc62c65b03a043025e9bd960d50f Mon Sep 17 00:00:00 2001 From: Bruno Fontes Date: Fri, 24 Dec 2021 11:01:28 -0300 Subject: [PATCH] Generic updates on laptop Again, I don't need to keep track of the changes. Just a matter of having them here. --- Services/after-wakeup.service | 2 +- Services/afterWakeUp.sh | 11 ++++++++++ Services/bruno-network-manager.service | 10 +++++++++ Services/bruno-network-manager.sh | 5 +++++ Services/enableDongleWifi.sh | 4 ++++ Services/fix-dns.service | 11 ---------- Services/fixDNS.sh | 28 +++++++++++++++----------- 7 files changed, 47 insertions(+), 24 deletions(-) create mode 100755 Services/afterWakeUp.sh create mode 100644 Services/bruno-network-manager.service create mode 100644 Services/bruno-network-manager.sh create mode 100755 Services/enableDongleWifi.sh delete mode 100644 Services/fix-dns.service diff --git a/Services/after-wakeup.service b/Services/after-wakeup.service index 9fdb467..755f433 100644 --- a/Services/after-wakeup.service +++ b/Services/after-wakeup.service @@ -4,7 +4,7 @@ After=suspend.target [Service] Type=simple -ExecStart=/usr/bin/systemctl restart openvpn-client@privatvpn.service +ExecStart=/bin/sh /home/bruno/Apps/linuxShortcuts/Services/afterWakeUp.sh [Install] WantedBy=suspend.target diff --git a/Services/afterWakeUp.sh b/Services/afterWakeUp.sh new file mode 100755 index 0000000..35ccb1c --- /dev/null +++ b/Services/afterWakeUp.sh @@ -0,0 +1,11 @@ +#/bin/sh + +sleep 10s +wifi=$(iwconfig 2>/dev/null | grep Quasimodo) +if [[ -z $wifi ]]; then + echo "Starting OPEN VPN" + /usr/bin/systemctl restart openvpn-client@archerc6.service +else + echo "At home, no VPN for you" + /usr/bin/systemctl stop openvpn-client@archerc6.service +fi diff --git a/Services/bruno-network-manager.service b/Services/bruno-network-manager.service new file mode 100644 index 0000000..eb75b05 --- /dev/null +++ b/Services/bruno-network-manager.service @@ -0,0 +1,10 @@ +[Unit] +Description=Manages Dongle Wifi, DNS and VPN +After=network-online.target + +[Service] +Type=oneshot +ExecStart=/bin/bash /home/bruno/Apps/linuxShortcuts/Services/bruno-network-manager.sh + +[Install] +WantedBy=network-online.target diff --git a/Services/bruno-network-manager.sh b/Services/bruno-network-manager.sh new file mode 100644 index 0000000..2d14ecd --- /dev/null +++ b/Services/bruno-network-manager.sh @@ -0,0 +1,5 @@ +#/bin/sh +cd /home/bruno/Apps/linuxShortcuts/Services/ +source ./enableDongleWifi.sh +source ./afterWakeUp.sh +source ./fixDNS.sh diff --git a/Services/enableDongleWifi.sh b/Services/enableDongleWifi.sh new file mode 100755 index 0000000..99a5366 --- /dev/null +++ b/Services/enableDongleWifi.sh @@ -0,0 +1,4 @@ +#/bin/sh + +dongleWifi=$(ip link | grep wlp | grep DOWN | awk '{ print $2 }' | tr -d :) +[ "$dongleWifi" ] && wpa_supplicant -B -i "$dongleWifi" -c /etc/wpa_supplicant/wpa_supplicant-wlan0.conf diff --git a/Services/fix-dns.service b/Services/fix-dns.service deleted file mode 100644 index 9e1accf..0000000 --- a/Services/fix-dns.service +++ /dev/null @@ -1,11 +0,0 @@ -[Unit] -Description=Test and fix DNS update with wrong time -Wants=network-online.target -After=network-online.target - -[Service] -Type=oneshot -ExecStart = /bin/bash /home/bruno/Apps/linuxShortcuts/Services/fixDNS.sh - -[Install] -WantedBy=multi-user.target diff --git a/Services/fixDNS.sh b/Services/fixDNS.sh index 51b2e7e..272f625 100755 --- a/Services/fixDNS.sh +++ b/Services/fixDNS.sh @@ -1,37 +1,41 @@ +#Aparentemente tem um momento em que quando o dns não está funcionando o dig também não retorna nada, então para o script a conexão está funcionando +source /home/bruno/enableDongleWifi.sh + function isDNSWorking() { - dnsWorking=$(dig brunofontes.net | grep "ANSWER: 0") + return $(dig brunofontes.net | grep "ANSWER: 0") } logfile="/var/log/bfontes" -sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Checking if DNS is working" >> "$logfile" -isDNSWorking +sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Checking if DNS is working" | sudo tee -a "$logfile" +dnsWorking=`isDNSWorking` +echo "$dnsWorking" if [ "$dnsWorking" ]; then - sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - DNS not working, hour may be wrong. Trying to fix..." >> "$logfile" + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - DNS not working, hour may be wrong. Trying to fix..." | sudo tee -a "$logfile" sudo chattr -i /etc/resolv.conf sudo sed -i 's/127.0.0.2/9.9.9.9/' /etc/resolv.conf 2>>"$logfile" sudo chattr +i /etc/resolv.conf - sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Forcing time to sync" >> "$logfile" + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Forcing time to sync" | sudo tee -a "$logfile" sudo chronyc online 2>"$logfile" sudo chronyc -a 'burst 4/4' i=0 while [ "$dnsWorking" -a $i -le 4 ]; do - sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Waiting..." >> "$logfile" + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Waiting..." | sudo tee -a "$logfile" sleep 20 - isDNSWorking + dnsWorking=`isDNSWorking` i=$(( "$i" + 1 )) done if [ -z "$dnsWorking" ]; then - sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - FIXED!" >> "$logfile" + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - FIXED!" | sudo tee -a "$logfile" else - sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Script has failed..." >> "$logfile" - sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Fixing DNS script has failed. More details on $logfile" >> ~/.log_error + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Script has failed..." | sudo tee -a "$logfile" + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Fixing DNS script has failed. More details on $logfile" | sudo tee -a ~/.log_error fi - sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Configuring DNS servers back." >> "$logfile" + sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Configuring DNS servers back." | sudo tee -a "$logfile" sudo chattr -i /etc/resolv.conf sudo sed -i 's/9.9.9.9/127.0.0.2/' /etc/resolv.conf sudo chattr +i /etc/resolv.conf fi -sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Done" >> "$logfile" +sudo echo "$(date +"%Y-%m-%d %H:%M:%S") - Done" | sudo tee -a "$logfile"