From 3499e75cd5d747f3f222f878aeb99e29d77f085e Mon Sep 17 00:00:00 2001
From: KatolaZ <katolaz@freaknet.org>
Date: Tue, 3 Jan 2017 00:34:47 +0000
Subject: Added dump to file -- Show interface status -- other changes

---
 setnet.dash | 377 ++++++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 327 insertions(+), 50 deletions(-)

(limited to 'setnet.dash')

diff --git a/setnet.dash b/setnet.dash
index bb92fdd..e005efa 100755
--- a/setnet.dash
+++ b/setnet.dash
@@ -19,7 +19,8 @@
 #
 # ----------------------------------------------------------------------
 #
-# Copyleft (c) KatolaZ (katolaz@freaknet.org) -- (2016, 2017)
+# Copyleft (C) Vincenzo "KatolaZ" Nicosia (katolaz@freaknet.org) --
+# (2016, 2017)
 #
 #
 
@@ -55,6 +56,14 @@ HARD_DEPS="ip dhclient dialog netstat"
 
 SOFT_DEPS="wpa_cli wpa_supplicant"
 
+##
+## Optional dependencies. The script will check if those dependencies
+## exist, and if they do, will set a variable HAS_OPTS which contains
+## the names of the commands actually found
+##
+
+OPT_DEPS="host arping traceroute netstat pastebinit"
+
 #################################
 
 #####################################
@@ -226,11 +235,14 @@ check_shell(){
 ##
 ## - check if the current shell is supported through check_shell
 ##
-## - each command in HARD_DEPS MUST exists, or the script exits
+## - each command in HARD_DEPS MUST exist, or the script exits
 ##
-## - each command in SOFT_DEPS SHOULD exists, od the script will log a
+## - each command in SOFT_DEPS SHOULD exist, or the script will log a
 ##   warning
 ##
+## - each command in OPT_DEPS MIGHT exist, and if it does its name is
+##   included in the variable "HAS_OPTS"
+##
 
 ##function
 check_deps(){
@@ -239,19 +251,29 @@ check_deps(){
     
     for h in ${HARD_DEPS}; do
         _W=$(which ${h})
-        if [ -z $_W ]; then
+        if [ -z ${_W} ]; then
             echo "Error: required command \"${h}\" not found. Exiting..."
             exit 1
         fi
-        log "check_deps" "${h}...found"
+        log "check_deps" "NOTICE: required command '${h}'...found"
     done
     
     for s in ${SOFT_DEPS}; do
         _S=$(which ${s})
-        if [ -z $_S ]; then
-            log "check_deps" "WARNING: ${s} not found! Some functions might not work properly"
+        if [ -z ${_S} ]; then
+            log "check_deps" "WARNING: suggested command '${s}' not found! Some functions might not work properly"
         fi
     done
+
+    HAS_OPTS=""
+    for o in ${OPT_DEPS}; do
+        _O=$(which ${o})
+        if [ -n ${_O} ]; then
+            HAS_OPTS=" ${HAS_OPTS} ${o} "
+            log "check_deps" "NOTICE: optional command '${o}'...found"
+        fi
+    done
+    
 }
 
 ##
@@ -430,9 +452,9 @@ config_ethernet_dhcp(){
 ##local 
 DEVNAME=$1
 
-	eval "${DIALOG}  --infobox 'Running \"dhclient ${DEVNAME}\"' ${INFO_HEIGHT} ${INFO_WIDTH}"
-	dhclient -r ${DEVNAME}
-	dhclient ${DEVNAME}
+	eval "${DIALOG}  --msgbox 'Running \"dhclient ${DEVNAME}\"' ${INFO_HEIGHT} ${INFO_WIDTH}"
+	dhclient -r ${DEVNAME} 
+	dhclient ${DEVNAME} 
 	show_device_conf ${DEVNAME}
 }
 
@@ -855,7 +877,7 @@ wifi_load_file(){
 			      if [ $? -eq 0 ]; then
 				        SEL_FILE=$(cat ${TMPFILE})
 			      else
-				        eval "${DIALOG}  --clear --infobox 'WPA_FILE was not modified' \
+				        eval "${DIALOG}  --clear --msgbox 'WPA_FILE was not modified' \
 						   ${INFO_HEIGHT} ${INFO_WIDTH}"
 				        return
 			      fi
@@ -870,12 +892,12 @@ wifi_load_file(){
 				        wifi_restart_wpa ${DEVNAME} ${WPA_FILE}
 			      fi
 		    else
-			      eval "${DIALOG}  --clear --infobox 'Invalid file name!\n WPA_FILE *not* changed' \
+			      eval "${DIALOG}  --clear --msgbox 'Invalid file name!\n WPA_FILE *not* changed' \
 					  ${WINDOW_HEIGHT} ${WINDOW_WIDTH}"
 			      return 
 		    fi
 	  else
-		    eval "${DIALOG}  --clear --infobox 'WPA_FILE was not modified' \
+		    eval "${DIALOG}  --clear --msgbox 'WPA_FILE was not modified' \
 				   ${INFO_HEIGHT} ${INFO_WIDTH}"
 	  fi
 	  
@@ -957,22 +979,20 @@ config_wifi(){
 configure_device(){
 
 ##local 
-DEVNAME=$1
-	
-	case ${DEVNAME} in
-		eth*)
-			config_ethernet ${DEVNAME}
-			;;
-		wlan*)
-			config_wifi ${DEVNAME}
-			;;
-		*)
-			eval "${DIALOG}  --clear --title 'ERROR' --msgbox \
-			'${DEVNAME}: Unsupported device type' \
-			${WINDOW_HEIGHT} ${WINDOW_WIDTH}"
-			;;
-	esac
+    DEVNAME=$1
 
+    ## Check if the network device is a wifi -- WEAK
+    IS_WIFI=$(iwconfig ${DEVNAME} 2>/dev/null | grep -c "IEEE 802.11")
+    
+	  case ${IS_WIFI} in
+		    0)
+			      config_ethernet ${DEVNAME}
+			      ;;
+		    *)
+			      config_wifi ${DEVNAME}
+			      ;;
+	  esac
+    
 
 }
 
@@ -1002,8 +1022,12 @@ show_device_menu(){
 	
 ##local 
     DEVNAME=$1
+    
     while true; do 	
-		    eval "${DIALOG}  --clear --cancel-label 'Up' --menu 'Device: ${DEVNAME}' \
+        DEV_STATUS=$(ip -o link | cut -d " " -f 2,9 | grep -E "^${DEVNAME}: " | cut -d " " -f 2)
+        log "show_device_menu" "DEVNAME: ${DEVNAME} DEV_STATUS: ${DEV_STATUS}"
+		    eval "${DIALOG}  --clear --cancel-label 'Up' --menu\
+             'Device: ${DEVNAME}\nStatus: ${DEV_STATUS}' \
 			       ${WINDOW_HEIGHT} ${WINDOW_WIDTH} 8 \
 			       'View' 'View current configuration' \
 			       'Conf' 'Configure device' \
@@ -1082,18 +1106,27 @@ show_info(){
 
 	cat <<EOF > ${TMPFILE}
 
-== setnet.sh 0.1 ==
+                      -+- setnet.sh ${VERSION} -+-
 
 setnet.sh is a simple state-less tool to manage and configure network
-interfaces. It is a shell wrapper around the functionalities of "ip",
-"dhclient", "wpa_cli", and can be used to configure network
-connections via Ethernet/Wi-Fi interfaces.
+interfaces. It is a shell wrapper around the functionalities of
+standard command-line tools, including "ip", "dhclient", "wpa_cli",
+etc., and can be used to configure network connections via
+Ethernet/Wi-Fi interfaces.
 
-Both Static and DHCP-based IP configuration is supported. 
+Both Static and DHCP-based IP configuration are supported. 
 
 At the moment, only WPA-PSK and open (no key) Wi-Fi connections are
 available. 
 
+For more information, please visit the webpage of the project:
+
+    http://kalos.mine.nu/setnet/
+
+Please report bugs at:
+
+    https://git.devuan.org/KatolaZ/setnet
+
 EOF
 	eval "${DIALOG}  --clear --cr-wrap --textbox ${TMPFILE} ${WINDOW_HEIGHT} ${WINDOW_WIDTH}"
 	return
@@ -1104,9 +1137,15 @@ show_copyright(){
 
 	cat <<EOF > ${TMPFILE}
 
-== setnet.sh 0.1 ==
+                          -+- setnet.sh ${VERSION} -+-
+
+--------------------------------------------------------------------
+
+  Copyleft (C) Vincenzo "KatolaZ" Nicosia <katolaz@freaknet.org> 
+               2016, 2017
+
+--------------------------------------------------------------------
 
-(c) KatolaZ (katolaz@freaknet.org) -- 2016, 2017
 
 EOF
 	eval "${DIALOG}  --clear --cr-wrap --textbox ${TMPFILE} ${WINDOW_HEIGHT} ${WINDOW_WIDTH}"
@@ -1119,7 +1158,7 @@ show_license(){
 
 	cat <<EOF > ${TMPFILE}
 
-== setnet.sh 0.1 ==
+                       -+- setnet.sh ${VERSION} -+-
 
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
@@ -1134,11 +1173,12 @@ General Public License for more details.
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
-----------------------------------------------------------------------
+--------------------------------------------------------------------
 
- (c) KatolaZ <katolaz@freaknet.org> -- 2016, 2017
+   Copyleft (C) Vincenzo "KatolaZ" Nicosia <katolaz@freaknet.org> 
+                2016, 2017
 
-----------------------------------------------------------------------
+--------------------------------------------------------------------
 
 EOF
 	eval "${DIALOG}  --clear --cr-wrap --textbox ${TMPFILE} ${WINDOW_HEIGHT} ${WINDOW_WIDTH}"
@@ -1176,9 +1216,32 @@ about_menu(){
 	  done
 }
 
+##function
+notfound(){
+
+    CMDNAME=$1
+
+    
+    eval "${DIALOG} --clear --msgbox 'Sorry! Commmand ${CMDNAME} not found!'" \
+         ${INFO_HEIGHT} ${INFO_WIDTH}
+    
+}
+
+
 ##function
 netdiag_DNS(){
 
+    DUMPFILE=$1
+    
+    if [ -n "${DUMPFILE}" ]; then
+        ## Dump to file
+        printf "\n======== DNS Configuration (/etc/resolv.conf)\n" >> ${DUMPFILE}
+        cat /etc/resolv.conf >> ${DUMPFILE}
+        echo "==================================" >> ${DUMPFILE}
+        return 0
+    fi
+
+    ## Dump to dialog
     NAMESERVERS=$(grep '^nameserver' /etc/resolv.conf)
     MSG_STR="Configured name servers in /etc/resolv.conf ==\n\n${NAMESERVERS}"
 
@@ -1187,11 +1250,50 @@ netdiag_DNS(){
     
 }
 
+##function
+netdiag_resolver(){
+
+    DUMPFILE=$1
+    
+    if [ -n "${DUMPFILE}" ]; then
+        ## Dump to file
+        printf "\n======== Resolver Configuration (/etc/nsswitch.conf)\n" >> ${DUMPFILE}
+        grep -v '^#' /etc/nsswitch.conf >> ${DUMPFILE}
+        echo "==================================" >> ${DUMPFILE}
+        return 0
+    fi
+
+    ## Dump to dialog
+    RESOLVER=$(grep -v '^#' /etc/nsswitch.conf)
+
+    eval "${DIALOG} --clear --title 'Resolver configuration (/etc/nsswitch.conf)' \
+          --msgbox '${RESOLVER}' "\
+         ${WINDOW_HEIGHT} ${WINDOW_WIDTH}
+    
+}
+
+
 ##function
 netdiag_routes(){
 
-    ROUTES=$(netstat -rn > ${TMPFILE} )
+    DUMPFILE=$1
 
+    HAS_NETSTAT=$(echo ${HAS_OPTS} | grep -c " netstat ")
+    if [ ${HAS_NETSTAT} -ne 1 ]; then
+        notfound "nestat"
+        return
+    fi
+
+    if [ -n "${DUMPFILE}" ]; then
+        ## Dump to file
+        printf "\n======== Routing table\n" >> ${DUMPFILE}
+        netstat -rn >> ${DUMPFILE}
+        echo "==================================" >> ${DUMPFILE}
+        return 0
+    fi
+    ## Dump to dialog
+    ROUTES=$(netstat -rn > ${TMPFILE} )
+    
     eval "${DIALOG} --clear --no-collapse --title 'Routing table (netstat -rn) [arrows to scroll]'" \
          "--tab-correct --tab-len 4 --textbox ${TMPFILE} "\
          ${LARGE_HEIGHT} ${LARGE_WIDTH}
@@ -1200,6 +1302,17 @@ netdiag_routes(){
 ##function
 netdiag_ARP(){
 
+    DUMPFILE=$1
+    log "netdiag_ARP" "DUMPFILE: '${DUMPFILE}'"
+    if [ -n "${DUMPFILE}" ]; then
+        ## Dump to file
+        printf "\n======== ARP table\n" >> "${DUMPFILE}"
+        cat /proc/net/arp >> "${DUMPFILE}"
+        echo "==================================" >> ${DUMPFILE}
+        return 0
+    fi
+
+    # Dump to dialog
     ARP=$(cat /proc/net/arp >${TMPFILE})
 
     eval "${DIALOG} --clear --no-collapse --title 'ARP table (/proc/net/arp) [arrows to scroll]'" \
@@ -1210,8 +1323,26 @@ netdiag_ARP(){
 ##function
 netdiag_connections(){
 
-    SERV=$(netstat -tnp | sed -r -e 's/$/\n/g' > ${TMPFILE})
+    DUMPFILE=$1
+
+   
+    HAS_NETSTAT=$(echo ${HAS_OPTS} | grep -c " netstat ")
+    if [ ${HAS_NETSTAT} -ne 1 ]; then
+        notfound "nestat"
+        return
+    fi
+
+    if [ -n "${DUMPFILE}" ]; then
+        ## Dump to file
+        printf "\n======== Active Network Connections\n" >> ${DUMPFILE}
+        netstat -tnp | sed -r -e 's/$/\n/g' >> ${DUMPFILE}
+        echo "==================================" >> ${DUMPFILE}
+        return 0
+    fi
 
+    ## Dump to dialog
+    SERV=$(netstat -tnp | sed -r -e 's/$/\n/g' > ${TMPFILE})
+    
     eval "${DIALOG} --clear --no-collapse "\
          " --title 'Active network connections (netstat -tnp) [arrows to scroll]'" \
          "--tab-correct --tab-len 4 --textbox ${TMPFILE} "\
@@ -1222,6 +1353,23 @@ netdiag_connections(){
 ##function
 netdiag_services(){
 
+    DUMPFILE=$1
+
+    HAS_NETSTAT=$(echo ${HAS_OPTS} | grep -c " netstat ")
+    if [ ${HAS_NETSTAT} -ne 1 ]; then
+        notfound "nestat"
+        return
+    fi
+
+    if [ -n "${DUMPFILE}" ]; then
+        ## Dump to file
+        printf "\n======== Active network services\n" >> ${DUMPFILE}
+        netstat -ltnp | sed -r -e 's/$/\n/g' >> ${DUMPFILE}
+        echo "==================================" >> ${DUMPFILE}
+        return 0
+    fi
+    
+
     SERV=$(netstat -ltnp | sed -r -e 's/$/\n/g' > ${TMPFILE})
     
     eval "${DIALOG} --clear --no-collapse "\
@@ -1234,6 +1382,11 @@ netdiag_services(){
 ##function
 netdiag_ping(){
     
+    HAS_PING=$(echo ${HAS_OPTS} | grep -c " ping ")
+    if [ ${HAS_PING} -ne 1 ]; then
+        notfound "ping"
+        return
+    fi
     eval "${DIALOG} --insecure --inputbox 'Host or IP to ping:' \
 				   ${INFO_HEIGHT} ${INFO_WIDTH}"  2> ${TMPFILE}
 
@@ -1258,6 +1411,11 @@ netdiag_ping(){
 ##function
 netdiag_traceroute(){
     
+    HAS_TRACERT=$(echo ${HAS_OPTS} | grep -c " traceroute ")
+    if [ ${HAS_TRACERT} -ne 1 ]; then
+        notfound "traceroute"
+        return
+    fi
     eval "${DIALOG} --insecure --inputbox 'Host or IP to trace:' \
 				   ${INFO_HEIGHT} ${INFO_WIDTH}"  2> ${TMPFILE}
 
@@ -1281,7 +1439,13 @@ netdiag_traceroute(){
 
 ##function
 netdiag_query(){
-    
+
+    HAST_HOST=$(echo ${HAS_OPTS} | grep -c " host ")
+    if [ $? -ne 1 ]; then
+        notfound "host"
+        return
+    fi
+
     eval "${DIALOG} --insecure --inputbox 'Hostname or IP to query:' \
 				   ${INFO_HEIGHT} ${INFO_WIDTH}"  2> ${TMPFILE}
     
@@ -1303,6 +1467,20 @@ netdiag_query(){
 
 }
 
+##function
+netdiag_devices(){
+
+    DUMPFILE=$1
+
+    if [ -n "${DUMPFILE}" ]; then
+        printf "\n======== Network Devices\n" >> ${DUMPFILE}
+        ip addr >> ${DUMPFILE}
+        echo "==================================" >> ${DUMPFILE}
+        return 0
+    fi
+}
+
+
 
 ##
 ## Main menu for network diagnostics
@@ -1319,6 +1497,7 @@ netdiag_menu(){
 			   'DNS' 'List DNS servers' \
          'Ping' 'Ping a host'  \
          'Query' 'DNS Query' \
+         'Resolver' 'Show resolver configuration' \
 			   'Routes' 'Show routing table' \
          'Services' 'List active network daemons'  \
          'Traceroute' 'Show the route to a host' " \
@@ -1343,6 +1522,9 @@ netdiag_menu(){
 				        ;;
             "Query")
                 netdiag_query
+                ;;
+			      "Resolver")
+				        netdiag_resolver
                 ;;
 			      "Routes")
 				        netdiag_routes
@@ -1359,6 +1541,96 @@ netdiag_menu(){
     
 }
 
+##function
+dump_file(){
+    
+    CONF=$1
+
+    log "dump_file" "CONF: ${CONF}"
+    
+    DUMPFILE="/tmp/network_dump.txt"
+    
+	  eval "${DIALOG}  --fselect ${DUMPFILE} ${WINDOW_HEIGHT} ${WINDOW_WIDTH}" \
+			   2>${TMPFILE}
+	  
+	  if [ $? -eq 0 ]; then
+		    SEL_FILE=$(cat ${TMPFILE})
+		    while [ -d "${SEL_FILE}" ]; do
+			      eval "${DIALOG}  --fselect ${SEL_FILE} ${WINDOW_HEIGHT} ${WINDOW_WIDTH}" \
+					   2>${TMPFILE}
+			      if [ $? -eq 0 ]; then
+				        SEL_FILE=$(cat ${TMPFILE})
+			      else
+				        eval "${DIALOG}  --clear --msgbox 'Dump aborted' \
+						   ${INFO_HEIGHT} ${INFO_WIDTH}"
+				        return
+			      fi
+		    done
+		    
+        ## The dump starts here....
+			  DUMPFILE=${SEL_FILE}
+        truncate -s 0 ${DUMPFILE}
+        echo "===== setnet ${VERSION}" >> ${DUMPFILE}
+        echo "===== Date: $(date)" >> ${DUMPFILE}
+        echo "===== Network configuration dump " >> ${DUMPFILE}
+        for c in ${CONF}; do
+            eval "netdiag_${c} \"${DUMPFILE}\""
+        done
+	  else
+		    eval "${DIALOG}  --clear --msgbox 'Dump aborted' \
+				   ${INFO_HEIGHT} ${INFO_WIDTH}"
+	  fi
+}
+
+
+##function
+dump_pastebin(){
+    
+    CONF=$1
+
+    
+
+}
+
+##function 
+dump_menu(){
+
+    eval "${DIALOG} --clear --checklist 'Select conf to dump' \
+             ${WINDOW_HEIGHT} ${WINDOW_WIDTH} 10 \
+             'ARP' 'ARP table' on \
+             'devices' 'Device configuration' on \
+             'DNS' 'DNS configuration' on \
+             'resolver' 'System resolver configuration' on \
+             'routes' 'Routing table' on \
+             'connections' 'Active network connections' on \
+             'services' 'Active network services' on " 2> ${TMPFILE}
+    if [ $? -ne 0 ]; then
+        return
+    fi
+    
+    DUMP_CONF=$(cat ${TMPFILE})
+    
+    eval "${DIALOG} --clear --cancel-label 'Up' \
+           --menu 'Dump configuration to:' \
+           ${INFO_HEIGHT} ${INFO_WIDTH} 6 \
+           'File' 'Dump to file' \
+           'Pastebin' 'Dump to pastebin'" \
+         2> ${TMPFILE}
+    if [ $? -eq 1 ];then
+			  return;
+		fi
+		
+		ACTION=$(cat ${TMPFILE})
+		case ${ACTION} in
+			  "File")
+            dump_file "${DUMP_CONF}"
+				    ;;
+			  "Pastebin")
+            dump_pastebin "${DUMP_CONF}"
+				    ;;
+		esac
+}
+
 
 ##function 
 show_toplevel(){
@@ -1368,6 +1640,7 @@ show_toplevel(){
 		   ${WINDOW_HEIGHT} ${WINDOW_WIDTH} 6 \
 		   'Setup' 'Setup interfaces' \
        'Info' 'Network diagnostics' \
+       'Dump' 'Dump current network status' \
 		   'About' 'License & Copyleft'" 2> ${TMPFILE}
     
 	  return $?
@@ -1407,11 +1680,12 @@ show_disclaimer(){
 
 	cat <<EOF > ${TMPFILE}
 
-                == setnet.sh 0.1 ==
+                -+- setnet.sh ${VERSION} -+-
 
-      (c) KatolaZ (katolaz@freaknet.org) -- 2016, 2017
+      Copyleft (C) KatolaZ (katolaz@freaknet.org) 
+                    2016, 2017
 
-    -+- This is the alpha release of setnet.sh -+-
+     -+- This is an alpha release of setnet.sh -+-
    
                  THIS IS FREE SOFTWARE
         YOU CAN USE AND DISTRIBUTE IT UNDER THE 
@@ -1423,7 +1697,7 @@ show_disclaimer(){
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE
 
         See "About" for more information about 
-                 copyright and license
+           your right and distribution terms
 EOF
 
 	eval "${DIALOG}  --clear --cr-wrap --textbox ${TMPFILE} 23 60"
@@ -1437,7 +1711,7 @@ initialise(){
     TMPFILE=$( (tempfile) 2>/dev/null) || TMPFILE=/tmp/setnet_$$
     WPA_PIDFILE=$( (tempfile) 2>/dev/null) || WPA_PIDFILE=/tmp/setnet_wpapid_$$
     
-	  trap cleanup 0 NONE HUP INT TRAP TERM QUIT
+	  trap cleanup 0 HUP INT TRAP TERM QUIT
 
     if [ -z ${TRUNCATE_LOG} ] || \
            [ ${TRUNCATE_LOG} = "yes" ] || \
@@ -1478,6 +1752,9 @@ main(){
 				      ;;
           "Info")
               netdiag_menu
+              ;;
+          "Dump")
+              dump_menu
               ;;
 			    "About")
 				      about_menu
@@ -1503,9 +1780,9 @@ while getopts ":c:hv" opt; do
 	  
 	  case $opt in
 		    c)
-			      echo "Got option -c ${OPTARG}"
+			      #echo "Got option -c ${OPTARG}"
 			      SETNETRC=$(realpath ${OPTARG})
-			      echo "SETNETRC: ${SETNETRC}"
+			      #echo "SETNETRC: ${SETNETRC}"
 			      ;;
 		    h)
 			      show_help $(basename $0)
-- 
cgit v1.2.3