1
 Welcome to the IP Check Program!! Please input the ip address: 300.500
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 pingips.sh: line 19: [: echo $ip | cut -d. -f$i: integer expression expected
 IP address is alive
 Thank you for using the IP Check Program. \nPress 0 to exit and any other number to 
  continue

could someone please help me out? this is the error displayed. i guess why the program is messed up. because "300.500" is obviously not a valid ip address but it reads it as alive. PLEASE HELP! My code is below

#!/bin/bash
rs=1
while [ $rs -gt 0 ]
do
    clear
#greet the user and request input
    echo -n "Welcome to the IP Check Program!! Please input the ip address: "
read ip
#check user's input is IP address format
isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
isIP=$?
if [ $isIP -eq 0 ];
then
    isValid=1

    for i in 1 2 3 4
    do
            n='echo $ip | cut -d. -f$i'
            if [ "$n" -gt 255 -o $n -lt 0 ];
            then
                    echo "INPUT ERROR. IP ADDRESS DIGITS SHOULD BE BETWEEN 0 AND 255 
ONLY"
                    isValid=0
            fi
    done
    if [ "$isValid" -eq 1 ];
    then
            echo "IP address is alive"
    fi
  else
    echo "IP address is not responding"
fi
#greet user after using program
echo -n "Thank you for using the IP Check Program. Press 0 to exit and any other number 
to continue"
read rs
cschneid
  • 10,237
  • 1
  • 28
  • 39
  • 2
    `n='echo ...'` sets `n` to the *string* "echo ...", not the output of that as a command; you want `n=$(echo ...)` (see ["How do I set a variable to the output of a command in Bash?"](https://stackoverflow.com/questions/4651437/how-do-i-set-a-variable-to-the-output-of-a-command-in-bash)). Also, `expr` is ancient and flaky; use `[[ "$var" =~ regex ]]` instead (see [BashFAQ #31: "What is the difference between test, `[` and `[[` ?"](http://mywiki.wooledge.org/BashFAQ/031)). Finally: run your script through [shellcheck.net](https://www.shellcheck.net) to look for other common mistakes. – Gordon Davisson Apr 11 '22 at 02:04

2 Answers2

0

@isioma-udobi , you need to improve your script. It has several basic problems that must be solved before you can make it work. What Gordon told you should help a lot, if you read what is pointed. And I reformated your code, made a few changes and added some didactic comments to it. Take a look, and see if you can progress with your script:

#!/bin/bash
# What is the point of this rs variable?? Give a better name to it
rs=1
while [ $rs -gt 0 ]

# where is the corresponding "done"???
do

    clear

    # greet the user and request input
    echo -n "Welcome to the IP Check Program!!"\
    "Please input the ip address: "
    read ip

    # check user's input is IP address format
    # Rewrite the following 2 lines as Gordon suggested
    #isIP="expr match $io'[0-9]\+\.[]0-9\+\.[0-9]\+\.[0-9]\+";
    #isIP=$?
    
    # This line will also change with the last 2
    if [ $isIP -eq 0 ];
    then
    isValid=1

    # Test if each part of the IPv4 is valid
    for i in 1 2 3 4
    do
        # In this line, you are simply creating a string
        # which looks like the command 'echo' wrinting
        # into the standard input of a 'cut'.
        n='echo $ip | cut -d. -f$i'

        # Check if the IPv4 part is in the valid
        # interval
        if [ "$n" -gt 255 -o $n -lt 0 ];
        then
        echo "INPUT ERROR. IP ADDRESS DIGITS"\
            "SHOULD BE BETWEEN 0 AND 255 ONLY"
        # Once a part is invalid, the others do
        # not need to be tested. You can test
        # them, though. This code do not break
        # the result
        isValid=0
        fi
    done

    # Is the whole IP is valid?
    if [ "$isValid" -eq 1 ];
    then
        echo "IP address is alive"
    fi
    else
    # echo "IP address is not ??responding??"
    echo "The given IPv4 address is not valid"
    fi
    # greet user after using program
    echo -n "Thank you for using the IP Check Program."\
    "Press 0 to exit and any other number to continue"
    read rs
Balaco
  • 12
  • 1
  • 8
0

I would use nmap for this instead of writing a custom shell script. You can scan ranges of IP's like this:

nmap -sn 123.123.123.1-255

or give it a list of hosts in a file:

nmap -iL input_file_name

more info in the man page.

Z4-tier
  • 7,287
  • 3
  • 26
  • 42