LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2010, 01:54 PM   #1
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Rep: Reputation: 15
How to Determine if Networking is on?


This sounds like a dumb question, but I'm writing some Perl scripts that need to know whether networking is on. Is there an easy and quick way to do that, and do that in a general way that will work for many different Linux machines?
 
Old 02-11-2010, 02:01 PM   #2
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
ping gateway, if exists any.

Last edited by nimnull22; 02-11-2010 at 02:05 PM.
 
Old 02-11-2010, 03:00 PM   #3
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Original Poster
Rep: Reputation: 15
I agree that this would work, but the problem is that you'd have to wait a few seconds for a ping to return. Is there a way to determine if networking is working in a much faster way? Simply check the state of some environment variable, or parse ipconfig output, or /proc/net files?

Also, how can I determine, in an automated way, the IP address of the Gateway?
 
Old 02-11-2010, 04:02 PM   #4
tredegar
LQ 5k Club
 
Registered: May 2003
Location: London, UK
Distribution: Fedora38
Posts: 6,147

Rep: Reputation: 435Reputation: 435Reputation: 435Reputation: 435Reputation: 435
Quote:
I agree that this would work, but the problem is that you'd have to wait a few seconds for a ping to return.
Code:
tred@lt:~$ ping -c 1 gateway
PING gateway (10.0.0.2) 56(84) bytes of data.
64 bytes from gateway: icmp_seq=1 ttl=255 time=2.31 ms
That's about 1% of the time it takes you to blink. So, what's the problem?
Quote:
Also, how can I determine, in an automated way, the IP address of the Gateway?
That would probably be distro-specific.

Perhaps if you told us a little more about what you are trying to achieve, and why, we could point you in better direction(s) to help you find your answer(s).
 
Old 02-11-2010, 04:48 PM   #5
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Original Poster
Rep: Reputation: 15
Okay, 2 ms is definitely fast enough. That will work. I just need to figure out how to automatically determine my gateway IP on a RedHat/Fedora distribution.

Did you manually determine your gateway IP and put that in /etc/hosts, or something similar? Is that how ping knows what "gateway" is for you?
 
Old 02-11-2010, 05:02 PM   #6
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
"route -n" has it in default route, if not - network was not set up.

Last edited by nimnull22; 02-11-2010 at 05:03 PM.
 
Old 02-11-2010, 05:02 PM   #7
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Original Poster
Rep: Reputation: 15
Actually, it's only 2 ms if the network is running, and the gateway is 2 ms away. If your network cable is disconnected, then the ping command hangs for about 5 seconds.

Still looking for an answer....
 
Old 02-11-2010, 05:04 PM   #8
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Look please my previous post
 
Old 02-11-2010, 05:12 PM   #9
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Original Poster
Rep: Reputation: 15
Your previous posts answers how to determine the gateway IP. Thank you.

But the problem is that this method takes about 5 seconds to determine whether or not the network is up (in the case where the network isn't up). Try it. Disconnect you ethernet cable, then run "ping -c 1 gateway". I'd like a method that determines fairly quickly, as in tenths of a second, whether or not the network interface is on, and the machine can talk to the rest of the world.
 
Old 02-11-2010, 05:25 PM   #10
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
Are you from India?

If you will not receive response from GW with in < 1 sec, network DOESN'T work, it is not necessary to wait.
 
Old 02-11-2010, 05:34 PM   #11
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Original Poster
Rep: Reputation: 15
Try reading the original question. These are Perl scripts that will be running the ping command, not human beings. They will wait until ping finishes even if they don't receive the response from the GW within a second.

The alternative would be to fork another process and then check the output of that process repeatedly, and terminate after a few tenths of a second. That's certainly possible, but it seems like a lot of effort just to determine whether or not the network is connected.
 
Old 02-11-2010, 05:55 PM   #12
nimnull22
Senior Member
 
Registered: Jul 2009
Distribution: OpenSuse 11.1, Fedora 14, Ubuntu 12.04/12.10, FreeBSD 9.0
Posts: 1,571

Rep: Reputation: 92
If you want to find out if computer REALLY can use network, script needs to ping GW and waits for result only within 1~1,5 seconds, by this result script can determine if a network is or isn't available.

If one want to just make sure that ethernet cable is connected to something, you can try to check output of "ethtool" and "Link detected: yes/no". Ethtool should be in most linux distributions.

Last edited by nimnull22; 02-11-2010 at 05:57 PM.
 
Old 02-11-2010, 06:41 PM   #13
sysbox
Member
 
Registered: Jul 2005
Posts: 117

Original Poster
Rep: Reputation: 15
ethtool sounds like what I'm looking for. Thanks.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Permissions on script sudo ln -s ../init.d/start-networking S90start-networking cnolasco Ubuntu 3 04-03-2007 08:27 AM
How do i determine my IP address? How do i determine my host name? jwymore Linux - Networking 5 02-07-2007 09:57 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 10:16 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration