LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 02-13-2011, 03:21 AM   #1
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Rep: Reputation: 3
bash script that read from file and ping remote host


I am posting this for google to find and anyone who need it

Q=What will this do?
A=This will read from /root/Desktop/iplist (or anywhere you want to put the list) after it reads the ip it will ping the ip two times. If the remote host is up then it will write the ip to a log. If the ping fails it will display it on the screen, write the ip to syslog so that you can do remote logging, and last it will send the ip by email to a user or a group of users.
Code:
#!/bin/bash
#wrote by Zach Underwood feb-12-2011
#test and wrote on centos 5.5 with sendmail
rm -f /root/Desktop/iplog #This will remove the good iplog
exec 9</root/Desktop/iplist #put full path the file where ip list is. Make sure that the ips are listed one per line.
while read -u9 ip
do
   ping -c 2 $ip > /dev/null 2> /dev/null  # ping and discard output
    if [ $? -eq 0 ]; then  # check the exit code
        echo "$ip is up" >> /root/Desktop/iplog #write  the ip of the hosts that are up
        
    else
        echo "$ip is down" # will show on the screen the ips that are not up
        logger -p auth.notice "$ip is down" # will write the down host to syslog
        echo "$ip is down" | mail -v -s alert user@anydomain.com > /dev/null 2> /dev/null #will send mail users about host down
    fi
done
 
Old 02-13-2011, 04:24 AM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
Quote:
Originally Posted by zunder1990 View Post
it will send the ip by email
Email isn't that efficient as it requires a human to respond. Besides that ICMP errors may be transient and also tell you nothing about the actual state of the connection. For instance a remote host may decide to (wrongly) block all ICMP but allow TCP/80. (Also see 'hping' and 'tcptraceroute'.) So unless you depend on the remote host and can correct errors there and 0) watch email constantly to take action or 1) have remote mail set up to respond with some action automagically, sending email is a waste of resources IMHO.


Quote:
Originally Posted by zunder1990 View Post
I am posting this for google to find
So you really think you're the first one to write this type of script? Did you search LQ? Anyway. Since you posted it, thanks, but it could do with some minor improvements. Here's me reciprocating:
Code:
#!/bin/bash
# Who am I?
PROGN="${0//*\//}"

# help function
_help() { echo "${PROGN}: /path/to/input_file."; exit 1; }

# If there's no single or too many arguments on the command line show help:
[ $# -eq 0 -o $# -gt 1 ] && _help

# If the dependencies arent met show a warning:
for BIN in ping mail logger; do 
 `which ${BIN}` > /dev/null 2>&1 || { echo "No ${BIN}, exiting." > /dev/stderr; exit 127; }
done

# If the input file isn't a file show help:
[ -f "${1}" ] && _help

# Assert the log file with fixed name "iplog" resides in the same directory as the input file:
IPLOG="${1%/*}/iplog"

# ...and if there actually exists a "iplog" then remove it
[ -f "${IPLOG}" ] && rm -f "${IPLOG}"

# You don't do something special with file descriptors so there's no need for trickery,
# also using UPPER CASE for variables makes them easier to spot:
cat "${1}" | while read IP; do
 ping -c 2 ${IP} > /dev/null 2>&1  # ping and discard output
 case "$?" in
  0) echo "${IP} is up" >> "${IPLOG}";;
  *) echo "${IP} is down"
     logger -p auth.notice -t "${PROGN}" "${IP} is down"
     echo "${IP} is down" | mail -s "Alert: ${IP} down" user@anydomain.com;;
 esac
done

# Code should exit OK
exit 0
 
Old 02-13-2011, 04:39 AM   #3
zunder1990
LQ Newbie
 
Registered: Feb 2011
Posts: 29

Original Poster
Rep: Reputation: 3
I wrote this for my work where all host are ping able. I posted this script because it is easy to understand what is going on. Any one can remove the email part but for my work i wanted it.
 
Old 02-13-2011, 05:20 AM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,353
Blog Entries: 55

Rep: Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541Reputation: 3541
Well good for you!
 
Old 12-19-2012, 10:15 AM   #5
swanross
LQ Newbie
 
Registered: Dec 2012
Posts: 1

Rep: Reputation: Disabled
Thanks

Thank you, you were right, I was googling and luckily I found your script. Pretty easy to understand, I modified two rows and works great
 
  


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
bash script recording remote host output? ieatbunnies Linux - Software 4 07-21-2010 12:23 PM
How to read a text file using a bash script Jeroen1000 Programming 8 09-30-2009 06:53 AM
Bash script to read .txt files on a remote Windows 2003 R2 server via FTP laurens Linux - Newbie 25 05-15-2009 06:01 AM
how to read text file using bash script kkpal Linux - Newbie 4 03-12-2008 01:57 AM
how to read text file using bash script kkpal Linux - Newbie 2 03-03-2008 11:40 AM


All times are GMT -5. The time now is 09:28 AM.

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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration