Visit Our UNIX and Linux User Community


Check connectivity script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Check connectivity script
# 1  
Old 10-22-2009
Check connectivity script

This past weekend I had some issues with my ISP.
So for future purpose I'm going to have some logging on my internet so I'm able to attach log files to my complaint email if this issue reoccurs.

Decided to do a simple ping script that runs every 5 or 10 min with crontab
if ping fail write date followed by ping result to pinglog.txt

Everything works except geting ping result into pinglog.txt
Script just prints to terminal.
I have tested the line in terminal and then result prints to pinglog.txt.

Script and pinglog.txt are both in /home/seb/bin
date gets printed into pinglog.txt
123google.com is intended for if to be true
Code:
#!/bin/bash

targetHost="123google.com"

/bin/ping -c 1 $targetHost
if [ $? != 0 ]
        then
echo    "Internet: DOWN `date`" >> pinglog.txt

        /bin/ping -c 1 $targetHost >> pinglog.txt

else
        echo "Internet: OK"
fi



---------- Post updated at 03:52 ---------- Previous update was at 03:42 ----------

nvm found my error

Code:
/bin/ping -c 1 $targetHost 2>> /home/seb/bin/pinglog.txt

# 2  
Old 10-22-2009
To be more clear, the problem could have solved by 2 things.

1. Redirection of stderror:
As in error situation we need to filter the stderr as 2.

2. Absolute path for output file.
We need to give the absolute path for redirection, especially in batch processes.
# 3  
Old 10-22-2009
As I'm writing this I'm currently building the script. I'm at the office so it's not going so fast since I get interrupted all the time.
First version was just to get everything working in my bash terminal. When I'm done there and the pinglog.txt looks good. Then I'm editing it for cron.


Just finished adding absolute path's for all commands.
# 4  
Old 10-22-2009
Maybe using nmap to check a webserver's port etc. is up instead of just ping'ing it might be also helpful (no idea if ping check is sufficient - just as an idea to add).
# 5  
Old 10-22-2009
Quote:
Originally Posted by zaxxon
Maybe using nmap to check a webserver's port etc. is up instead of just ping'ing it might be also helpful (no idea if ping check is sufficient - just as an idea to add).
PING might not be helpful all the time, site might be up but replying back to control message protocol might be denied, in that case ping will not get a reply
# 6  
Old 11-04-2009
I decided to remake the script so its less lines..
Right now It looks like this

Code:
#!/bin/bash

targetHost="
www.123sunet.se
www.123arla.se
www.123dn.se
"
count=0

for x in $targetHost
do
	if  /bin/ping -c1 -w1 $x $1>/dev/null 
	then
	echo "ok">/dev/null
	else
		let ++count 		

	fi
done
 
echo $count
if [ $count -eq 3 ]
then
	for x in $targetHost
	do
		/bin/echo "Internet: DOWN `date` $x" >> /home/seb/bin/pinglog.txt
		/bin/ping -c 1 $x 12>> /home/seb/bin/pinglog.txt
		/bin/echo "--------------------------------------------" >> /home/seb/bin/pinglog.txt
	done
else
	echo "Do nothing everything works"
fi

Here is pinglog.txt
Code:
seb@seb-VM:~/bin$ cat pinglog.txt 
Internet: DOWN Wed Nov  4 20:27:02 CET 2009 www.123sunet.se
--------------------------------------------
Internet: DOWN Wed Nov  4 20:27:02 CET 2009 www.123arla.se
--------------------------------------------
Internet: DOWN Wed Nov  4 20:27:02 CET 2009 www.123dn.se
--------------------------------------------
seb@seb-VM:~/bin$


Last edited by chipmunken; 11-04-2009 at 03:30 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #750
Difficulty: Medium
The IBM Personal Computer/AT (model 5170), announced on August 15, 1984.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Check connectivity with multiple hosts - BASH script available here

Hi everyone! Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....). I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl). It worked for me so I'm sharing it... (9 Replies)
Discussion started by: Fr3dY
9 Replies

2. Shell Programming and Scripting

Help with shell script to check the tcp network connectivity between server

Hello, I have a requirement to check the tcp network connectivity between server it's running on and the list of host's and ports combination. i have written the below code but it doesn't work, but when i execute the nc command outside the script it works fine. please help me where i am... (8 Replies)
Discussion started by: sknovice
8 Replies

3. Shell Programming and Scripting

Check the connectivity of the DB through script, exit if no connection

check the connectivity of the DBs through script, script should exit if no connection and display the output as below. connectivity for DB1 is OK connectivity for DB2 is OK connectivity for DB3 is FAILED for DB in 1 2 3 do (sqlplus -s... (5 Replies)
Discussion started by: only4satish
5 Replies

4. Solaris

Sybase Connectivity Check through Shell Script

Hi, I need to check the sysbase database connectivity through the Unix Shell Script. Can you help me on the how to write the script to test the sysbase database connection. Thanks in Advance Nandha (0 Replies)
Discussion started by: nandha2387
0 Replies

5. Shell Programming and Scripting

Check the Sybase connectivity

Hi , I am able to connect to sybase using the below script ,but i want to check the status of the connectivity like if there is any network failure during downloading the file ,it will come out with exit 1 status .Any help on this ? # isql database_name db_user_name <<EOF select date_val from... (0 Replies)
Discussion started by: mohan705
0 Replies

6. IP Networking

Connectivity check.

How to check connectivity with an external server if I know the IP. Please note traceroute is not working for me, as it is saying command not found. (6 Replies)
Discussion started by: nixhead
6 Replies

7. IP Networking

Need to check the connectivity between 2 servers

Hi All, I need an automation script to check the connectivity between 2 UNIX servers. Could anybody please help in this regards? regards, janardhan (1 Reply)
Discussion started by: rjanardhan83
1 Replies

8. Shell Programming and Scripting

Linux: Writing a tricky script to check connectivity

So, first and foremost, I'm having issues with my internet connection. Periodically, the connection drops across the network. The fix is simple enough: restart the modem. However, this gets old when the connection dies out every hour. I can hit my surfboard on 192.168.100.1, and navigate to a... (5 Replies)
Discussion started by: kungfujoe
5 Replies

9. Shell Programming and Scripting

Script to check connectivity

I want to write a script to check if a unix box say abc.tdc.cin.net can be connected or not on certain port say 22. right know i have to telnet them manually from DOS prompt and if it is successful then isay it is connected. Also to check Database connectivity I am using tnsping From DOS prompt.... (3 Replies)
Discussion started by: kukretiabhi13
3 Replies

10. Solaris

How to check Oracle odbc connectivity in solaris ?

Hi, I have my application and database servers running on solaris. I need to check Db connectivity for a datasource in my Db server. Please guide me how to do it in solaris env. Aru (2 Replies)
Discussion started by: aarora_98
2 Replies

Featured Tech Videos