GIDForums  

Go Back   GIDForums > Computer Forums > Computer Software Forum - Linux
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 28-Oct-2011, 04:33
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 1,049
admin will become famous soon enough

BASH to test if local IP address or hostname is up


I know how to find out if a LAN IP address e.g., 192.168.1.9, is up with just a few lines of PHP but now I want something similar in BASH.

I haven't really gone looking for the information myself but I am sure someone here can write this without even blinking.

I just want a function (preferably) that accepts an argument i.e. an IP address or a hostname, and checks to see that it is connected, returning an integer if invalid or offline.

Thank you.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #2  
Old 29-Oct-2011, 12:32
TurboPT's Avatar
TurboPT TurboPT is offline
Senior Member
 
Join Date: Feb 2006
Location: Atlanta, GA
Posts: 1,511
TurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the roughTurboPT is a jewel in the rough

Re: BASH to test if local IP address or hostname is up


Are you trying to do something more than what the ping command can do...would that not be the same task without having a script?
__________________
Use the force...read the source!!
WYCIWYG -- what you code is what you get!
  #3  
Old 29-Oct-2011, 21:53
admin's Avatar
admin admin is offline
Administrator
 
Join Date: Sep 2002
Posts: 1,049
admin will become famous soon enough

Re: BASH to test if local IP address or hostname is up


Thank you, TurboPT. Yes, that is how I was doing it with PHP too. I just didn't know how to create a function in BASH -- but now I do.

Here is my FIRST BASH function. I saved it to a file: functions.sh, and plan to use it in many other scripts that I will write.

Code:
# Tests an IP Address (IPv4) or hostname to see if it is connected. # Exit code 0 means online. # Exit code 2 means offline or host is invalid. function is_address_alive() { ping -c 1 $1 > /dev/null 2>&1 }

See the little example below if you would like to review/correct my work.

In my incomplete script to auto mount a SMB share, I will use that function like this [onlogin.mount.nasty.sh]:

Code:
#!/bin/bash # The host name of the NAS. address="nasty" # Include some common functions. source /home/jayantha/workspace/php/common/bash/functions.sh # Function to test if address alive. Exit code 2 for dead address. is_address_alive $address if [ $? -eq 0 ]; then # This is as far as I've got. :) fi

Now I have to figure out the best/easiest way to mount the Network Attached Storage (NAS). I want it to be automatically mounted for me, and manually for everybody else.
__________________
Custom BB codes you can use here:
[HTML] | [C++] | [CSS] | [JAVA] | [PY] | [VB]
  #4  
Old 26-Jun-2017, 18:25
ChristianC123 ChristianC123 is offline
New Member
 
Join Date: Jun 2017
Posts: 5
ChristianC123 is on a distinguished road

Re: BASH to test if local IP address or hostname is up


You can use ping or fping (advanced)

ping -c 1 some_ip_here &> /dev/null

is host reachable it will return 0 or 1

Regards
Christian
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Need help with understanding pointers prattcmp C++ Forum 16 23-Apr-2011 10:07
[Tutorial] How to hide e-mail address from SPAM Bots BobbyDouglas Web Design Forum 40 13-Feb-2006 10:44
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 15:13
CSS Layout question oihjk Web Design Forum 3 28-May-2003 11:36

Network Sites: GIDNetwork · GIDApp · GIDBlog · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 08:31.


vBulletin, Copyright © 2000 - 2017, Jelsoft Enterprises Ltd.