Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

script about ping, telnet, traceroute 2

Status
Not open for further replies.

peac3

Technical User
Jan 17, 2009
226
AU
Hi guys,

Im building the script to do ping, telnet and trace route.

I know about those commands work and just trying to get the idea from you guys about the nicest way to do it.

1. about ping :
you know the output is number of iterations, is there anyway to make the output like "ping is alive", how do you guys usually do it by how many iteration to make sure that its alive.


2. telnet, the output is like this :
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.1

How to make the output is like "telnet to port ## is alive"

3. I think I dont need to ask about trace route.

Any input will be much appreciated.

Thank you,
 
Most ping implementations have an option to send only a specific number of packets. It's also useful to reduce the wait time if you need the script to run quickly. For example, on HP-UX, ping hostname -n1 -m3 will send one ping and only wait 3 seconds for a reply. You can then act based on the return code, e.g.

Code:
if ping hostname -n1 -m3 > /dev/null
then
    echo hostname is alive

Unfortunately ping implemenations vary a lot between different flavours of Unix, so if your script needs to be portable then you will need to handle that.

For me 1 iteration is usually enough.

For telnet you can often do something like:

Code:
if telnet hostname 80 </dev/null 2>&1 | grep -q Connected
then
    echo telnet to port 80 is alive
fi

However this may not work in all cases, e.g. on HP-UX the telnet process hangs waiting for input.

Annihilannic.
 
Hi Anni,

The telnet works in my OS redhat by checking "Connected"

but the ping, which message do we check to ensure it's connected? the message are similar between connected and not connected. The difference only packet loss as far as I can see.

Are we always looking for 0% packet loss?

in REdhat:
I used -c [count Echo request packet] and -w [timeout]

If connection not connected
Code:
$ ping 192.168.1.120 -c1 -w1
PING 192.168.1.120 (192.168.1.120) 56(84) bytes of data.

--- 192.168.1.120 ping statistics ---
1 packets transmitted, 0 received, 100% packet loss, time 1000ms

If connection connected
Code:
$ ping 192.168.1.2 -c1 -w1
PING 192.168.1.2 (192.168.1.2) 56(84) bytes of data.
64 bytes from 192.168.1.2: icmp_seq=1 ttl=64 time=0.062 ms

--- 192.168.1.2 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.062/0.062/0.062/0.000 ms

Thank you,
 
If you want to guarantee a sucessful connection, you would always be looking for 0% packet loss I would have thought.

The internet - allowing those who don't know what they're talking about to have their say.
 
Hi Anni/Ken

I was just trying to understand this command
Code:
telnet hostname 80 </dev/null 2>&1

I thought /dev/null is a black hole that we usually throw something into (ie 2>/dev/null, throwing standard error into black hole)
not redirect the black hole into the statement.

Can someone explain why we need /dev/null into the stattement?

Thanks.
 
In this instance the < /dev/null is acting essentally as an empty input file, so that no user intervention is required to get the response from the host being contacted.

The internet - allowing those who don't know what they're talking about to have their say.
 
What with 'sucessful' and 'essentally' in my last two posts, I seem to have lost the ability to spell over the weekend!

The internet - allowing those who don't know what they're talking about to have their say.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top