So I am both new to Linux and bash scripting and am attempting to make a script that checks network settings and connectivity in a new bash shell window when a non-root user logs on.
Currently I am running into an issue where the script will run but have errors if I do not run the script as root. I know this means I have a permissions issue and am trying to figure out the minimum required permissions to run the script without error.
Setting the SUID does not resolve the issue.
Thanks in advance for looking at this.
Below is the ls -l
-rwsr-xr-x 1 root root 1367 Jan 17 08:46 ifscript.sh*
**Here is the output of the script when run as a normal user. **
mark@linux-ika4:~/bin> ifscript.sh
VERIFYING NETWORKING SERVICE
Network Service: ON
/home/mark/bin/ifscript.sh: line 13: route: command not found
TESTING LOCAL IP CONNECTIVITY
Usage: ping -LRUbdfnqrvVaAD] -c count] -i interval] -w deadline]
-p pattern] -s packetsize] -t ttl] -I interface]
-M pmtudisc-hint] -m mark] -S sndbuf]
-T tstamp-options] -Q tos] [hop1 …] destination
Ping Error
TESTING REMOTE IP CONNECTIVITY
IP=SUCCESS
TESTING DNS AVAILABILITY
DNS=REACHABLE
TESTING DNS - RESOLVING WWW.MSN.COM
Address: 131.253.13.140
DISPLAYING NETWORK INFO
Default Gateway:
Primary DNS: 172.25.80.20
/home/mark/bin/ifscript.sh: line 56: ifconfig: command not found
Interface: eth0
/home/mark/bin/ifscript.sh: line 63: ifconfig: command not found
mark@linux-ika4:~/bin> ifconfig
Absolute path to ‘ifconfig’ is ‘/sbin/ifconfig’, so running it may require superuser privileges (eg. root).
Here is the output when run as root.
linux-ika4:/home/mark/bin # ifscript.sh
VERIFYING NETWORKING SERVICE
Network Service: ON
TESTING LOCAL IP CONNECTIVITY
IP=SUCCESS
TESTING REMOTE IP CONNECTIVITY
IP=SUCCESS
TESTING DNS AVAILABILITY
DNS=REACHABLE
TESTING DNS - RESOLVING WWW.MSN.COM
Address: 131.253.13.140
DISPLAYING NETWORK INFO
Default Gateway: 10.0.2.2
Primary DNS: 172.25.80.20
Interface: enp0s3
inet addr:10.0.2.15 Bcast:10.0.2.255 Mask:255.255.255.0
**
Here is the script.**
#!/bin/bash
#runs networking test commands
echo VERIFYING NETWORKING SERVICE
CHKCFG=$(chkconfig network)
case $CHKCFG in
‘network on’) echo Network Service: ON
;;
‘network off’) echo Network Service: OFF
;;
*) echo No response to chkconfig network
;;
esac
DG=$(route | head -n 3 | tail -n 1 | cut -d ’ ’ -f10)
echo
echo TESTING LOCAL IP CONNECTIVITY
ONE=$(ping $DG -c 1 | grep packets | cut -d ’ ’ -f 4)
case $ONE in
- echo “IP=SUCCESS”
;; - echo “IP=FAILED”
;;
*) echo “Ping Error”
;;
esac
echo
echo TESTING REMOTE IP CONNECTIVITY
ONE=$(ping 8.8.8.8 -c 1 | grep packets | cut -d ’ ’ -f 4)
case $ONE in - echo “IP=SUCCESS”
;; - echo “IP=FAILED”
;;
*) echo “Ping Error”
;;
esac
echo
echo TESTING DNS AVAILABILITY
DNS=$(cat /etc/resolv.conf | grep nameserver | head -n 1 | cut -d ’ ’ -f2)
ONE=$(ping $DNS -c 1 | grep packets | cut -d ’ ’ -f 4)
case $ONE in - echo “DNS=REACHABLE”
;; - echo “DNS=FAILED”
;;
*) echo “Ping Error”
;;
esac
echo
echo TESTING DNS - RESOLVING WWW.MSN.COM
nslookup www.msn.com $DNS | grep Address:| tail -n 1
echo
echo DISPLAYING NETWORK INFO
echo Default Gateway: $DG
echo Primary DNS: $DNS
INTER=$(ifconfig | grep enp0s3)
if -n “$INTER” ];
then
echo “Interface: enp0s3”
ifconfig enp0s3 | grep "inet "
else
echo “Interface: eth0”
ifconfig eth0 | grep "inet "
fi
exit 0
**
—end of script—**