Results 1 to 6 of 6

Thread: Running root level networking commands from a script

  1. #1

    Default Running root level networking commands from a script

    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
    1) echo "IP=SUCCESS"
    ;;
    0) 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
    1) echo "IP=SUCCESS"
    ;;
    0) 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
    1) echo "DNS=REACHABLE"
    ;;
    0) 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---

  2. #2

    Default Re: Running root level networking commands from a script

    Quote Originally Posted by markeriv View Post
    /home/mark/bin/ifscript.sh: line 13: route: command not found
    ...
    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).
    ifconfig and route are located in /sbin/ which is not in the user's path by default.
    Just call them with the full path name, i.e. replace all occurences of "ifconfig" with "/sbin/ifconfig" and "route" with "/sbin/route" in your script.

    AFAICS your script should run then with user permissions.

  3. #3
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,818

    Default Re: Running root level networking commands from a script

    Here we go again (not your fault, it is hidden somewhere).

    Please use CODE tags around your copied/pasted computer texts. You get the CODE tgas by clicking on the # button in the tool bar of the post editor. And please copy prompt, command, output, next prompt all in one sweep (you partly did that already), The no explantions like "Below is the ls -l" is needed, because it ius already there.
    Henk van Velden

  4. #4

    Default Re: Running root level networking commands from a script

    Better yet, use the shoter, easier, consolidated commands that weren't
    deprecated a decade ago (from the ifconfig manpage: "WARNING: Ifconfig is
    obsolete on system with Linux kernel newer than 2.0. On this system you
    should use ip. See the ip manual page for details"):

    Code:
    --------------------
    ip addr
    ip route
    --------------------

    No more need for the full path, shorter typing, ongoing development, can
    type 'ip' with one hand.... all good things.

    --
    Good luck.

    If you find this post helpful and are logged into the web interface,
    show your appreciation and click on the star below...

  5. #5

    Default Re: Running root level networking commands from a script

    Thanks all!
    I managed to get it working and am now digging through the ip man page.
    I will use the tags in the future and didn't know they existed. Neat feature...

  6. #6
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,818

    Default Re: Running root level networking commands from a script

    Quote Originally Posted by markeriv View Post
    I will use the tags in the future and didn't know they existed. Neat feature...
    That is not your fault. It is rather hidden. Thus we have to explain to every one again and again
    Henk van Velden

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •