How to check if the variable contains a valid IP address

advertisements

I am writing a shell script program in which I am taking IP address or system name from the user, can anybody tell me how to check if the given IP address or system name is valid. I think ping will not work, I tried it but I can't come out of it, so is there some other method.


Try this, hope it wi

#! /bin/sh

valid_ip()
{
  # Check if IP format is num.num.num.num / num between 0..255
  if [ "$(sipcalc $1 | grep ERR)" != "" ]; then
    echo "incorrect"
    return 1
  fi
  echo "correct"
  return 0
}