Ubuntu Sverige https://ubuntu-se.org/phpBB3/ |
|
nmtest https://ubuntu-se.org/phpBB3/viewtopic.php?f=67&t=62256 |
Sida 1 av 1 |
Författare: | Osprey [ 08 jun 2022, 12:37 ] |
Inläggsrubrik: | nmtest |
Nätverksverktyg som bygger på nmcli, vet inte om jag lagt ut det tidigare.... Kod: #! /bin/bash # # © Magnus Ewert & GPL # TTIMES=8 # Test times TTIMEOUT=2 # Test timeout # MORE=true STDIFS=$IFS # OS=$(cat /etc/os-release | grep ID_LIKE | awk '{ print $1 }' | awk -F = '{ print $2 }' | sed 's/"//g') #echo "OS: $OS" # if [[ $(whoami) != "root" ]]; then echo echo "-This script must be run by root..." echo exit fi # if [[ -t 1 ]]; then if [[ $MORE == true ]]; then if [[ $OS != "suse" ]]; then $0 $@ | more exit else #$0 $@ | less --QUIT-AT-EOF $0 $@ | less -F -X --quit-at-eof exit fi else #clear -x LILAC="\033[1m\033[35m" GREEN="\033[1m\033[32m" RED="\033[1m\033[31m" STD="\033[0m" fi elif [[ -f /dev/stdout ]]; then LILAC="" GREEN="" RED="" STD="" else #clear -x LILAC="\033[1m\033[35m" GREEN="\033[1m\033[32m" RED="\033[1m\033[31m" STD="\033[0m" fi # ##########################################[ USAGE ]##################################################################### # function usage { cat << EOD usage: $CMDNAME [-f] [-n] [-o] [-p] [-s] [-t] [-w] [DEVICE] DEVICE specifies the device to operate on. If not specified the script uses the first found device that looks ok. Options: -f|--fixed The system uses hardcoded addresses and DHCP shall not be tested. This is used by default if the host address is found in /etc/hosts. -n|--no-colors Do not use colors in the output. This is the default if output is redirected to a file. -o seconds|--timeout=seconds Timeout for ping, nslookup and wget, the default is $TTIMEOUT seconds. -p|--speed Perform a test of the network speed (it may take a little while). -s|--show Show more information. -t|--test Perform a test of the network functions. -w|--wget Use a wget spider to check reachability instead of ping. In some environments ping is prohibited and will always fail. EOD exit } # function reachable { IPADDRESS=$1 #echo "IPAD: $IPADDRESS" if [[ $RTYPE == "Ping" ]]; then putOut "-Checking reachability with $RTYPE" "--" ping -f -W $TTIMEOUT -c $TTIMES $IPADDRESS > /dev/null 2>&1 IRET=$? else putOut "-Checking reachability with $RTYPE" "--" #LC_ALL=C wget -v -d --tries=1 --timeout=1 --no-check-certificate https://192.168.1.1 LC_ALL=C wget -q --tries=$TTIMES --timeout=$TTIMEOUT --spider --no-check-certificate https://${IPADDRESS} IRET=$? fi return $IRET } # function putOut { INAME=$1 if [[ ! -z $2 ]]; then VALUES=$2 else VALUES="--" fi UP=$(echo $INAME | grep '^[A-Z]') if [[ ! -z $UP && $BAR == true ]]; then BAR=false echo "----" fi IFS="|" let I=0 for VALUE in $VALUES; do let I+=1 done #echo "I=$I" if [[ $I -gt 1 ]]; then IFS=" | " let I=0 for VALUE in $VALUES; do let I+=1 NAME="$INAME[$I]" printf "%-40s%s\n" $NAME "$VALUE" done else printf "%-40s%s\n" $INAME $VALUE fi } # ##########################################[ GETOPT ]#################################################################### # TEMP=`getopt -od:fhno:pstw --long dns:,fixed,help,no-colors,timeout:,show,speed,test,wget -n $(basename $0) -- "$@"` if [[ $? -ne 0 ]]; then usage fi eval set -- "$TEMP" TEST=false SPEED=false FIXED=false QUIET=false SHOW=false RTYPE="Ping" NEWDNS="" CMDNAME=$(basename $0) while true; do case $1 in -d|--dns) shift NEWDNS=$1 shift ;; -f|--fixed) FIXED=true shift ;; -h|--help) usage ;; -n|--no-colors) LILAC="" GREEN="" RED="" STD="" shift ;; -o|--timeout) shift TTIMEOUT=$1 shift ;; -p|--speed) SPEED=true shift ;; -s|--show) SHOW=true shift ;; -t|--test) TEST=true shift ;; -w|--wget) RTYPE="Wget" shift ;; --) shift break ;; *) echo "-Got a star..." shift ;; esac done PARAM=$1 # if [[ ! -z $NEWDNS ]]; then echo echo "NEWDNS: $NEWDNS" echo exit fi # ##########################################[ STARTUP ]################################################################### # clear -x echo # asleep - disconnected - connecting - connected #LC_ALL=C nmcli -t -f STATE general #systemctl start NetworkManager.service #OUT=$(LC_ALL=C nmcli -t -f RUNNING general 2>&1) #-- echo -e $LILAC"Network Manager:"$STD RUNNING=$(nmcli -t -f RUNNING general 2>&1) which systemctl >> /dev/null 2>&1; SYSTEMCTL=$? if [[ $RUNNING == "running" ]]; then echo $RUNNING if [[ $SYSTEMCTL == 0 ]]; then systemctl status NetworkManager.service echo fi else echo -en $RED echo "-Network Manager is not running" echo -e $GREEN echo "-Try with:" echo if [[ $SYSTEMCTL == 0 ]]; then echo " systemctl start NetworkManager.service" else echo " start network-manager" fi echo -e $RED echo "-Can not continue..." echo -e $STD exit fi ##################### DNS84=false DNS88=false #CONNECTIONS=$(nmcli -t -f name,device con) IFS=$STDIFS if [[ -z $PARAM ]]; then DEVICE="" DEVICES=$(LC_ALL=C nmcli -t -f device,state,connection device | grep -v ^lo: | grep -v "unavailable" ) for DEV in $DEVICES; do NAME=$(echo $DEV | awk -F : '{ print $1 }') STATE=$(echo $DEV | awk -F : '{ print $2 }') CONN=$(echo $DEV | awk -F : '{ print $3 }') #echo "NAME: $NAME" #echo "STATE: $STATE" #echo "CONN: $CONN" if [[ $STATE == "connected" || $STATE == "disconnected" ]]; then #GWY=$(nmcli -g IP4.GATEWAY device show $DEV) IPADDR12=$(nmcli -g IP4.ADDRESS dev show $NAME | awk -F . '{ print $1"."$2 }') #if [[ $DEV != $CONN ]]; then #if [[ $GWY != "0.0.0.0" ]]; then if [[ $IPADDR12 != "169.254" ]]; then # Skip link-local DEVICE=$NAME break fi elif [[ $STATE == "unmanaged" ]]; then DEVICE=$NAME # Save device, but let it be overritten if some better fi # comes, that is in stat connected or disconnected. done else DEVICE=$PARAM fi STDERR=$(nmcli -g GENERAL.DEVICE device show $DEVICE 2>&1 > /dev/null) if [[ $? != 0 ]]; then #nmcli -g GENERAL.DEVICE device show $DEVICE >> /dev/null 2>&1 echo -en $RED echo $STDERR echo -e $STD exit fi #DEVICE="usb0" #echo "DEVICE: $DEVICE" if [[ ! -z $DEVICE ]]; then CONNECTION=$(nmcli -g GENERAL.CONNECTION device show $DEVICE) if [[ ! -z $CONNECTION ]]; then #echo "CONNECTION: $CONNECTION" GATEWAY=$(nmcli -g IP4.GATEWAY connection show "$CONNECTION") #echo "GATEWAY: $GATEWAY" CONDHSRVR=$(nmcli con show "$CONNECTION" | grep " domain_name_servers" | awk -F = '{ print $2 }' | xargs) fi IPADDR=$(nmcli -g IP4.ADDRESS device show "$DEVICE" | awk -F / '{ print $1 }') #echo "IPADDR: $IPADDR" else CONNECTION="" IPADDR="" fi #CONNECTION="Tårdbunden anslutning 2" # ##########################################[ MAIN ]###################################################################### # ##########################################[ SHOW ]###################################################################### if [[ $SHOW == true ]]; then echo -e $LILAC"General Status:"$STD nmcli general status echo echo -e $LILAC"Devices:"$STD nmcli dev if [[ ! -z $(nmcli device wifi list) ]]; then echo echo -e $LILAC"WiFi:"$STD nmcli device wifi list fi echo echo -e $LILAC"Connections:"$STD nmcli con show echo BAR=false if [[ ! -z $DEVICE ]]; then echo -e $LILAC'Device: "'$DEVICE'"'$STD nmcli device show $DEVICE else echo -e $LILAC"Device: No specific device found - showing all..."$STD nmcli device show $DEVICE DEVICE="<none>" fi echo BAR=true if [[ ! -z $CONNECTION ]]; then echo -e $LILAC'Connection: "'$CONNECTION'"'$STD VALUES="$(nmcli -g connection.id connection show "$CONNECTION")" putOut "connection.id" "$VALUES" VALUES="$(nmcli -g connection.type connection show "$CONNECTION")" putOut "connection.type" "$VALUES" VALUES="$(nmcli -g connection.interface-name connection show "$CONNECTION")" putOut "connection.interface-name" "$VALUES" VALUES="$(nmcli -g connection.autoconnect connection show "$CONNECTION")" putOut "connection.autoconnect" "$VALUES" VALUES="$(nmcli -g connection.read-only connection show "$CONNECTION")" putOut "connection.read-only" "$VALUES" VALUES="$(nmcli -g 802-3-ethernet.mtu connection show "$CONNECTION")" putOut "802-3-ethernet.mtu" "$VALUES" VALUES="$(nmcli -g ipv4.method connection show "$CONNECTION")" putOut "ipv4.method" "$VALUES" VALUES="$(nmcli -g ipv4.dns connection show "$CONNECTION")" putOut "ipv4.dns" $VALUES VALUES="$(nmcli -g ipv4.addresses connection show "$CONNECTION")" putOut "ipv4.addresses" $VALUES VALUES="$(nmcli -g ipv4.gateway connection show "$CONNECTION")" putOut "ipv4.gateway" $VALUES VALUES="$(nmcli -g ipv4.routes connection show "$CONNECTION")" putOut "ipv4.routes" $VALUES VALUES="$(nmcli -g ipv4.ignore-auto-routes connection show "$CONNECTION")" putOut "ipv4.ignore-auto-routes" $VALUES VALUES="$(nmcli -g ipv4.ignore-auto-dns connection show "$CONNECTION")" putOut "ipv4.ignore-auto-dns" $VALUES else CONNECTION="<none>" echo -e $LILAC'Connection: "'$CONNECTION'"'$STD echo -en $RED echo "-No existing connection found..." echo -en $STD fi echo fi # ##########################################[ TEST ]###################################################################### # if [[ $TEST == true || $SPEED == true ]]; then BAR=false echo -e $LILAC'Internet: "'$CONNECTION'" via "'$DEVICE'"'$STD if [[ $DEVICE == "" || $DEVICE == "<none>" ]]; then echo -e $RED echo "-Can not run test without a device..." echo -e $STD exit fi DNSs=$(nmcli -g IP4.DNS device show $DEVICE) IFS=" | " for DNS in $DNSs; do if [[ $DNS == "8.8.4.4" ]]; then DNS84=true elif [[ $DNS == "8.8.8.8" ]]; then DNS88=true fi done fi if [[ $TEST == true ]]; then # # GENERAL #INTERNET=$(nmcli general status | tail -n +2 | awk '{ print $2 }') NONETWORK=false INTERNET=$(nmcli networking connectivity) #INTERNET="limited" if [[ $INTERNET == "full" ]]; then putOut "General Status:" "Full (the computer reaches Internet)" elif [[ $INTERNET == "limited" ]]; then putOut "General Status:" "Limited connection (to the gateway, but no more)" echo -e $RED echo "-The host is connected to a network, but it has no access to the Internet." echo "-Check that power is on, cables are connected and that the network is started." echo -e $GREEN echo "-Try with:" echo echo " nmcli device connect $DEVICE" echo echo " and if that does not work:" echo echo " nmcli networking on" echo echo "-Check also further on in the following tests..." echo -e $STD elif [[ $INTERNET == "portal" ]]; then putOut "General Status:" "Portal" echo -e $RED echo "-The host is behind a captive portal and cannot reach the full Internet." echo -e $STD echo "-Can not continue..." echo exit elif [[ $INTERNET == "none" ]]; then putOut "General Status:" "Not connected" echo -e $RED echo "-The host is not connected to any network." echo "-Check that power is on, cables are connected and that the network is started." echo -e $GREEN echo "-Try with:" echo echo " nmcli device connect $DEVICE" echo echo " and if that does not work:" echo echo " nmcli networking on" echo -e $STD echo "-Can not continue..." echo exit elif [[ $INTERNET == "unknown" ]]; then putOut "General Status:" "Unknown" echo -e $RED echo "-The connectivity status cannot be found out." echo "-Check that power is on, cables are connected and that the network is started." echo -e $STD echo "-Can not continue..." echo exit fi echo # # DHCP HOSTS=$(cat /etc/hosts | grep $(hostname) | grep -v 127.0 | awk -F \# '{ print $1 }' | awk '{ print $1 }') if [[ ! -z $HOSTS ]]; then putOut "NOT Testing DHCP" "Hostname exists in /etc/hosts" elif [[ $FIXED == false ]]; then putOut "Testing DHCP" "--" if [[ $OS == "suse" ]]; then DHCPADDR=$(dhclient -v 2>&1 >/dev/null | grep bound | awk '{ print $3 }') IRET1=$? DHCPSRVR=$(dhclient -v 2>&1 >/dev/null | grep DHCPACK | awk '{ print $3 }') IRET2=$? else DHCPADDR=$(dhclient -v 2>&1 >/dev/null | grep DHCPACK | awk '{ print $3 }') IRET1=$? DHCPSRVR=$(dhclient -v 2>&1 >/dev/null | grep DHCPACK | awk '{ print $5 }') IRET2=$? fi if [[ $IRET1 == 0 && $IRET2 == 0 ]]; then putOut "-Got address from $DHCPSRVR:" "$DHCPADDR" IPADDR=$(nmcli -g IP4.ADDRESS device show "$DEVICE" | awk -F / '{ print $1 }') if [[ $DHCPADDR == $IPADDR ]]; then putOut "--This is the currently used address" "Ok" else echo -en $RED putOut "--Another address is used:" "$IPADDR" echo "DHCPADDR: $DHCPADDR" echo "IPADDR: $IPADDR" echo -en $STD if [[ $DHCPADDR == $IPADDR ]]; then putOut "---DHCP has changed address to:" "$DHCPADDR" else echo -en $RED putOut "---DHCP could not change address" "Fail" echo echo "-To fix this, you have to execute:" echo echo " ip addr flush dev $DEVICE" echo " nmcli device connect $DEVICE" echo -en $STD fi fi else putOut "-Got no address" "Fail ($IRET1/$IRET2)" echo -e $RED echo "-Check that the DHCP server (probably $GATEWAY) is setup to handle DHCP." echo '-If hardcoded addresses is used, run this script with "-f" to skip this test' echo -e $GREEN echo "-Try with:" echo echo " dhclient -v" echo -e $STD echo "-Can not continue..." echo exit fi else putOut "NOT Testing DHCP" "-f is specified" fi echo # # GATEWAY putOut "Testing Gateway reachacility" "--" reachable $GATEWAY IRET=$? ###IRET=1 if [[ $IRET == 0 ]]; then putOut "--Gateway is responding" "Ok" else putOut "--Gateway does NOT respond" "Fail" echo -en $RED echo "--Check that power is on, cables are connected and that network is started." echo echo "--Normally in a DHCP network, both the Gateway and the DHCP server is defined" echo "--by DHCP. So check out the status of this server." echo "--DHCP server: $DHCPSRVR" echo "--Gateway: $GATEWAY" if [[ $GATEWAY != $DHCPSRVR ]]; then echo echo "--Note! that Gateway is not the same as DHCP-server. Although this is quite" echo "--possible, it is uncommon for home networks." fi echo -e $STD echo "-Can not continue..." echo exit fi echo # DNS let EXTDNS=0 let TOTDNS=0 FIRSTDNS="" DNSs=$(nmcli -g IP4.DNS connection show "$CONNECTION") #echo "DNSs: >$DNSs<" IFS=" | " for DNS in $DNSs; do let TOTDNS+=1 putOut "Testing DNS from nmcli" "$DNS" reachable $DNS IRET=$? if [[ $IRET == 0 ]]; then putOut "--$RTYPE to $DNS" "Ok" else putOut "--$RTYPE to $DNS" "Fail ($IRET)" fi putOut "-Testing nameserver function" "--" LOOKUP=$(nslookup -timeout=$TTIMEOUT "www.google,com" $DNS | wc -l) IRET=$? if [[ $IRET == 0 ]]; then let EXTDNS+=1 if [[ -z $FIRSTDNS ]]; then FIRSTDNS=$DNS fi putOut "--Nslookup via $DNS" "Ok" else putOut "--NSlookup via $DNS" "Fail" fi done # # RESOLV IFS=$STDIFS let INTDNS=0 RESOLVs=$(grep ^nameserver /etc/resolv.conf | awk '{ print $2 }') for RESOLV in $RESOLVs; do ALREADY_TESTED=false for iDNS in $DNSs; do DNS=$(echo $iDNS | xargs) if [[ $RESOLV == $DNS ]]; then ALREADY_TESTED=true break fi done if [[ $ALREADY_TESTED == false ]]; then let TOTDNS+=1 if [[ $RESOLV == "127.0.0.53" ]]; then putOut "Testing DNS from /etc/resolv.conf:" "$RESOLV (internal cache)" putOut "-No test with $RTYPE is done" "-- (not possible for this address)" else putOut "Testing DNS from /etc/resolv.conf:" "$RESOLV" if [[ ! -z $RESOLV ]]; then reachable $RESOLV IRET=$? if [[ $IRET == 0 ]]; then putOut "-$RTYPE to $RESOLV" "Ok" else putOut "-$RTYPE to $RESOLV" "Fail" fi fi fi putOut "-Testing nameserver function" "--" LOOKUP=$(nslookup -timeout=$TTIMEOUT "www.google,com" $RESOLV | wc -l) IRET=$? if [[ $IRET == 0 ]]; then if [[ $RESOLV != "127.0.0.53" ]]; then if [[ -z $FIRSTDNS ]]; then FIRSTDNS=$HOST fi let EXTDNS+=1 else let INTDNS+=1 fi putOut "--Nslookup via $RESOLV" "Ok" else putOut "--NSlookup via $RESOLV" "Fail" fi else #putOut "$RESOLV" "already tested" : fi done let WRKDNS=$EXTDNS+$INTDNS putOut "Total defined DNSs:" "$TOTDNS" putOut "Total working DNSs:" "$WRKDNS" putOut "-Working External DNSs:" "$EXTDNS" putOut "-Working Internal DNSs:" "$INTDNS" ###let EXTDNS=0 if [[ $EXTDNS -lt 1 ]]; then echo -en $RED echo "Total number of DNSs, that can translate site names are zero. This means" echo "that it is maybe possible to reach external sites via their numerical" echo "addresses, but NOT via their names." echo if [[ ! -z $DHCPADDR ]]; then echo "DHCP is working so the DNS should probably be defined there. Check the DHCP-" echo "server ($DHCPSRVR) so that everything is Ok there." echo -e $GREEN echo "To hardcode a DNS until this is fixed, you can try with:" echo echo ' nmcli con modify "'$CONNECTION'" +ipv4.dns "'8.8.8.8'"' echo echo " and then restart the connection with:" echo echo ' nmcli con down "'$CONNECTION'"' echo ' nmcli con up "'$CONNECTION'"' echo echo "This setting is then removed with:" echo echo ' nmcli con modify "'$CONNECTION'" -ipv4.dns "'8.8.8.8'"' echo echo " and a restart in the same way." echo else echo "DHCP is not used, so the DNS has to be defined in /etc/resolv.conf. Check" echo "this file and add a nameserver there." fi echo -e $STD echo "-Can not continue..." echo exit fi echo # # EXTERNAL IP which wget >> /dev/null 2>&1 WGET=$? which curl >> /dev/null 2>&1 CURL=$? #FETCHADDR="http://nst.sourceforge.net/nst/tools/ip.php" FETCHADDR="https://api.ipify.org" if [[ $WGET == 0 ]]; then LOOKUP=$(nslookup -timeout=$TTIMEOUT $FETCHADDR | wc -l) IRET=$? if [[ $IRET == 0 ]]; then EXTIP=$(wget -qO - $FETCHADDR) if [[ $? == 0 ]]; then putOut "Checking External IP with Wget" "Ok" putOut "-External IP address:" "$EXTIP" else putOut "Checking External IP with Wget" "Fail" putOut "-External IP address:" "Fail" fi else putOut "Checking External IP with Wget" "Fail" putOut "-No answer via nslookup" "Fail" fi elif [[ $CURL == 0 ]]; then LOOKUP=$(nslookup -timeout=$TTIMEOUT $FETCHADDR | wc -l) IRET=$? if [[ $IRET == 0 ]]; then EXTIP=$(curl -s $FETCHADDR) if [[ $? == 0 ]]; then putOut "Checking External IP with Curl" "Ok" putOut "-External IP address:" "$EXTIP" else putOut "Checking External IP with Curl" "Fail" putOut "-External IP address:" "--" fi fi else putOut "Checking External IP" "Fail" echo -en $RED echo "Neither Wget or Curl is installed, install one of them to check this..." echo -en $STD fi echo fi # # SPEED if [[ $SPEED == true ]]; then #putOut "Checking speed - this may take a while..." " " INTERNET=$(nmcli networking connectivity) echo "Checking speed - this may take a while..." which speedtest-cli >> /dev/null 2>&1 SPEEDTEST=$? #if [[ ! -z $(which speedtest-cli) ]]; then if [[ $INTERNET == "full" ]]; then if [[ $SPEEDTEST == 0 ]]; then UPLOAD=$(speedtest-cli --no-download | grep "^Upload" | awk -F : '{ print $2 }' | xargs) DOWNLOAD=$(speedtest-cli --no-upload | grep "^Download" | awk -F : '{ print $2 }' | xargs) putOut "-Upload:" "$UPLOAD" putOut "-Download:" $DOWNLOAD else putOut "-Not installed:" "speedtest-cli" echo -en $RED echo "speedtest-cli must be installed to check the connection speed..." echo -en $STD fi else echo -en $RED echo "-Can not test speed, Internet is not available" echo -en $STD fi echo fi # exit ![]() |
Sida 1 av 1 | Alla tidsangivelser är UTC + 1 timme [ Sommartid ] |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |