Browse Source

adding function to switch DNS nameservers + monitoring of current settings; adding a README

master
mxmehl 5 years ago
parent
commit
9d605278e7
  1. 46
      README.md
  2. 2
      config.cfg.sample
  3. 56
      status-interaction.sh
  4. 17
      status-misc.sh

46
README.md

@ -0,0 +1,46 @@
# Dynamic Status
These scripts control and monitor several functions of my computer. Most importantly it is possible to switch to a "offline status" which disables several internet-using monitoring functions.
status-ip.sh and status-misc.sh rely on `xfce-genmon-plugin` to show some status messages on the XFCE task bars.
status-interaction.sh enables users to actively change some values, e.g. the on-/offline status, the used DNS nameserver, and to trigger some actions, e.g. to send mails in an `msmtp` queue.
I tried to make it as compatible to as many systems as possible but some values are still hard-coded.
## DNS nameserver switch
This feature heavily depends on network-manager and it's dispatcher feature. In order to use it, put a file in `/etc/NetworkManager/dispatcher.d/` and name it `90-custom-resolv.conf` for example. Fill it:
```
#!/bin/bash
#
# Depending on dynamic status, override /etc/resolv.conf
CUSTOM=/tmp/resolv_custom.conf
DEFAULT=/home/user/bin/default-nameserver.sh
if [ ! -e $CUSTOM ]; then # if file does not exists, use default nameserver script
bash $DEFAULT
echo "# 90-custom-resolvconf executed default-nameserver.sh" >> /etc/resolv.conf
exit 0
fi
SET=$(cat $CUSTOM | head -n1 | awk '{print $NF}') # first line, last word
if [ "$SET" != "auto" ]; then
cp -f /etc/resolv.conf /etc/resolv.conf.bak # backup original resolv config file
cat $CUSTOM > /etc/resolv.conf # use custom file, update original one
else
echo "# 90-custom-resolvconf didn't change any substantial values here, just added this line." >> /etc/resolv.conf
fi
```
The file has to be owned by root and be executable in order to function. It will either execute a script that sets default nameserver settings (in my case, it's localhost since I use a local dnsmasq), or take the values from another file which is created by switching the nameservers with status-interaction.sh.
Nameservers can be configured in `config.cfg` as value of `NS=`. It holds following pattern:
`set1(ip1,ip2,ip3)|set2(ip4,ip5)`
Please remember to put it between quotes.

2
config.cfg.sample

@ -12,6 +12,8 @@ VPNIP="1.2.3.4|90.80.70.60"
# status-misc.sh
SVNSTATUS = http://website.showing."fin;revision".or.similar
NS="dnsmasq(127.0.0.1,::1)|google(8.8.8.8,8.8.4.4,2001:4860:4860::8888,2001:4860:4860::8844)"
# MSMTP paths
MSMTP_LIST=/usr/local/bin/msmtp-listqueue.sh

56
status-interaction.sh

@ -34,9 +34,58 @@ function switch_conn {
fi
}
# SWITCH NAMESERVERS
function switch_ns {
read -p "Switch nameservers? [Y/n]: " YN
if [[ $YN =~ ^(Y|y|)$ ]]; then
NS_SETS=$(echo "$NS" | grep -o "|" | wc -l) # number of nameserver sets (separated by |)
NS_SETS=$(expr $NS_SETS + 1) # add number +1 to correct count
for ((i = 1; i <= $NS_SETS; i++)); do # circle through all nameserver sets and number them
NS_NAME[$i]=$(echo $NS | cut -d"|" -f$i | sed -E 's/(^.+?)\((.+?)\)/\1/')
NS_VALUE[$i]=$(echo $NS | cut -d"|" -f$i | sed -E 's/(^.+?)\((.+?)\)/\2/')
echo "$i. ${NS_NAME[$i]}"
done
NS_NAME[0]="auto"; NS_VALUE[0]=""
echo "0. ${NS_NAME[0]} (DHCP-given)"
# Choose nameserver number and verify selection
OK=0
while [ "$OK" != "1" ]; do
echo; read -p "Type number of nameserver set: " i
if [ "${NS_NAME[$i]}" == "" ]; then
OK=0
echo "A nameserver set with this number does not exist. Please choose again."
else
OK=1
echo "Nameserver set switched to \"${NS_NAME[$i]}\". Please reconnect with NetworkManager to put the changes into effect."
fi
done
# Set definite values
NS_NAME=${NS_NAME[$i]}
NS_VALUE=${NS_VALUE[$i]}
NONS=$(echo "$NS_VALUE" | grep -o "," | wc -l) # number of nameservers in chosen set
NONS=$(expr $NONS + 1)
echo "# Custom nameserver set: $NS_NAME" > /tmp/resolv_custom.conf # write custom resolv config, which will be interpreted by "90-custom-resolvconf"
for ((i = 1; i <= $NONS; i++)); do
echo "nameserver $(echo $NS_VALUE | cut -d"," -f$i)" >> /tmp/resolv_custom.conf
done
var_ns=$NS_NAME # save name of nameserver set for later update of status file
fi # / YN
}
# OPTION SCREEN
function exec {
ACTIONS=("send mails in queue" "list mails in queue" "switch connection status [$var_conn]" "quit")
ACTIONS=("send mails in queue" "list mails in queue" "switch connection status [$var_conn]" "switch nameservers" "quit")
PS3="Select action: "
select action in "${ACTIONS[@]}"
@ -52,7 +101,10 @@ function exec {
3) # Switch online status
switch_conn
break ;;
4|"q") # exit silently, one can also press "q" instead of the number
4) # Switch nameservers
switch_ns
break ;;
5|"q") # exit silently, one can also press "q" instead of the number
exit 0
break ;;
esac

17
status-misc.sh

@ -15,11 +15,13 @@ function isonline {
fi
}
# connection status
TOOL="$TOOL;conn=$var_conn"
# NUMBER OF UNSENT EMAILS
function check_mailqueue {
QUEUE=$($MSMTP_LIST | grep -o "^From: " | wc -l)
OUT="$OUT;mq=$QUEUE"
TOOL="$TOOL;conn=$var_conn"
# If $QUEUE > 0 and >10 runs, show notification
if [ "$QUEUE" -gt 0 ]; then
@ -52,10 +54,23 @@ function check_svn {
TOOL="$TOOL;svn=$SVN_REV"
}
# NAMESERVER STATUS
function check_ns {
FIRSTNS=$(grep -m1 "^nameserver" /etc/resolv.conf | cut -d" " -f2) # first nameserver in /etc/resolv.conf
if [ "$var_ns" == "" ]; then # var_ns is only set with status-interaction.sh, no default value in config.cfg
OUT="$OUT;ns=default"
else
OUT="$OUT;ns=$var_ns"
fi
TOOL="$TOOL;ns=$FIRSTNS..."
}
# OUTPUT INFO ON STATUSBAR
check_mailqueue
check_svn
check_ns
# Replace ; by | to make it look nicely
OUT=$(echo $OUT | sed -r 's/^;//')
OUT=$(echo $OUT | sed -r 's/;/ \| /g')
TOOL=$(echo $TOOL | sed -r 's/^;//')

Loading…
Cancel
Save