Quick bash question


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Quick bash question
# 1  
Old 09-17-2010
Quick bash question

I need to speed up this process. I would like for this script to spawn a sub-process for each line it reads and move on to the next without waiting for the data to be returned.
I know with a large list this can be dangerous and eat system resources. However I use this script only when I get a large outage and I need to verify if it is the tools having a hiccup or if sites are truly down. Ignore the remarked out lines, I have a manual process for getting a clean file, I just left that code there for my own reference. The part in red is what I want to run as sub processes for each line in the file. In short open up a instance of ping for each host name.

Forgive any style errors as I am fairly new to scripting
Code:
#!/bin/bash

#reads input from the keyboard to populate variable "filename"
echo 'Enter file name:'
read filename

#Reads the file line by line and prints the third field which is the device name.
#Device names are saved to the file convert.txt then the entire file is moved back to the orginal file
#Leaving a clean file of just device names
# cat $filename | awk '{print $3}' >>convert.txt && mv convert.txt $filename

#Reads the device names from the now clean file one line at a time and stores the device name as variable  "ipaddr"
for ipaddr in `cat $filename`;

#This loop will ping each device name and print out on the screen is the device is up or down

do
count=$(ping -c4 $ipaddr | grep received |  awk -F',' '{ print $2 }' | awk '{ print $1 }')
if [ $count -eq 0 ]; then
  echo "Host : $ipaddr is down (ping failed) at $(date)"
else  echo "Host : $ipaddr is up"
fi
done;



Moderator's Comments:
Mod Comment Please use code tags, thank you

Last edited by Franklin52; 09-17-2010 at 04:04 AM..
# 2  
Old 09-17-2010
Could I suggest you look at "nmap" ? There is a "ping" facility in it that is concurrent and very fast and looks like it would replace the great majority of your above script.
This User Gave Thanks to citaylor For This Post:
# 3  
Old 09-17-2010
- Don't use the date command in the loop but once before the loop
- It's superfluous to use grep and awk twice

This loop should be faster:
Code:
mydate=$(date)

while read ipaddr
do
  count=$(ping -c4 $ipaddr | awk -F"[ ,]" '/received/{print $2}')
  if [ $count -eq 0 ]; then
    echo "Host : $ipaddr is down (ping failed) at $mydate"
  else
    echo "Host : $ipaddr is up"
fi
done < $filename

This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 09-17-2010
I would use nmap, however it is not installed on the box I am working on. I do not have the ability to install it. It is a production box in a clients network.

---------- Post updated at 02:45 AM ---------- Previous update was at 02:38 AM ----------

The speed issue comes from the execution of the ping command. The script reads one line from the file executed the ping command then displays the edited output and then moves on to the next line. At most my file will have 40 - 50 host names, however when executed one at a time and waiting for the output before continuing it still takes quite a bit of time. I am wanting to send pings to every line in the file at the same time and post the output.

Thank you for the cleaned up version of code, I will implement that.
# 5  
Old 09-17-2010
Code:
#!/usr/bin/env ruby -w
require 'net/ping'
File.readlines("file").each do |line|
   device=line.split.at(2)
   pt = Net::Ping::External.new(device)
   puts pt.ping?(device) ? "#{device} is up " : "#{device} is down "
end

This User Gave Thanks to kurumi For This Post:
# 6  
Old 09-17-2010
Thank you for the reply kurumi. Net ping is not installed on this box. I cannot install it, I do not have permissions to install anything on the box. The only shell I have access to is BASH. I am limited in working within that.
# 7  
Old 09-17-2010
Hi,

Finding a solution for this problem was fun i must admit.

Here is one solution full bash.

This script will do as many max concurrent process as you want, just set MAXJOBS to it.

Typical usage is cat iplist.lst | thescript.sh

Code:
#!/bin/bash

# Max concurrent job process
MAXJOBS=60

trap "kill 0" EXIT

IPLIST=($(cat))

index=0
index_count=${#IPLIST[@]}

function doping {
    local ipad=$1
    if [ -n "$ipad" ] ; then
        ping -c4 -q $ipad &>/dev/null
        [ $? -eq 0 ] && echo "Host : $ipad is up" || echo "Host : $ipad is down"
    fi
}

while [[ $index -le $index_count ]]
do
  # The current ip
  IPADDR=${IPLIST[$index]}

  NBJOBS=$(jobs -p | wc -l)
  if [ "$NBJOBS" -lt "$MAXJOBS" ] ; then

      doping $IPADDR &
      ((index++))
      continue

  fi
done
wait

Tested it with 400+ Hosts and MAXJOBS=200 it was faaast Smilie
This User Gave Thanks to Chirel For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Quick Bash question.

Hi, I'm basically looking to see what this line of code does: . `dirname $0`/../config/config Thanks. (1 Reply)
Discussion started by: cabaiste
1 Replies

2. Shell Programming and Scripting

Quick question: calling c-shell script from bash

Hello, I have a quick reference question: I have a very long, but fairly straigtforward script written in c-shell. I was wondering if it is possible to call this script from bash (for ex. having a function in bash script which calls the c-shell script when necessary), and if so, are there any... (1 Reply)
Discussion started by: lapiduslost
1 Replies

3. UNIX for Dummies Questions & Answers

Quick question

Hi guys Quick question Im creating an FTP server and im chrooting each user to there home directory blah blah. Ive also setup scponly so there locked etc. Im a novice at unix and have just reaslised the primary group of scponly is the username of one of the ftp users... which im sure... (1 Reply)
Discussion started by: mokachoka
1 Replies

4. Shell Programming and Scripting

Quick question

When I have a file like this: 0084AF aj-123-a NAME Ajay NAME Kumar Engineer 015ED6 ck-345-c 020B25 ef-456-e 027458 pq-890-p NAME Peter NAME Salob Doctor 0318F0 xy-123-x NAME Xavier Arul NAME Yesu Supervisor 0344CA de-456-d where - The first NAME is followed by... (6 Replies)
Discussion started by: ajay41aj
6 Replies

5. Shell Programming and Scripting

quick newbie bash question

I'm in .profile. I want to export a directory ( export MY_TOOL_HOME=/tools/my tool". Unfortunately for me, the directory I want to export to has a space in its name. So when I try to cd $MY_TOOL_HOME, i get a No such file or directory at the command line... thanks for the help (4 Replies)
Discussion started by: redsand9009
4 Replies

6. AIX

quick question

Hi, At best I'm a junior admin with a big problem. My developers have got my root password and mgmt insists they need it. I can't even change it when people knowing it leave. I'm certain they've hardcoded it into routines. I've searched my servers and grepped everything & can't find it. ... (5 Replies)
Discussion started by: keith.m
5 Replies

7. UNIX for Dummies Questions & Answers

Quick question

Hi, Is there a simple way, using ksh, to find the byte position in a file that a stated character appears? Many thanks Helen (2 Replies)
Discussion started by: Bab00shka
2 Replies

8. UNIX for Dummies Questions & Answers

Quick Question

Hi, I am new to UNIX, and am learning from this tutorial : http://www.ee.surrey.ac.uk/Teaching/Unix/index.html It keeps telling me to files downloaded from the internet (like .txt files) to the directory, and I dont know how to. How do I add .txt files to my directory? Thanks. (6 Replies)
Discussion started by: IAMTHEEVILBEAN
6 Replies

9. Shell Programming and Scripting

A very quick question

Just a super quick question: how do you put a link in your php code. I want to make a link to something in /tmp directory. i.e. how do you put a href into php, I think it's done a bit differently. thanks john (1 Reply)
Discussion started by: jmg5
1 Replies

10. UNIX for Dummies Questions & Answers

Quick Question

I know in DOS, when you want to pull up your last/previous command, you hit the up/down arrows. How do you do that with UNIX? (3 Replies)
Discussion started by: Tracy Hunt
3 Replies
Login or Register to Ask a Question