validating(pingable or not) remote ip address in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting validating(pingable or not) remote ip address in shell script
# 1  
Old 01-23-2012
validating(pingable or not) remote ip address in shell script

i need to verify whether the ip adress given as input to the shell script is pingable or not... that is whether the ip is alive and responding..
Code:
 
ping $ip_adress

the above wont work in script because the execution is continuous... so the shell script keeps will dwell in this pinging process... i need one shot command to check whether the remote ip is alive or not...
# 2  
Old 01-23-2012
Try this:
Code:
ping -c1 $ip_address >> /dev/null
[ $? -eq 1 ] && echo "$ip_address may not be alive


Last edited by balajesuri; 01-23-2012 at 08:35 AM..
This User Gave Thanks to balajesuri For This Post:
# 3  
Old 01-23-2012
thanks you code works... i used the below
Code:
ping -c1 10.19.123.104 | grep "Unreachable"

if output is nil then pingable else not... but the thing is its consuming time about 1 to 2 seconds... is there anyway we could decrease time consumed..
# 4  
Old 01-23-2012
I don't think so.
# 5  
Old 01-23-2012
oh okay... one more thing... i need to validate ip... i referred few threads in unix..

Code:
 #echo 10.19.123.250 |awk -F"." '$1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ' | wc -l
1
# echo 10.19.123.256 |awk -F"." '$1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ' | wc -l
0
# echo 10.19.123.250.123 |awk -F"." '$1 <=255 && $2 <= 255 && $3 <= 255 && $4 <= 255 ' | wc -l
1

the above awk code doesnt validate more characters... i tried to validate by finding no of '.' in the ip.. but grep -cF "." doesnt work too... is there anyway to count no of dots in an ip using grep..?
# 6  
Old 01-23-2012
Validate IP based on what? Your awk statements have a condition but no action.
# 7  
Old 01-23-2012
the awk is validating whether the numbers in ip doesnt exceed 255.. its doing that.. if we get 0 then invalid ip(ip having a number greater than 255) and if 1 then its a valid ip... (the 0's and 1's shown in above code is actual output)

now i found out how to count number of '.' in an ip
i used
Code:
 
 echo 10.12.12.11 | awk '{ split($0,a,"."); print length(a) - 1 }'

it worked :-)
if the number obtained is not equal to 3 then its invalid ip... thanks for helping in ip ping
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to delete the ip address from files

Hello, I am new to shell scripting, need help, my requirement is to delete the ip address from serveral files, please suggest (2 Replies)
Discussion started by: manoj.solaris
2 Replies

2. Red Hat

How to replace Ip address in .xml file through shell script?

I have one .xml file. which contains the following line. <ParamString StringId="PortAddress" StringValue="172.27.166.170" /> <ParamString StringId="PortAddress" StringValue="172.27.166.171" /> <ParamString StringId="PortAddress" StringValue="172.27.166.202" /> <ParamString... (9 Replies)
Discussion started by: Anjan Ganguly
9 Replies

3. Shell Programming and Scripting

To run a local shell script in a remote machine by passing arguments to the local shell script

I need to run a local shell script on a remote machine. I am able to achieve that by executing the command > ssh -qtt user@host < test.sh However, when I try to pass arguments to test.sh it fails. Any pointers would be appreciated. (7 Replies)
Discussion started by: Sree10
7 Replies

4. Shell Programming and Scripting

Triggering remote UNIX shell script from Remote desktop

I m trying to run a batch script in remote desktop which executes unix commands on the unix server...the problem is i wnt the output in HTML format.so in my batch script i m giving the cmd like ssh hostname path ksh HC_Report.ksh>out.html ...but it generates the HTML file in remote desktop .i... (2 Replies)
Discussion started by: navsan
2 Replies

5. Shell Programming and Scripting

Validating month value in shell script

Hi, the below script is to get & check the correct values for minutes (0-59) that is inputed by user : printf "$FBOLD\nPlease enter the minutes (0-59): $FREG" read MIN case "$MIN" in |) break 2;; *) echo "" echo "Invalid minutes, please... (4 Replies)
Discussion started by: milink
4 Replies

6. Shell Programming and Scripting

Shell script for validating fields in a file

Hi, I have not used Unix in a very long time and I am very rusty. I would appreciate any help I can get from the more experienced and experts in Shell script. I am reading one file at a time from a folder. The file is a flat file with no delimeters or carriage return. Col1 through col6 is... (5 Replies)
Discussion started by: asemota
5 Replies

7. Shell Programming and Scripting

EMail Address Validation (Shell Script)

Hi, Can anyone provide me the code snippet for EMail Address Validation. User is going to enter the email address in form window. I need to validate the format is correct. Thanks in Advance BS (3 Replies)
Discussion started by: balajiora
3 Replies

8. Shell Programming and Scripting

need to check whether a sever is pingable or not inside the script

Hi, need to write a script which will check number of ip address are able to ping or not .. (2 Replies)
Discussion started by: mail2sant
2 Replies

9. Shell Programming and Scripting

Validating IP address.

Hi All, This is a small snippet that I am using to validate the format of IP address.This is working fine. Since this is not elegant, please let me know if anyone has a better working code than the one below. Also I am planning to ping the ip address given by the user and check the $?... (3 Replies)
Discussion started by: nua7
3 Replies

10. Shell Programming and Scripting

validating 10. IP address any way possible

HELP: validating IP addresses any way possible -------------------------------------------------------------------------------- I am trying to validate input from the user in a script. I thought is was easy to do using regular expressions but I can't figure out how to use REs in a... (1 Reply)
Discussion started by: nrodolfich
1 Replies
Login or Register to Ask a Question