Multiple IP Loop


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Multiple IP Loop
# 1  
Old 10-30-2012
Lightbulb Multiple IP Loop

Hey all,

I'm trying to do some very basic scripting. I been scouring the board looking at examples but I just can't seem to piece something together.

Edit: Now that I look at it, maybe my request is a little more complex and I'm biting off more than I can chew...

1. Take a list of IPs/hosts (hosts.txt)

Code:
cat Hosts.txt
host1
host2
host3

For each host:
1. Continually Ping with Timestamp
2. Append each in their own log file (host1.log, host2.log, host3.log)
3. Every 30 seconds perform an nmap -sP - Append another individual log file (host1-nmap.log, host2-nmap.log, host3-nmap.log)
4. Run this script maybe in crontab so that it keeps running until you run a "service stop"?

Honestly, I'm not entirely looking just for the answer, would just appreciate some tips, examples and kinda where to look.

Thanks!
# 2  
Old 10-30-2012
Quote:
Originally Posted by YouFatPenguine
1. Take a list of IPs/hosts (hosts.txt)

Code:
cat Hosts.txt
host1
host2
host3

For each host:
1. Continually Ping with Timestamp
2. Append each in their own log file (host1.log, host2.log, host3.log)
3. Every 30 seconds perform an nmap -sP - Append another individual log file (host1-nmap.log, host2-nmap.log, host3-nmap.log)
4. Run this script maybe in crontab so that it keeps running until you run a "service stop"?
Let us start with 1.: Because commands in a script are only processed sequentially you will have to send the jobs in the background to have them run concurrently:

Code:
while read chHost ; do
     ping "$chHost" &
done < hosts.txt

2. is quite simple, just add a redirection:

Code:
while read chHost ; do
     # ping "$chHost" > "$chHost".log &           # create new logfile every run
     ping "$chHost" >> "$chHost".log &            # append to existing log
done < hosts.txt


For 3. set up a second loop:

Code:
while : ; do
     while read chHost ; do
          nmap -sP $chHost >> ${chHost}-nmap.log
     done < hosts.txt
     sleep 30
done

That leaves the starting/stopping issue. I'd use a PID file in "/var/run" and use that to eliminate the process:

Code:
#! /bin/ksh

DispHostList ()
{
typeset chHost=""

while reach chHost ; do
     print - "$chHost"
done < /path/to/hosts.txt

return 0
}



pStop ()
{
typeset fPID="$1"

if [ ! -e "$fPID" ] ; then
     print -u2 "ERROR: there is nothing to stop"
     exit 1
else
     kill -15 "$(cat "$fPID")"
fi

return 0
}



pStart ()
{
typeset PID="$$"
typeset chHost=""
typeset fPID="$1"

if [ -e "$fPID" ] ; then
     print - u2 "ERROR: another instance seems to run."
     exit 1
else
     print - "$PID" > "$fPID"
fi

DispHostList |\
while read chHost ; do
     # ping "$chHost" > "$chHost".log &           # create new logfile every run
     ping "$chHost" >> "$chHost".log &            # append to existing log
done

while : ; do                                      # this will loop forever
     DispHostList |\
     while read chHost ; do
          nmap -sP $chHost >> ${chHost}-nmap.log
     done
     sleep 30
done

}


# main()
typeset fRun="/var/run/pinglog.pid"

case "$1" in
     start)
          pStart "$fRun"
          ;;

     restart)
          pStop "$fRun"
          pStart "$fRun"
          ;;

     stop)
          pStop "$fRun"
          ;;

     *)
          print -u2 "ERROR: do not know how to handle \"$1\""
          exit 1
          ;;
esac

exit 0

I hope this helps.

bakunin

Last edited by bakunin; 10-30-2012 at 01:35 PM.. Reason: corrected typo
# 3  
Old 10-30-2012
Wow, my head is spinning a bit.

So, I had gotten close to the list thing stumbling through, but essentially took your advice verbatim and did the following:

Code:
#!/bin/bash

#while read chHost ; do
# ping "$chHost" &
#done < hosts.txt

while read chHost ; do
 # ping "$chHost" > "$chHost".log &             # Create new log ever run
 ping "$chHost" | xargs -n1 -i bash -c 'echo `date +"%m-%d %H:%M:%S"`" {}"' >> ./Ping/"$chHost".log &
done < hosts.txt

Which works great. But I noticed it was a bear to try and do any sort of PS since it spawned so many. The second PID portion of your suggestion REALLY has my head spinning and thus I'll need to re-read it.

So, in the meantime, I've just adjusted the script to do the following (which is probably a better I idea anyway)

Code:
ping -c 28800  "$chHost" | xargs -n1 -i bash -c 'echo `date +"%m-%d %H:%M:%S"`" {}"' >> ./Ping/"$chHost".log &

Thanks a ton!
# 4  
Old 10-30-2012
Quote:
Originally Posted by YouFatPenguine
The second PID portion of your suggestion REALLY has my head spinning and thus I'll need to re-read it.
As much as i would like to take the praise for this idea: it is common within many UNIX programs (daemons mostly) to create a file with the PID (process ID) in /var/run at the start and use the PID stored there to shut down the process.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Multiple if within while loop ksh

Hi All, I'm trying to write while loop with multiple if conditions. Each and every if condition with different variables. whenever one if condition fails i have remove the file from filename and have to pick another file and loop should exit until the last file found in filename. Please help... (4 Replies)
Discussion started by: Kayal
4 Replies

2. Shell Programming and Scripting

Loop over multiple arrays

Hi All I need really really help with this :- I have two files ( File1 , File 2) both files are output of two different scripts. File1 usually has a list of names ( sometimes 3 names sometimes 5 sometimes more , depends about the output of the script) File2 usually has a list of numbers... (2 Replies)
Discussion started by: samsan
2 Replies

3. Shell Programming and Scripting

Reset while loop to loop same file multiple times

Hi, I want to read file multiple times. Right now i am using while loop but that is not working. ex. While read line do while read line2 do echo stmt1 #processing some data based on data., done < file2.txt done < file1.txt # This will have 10... (4 Replies)
Discussion started by: tmalik79
4 Replies

4. Shell Programming and Scripting

How to use for/while loop with multiple variables?

Hi, I have two variables like below which will always be of the same size a=1:2:3 b=A:B:C I need to use a for/while loop that will have both the variables available. I cannot use an array here and will probably might iterate through the variable as echo $a | tr ':' '\n' and thus iterate... (5 Replies)
Discussion started by: Elizabeth H
5 Replies

5. Shell Programming and Scripting

Doing multiple tasks in a loop.

I am in the process of updating a folder of hundreds of recipe html files. I've already managed to modify a number of things in each file but I have run into something that's beyond my ability. I have a text file that I need to insert the contents into the html at a specific point. It creates... (0 Replies)
Discussion started by: Trapper
0 Replies

6. UNIX for Dummies Questions & Answers

multiple variables in for loop

hi, I want an equivalent for loop for this C code in unix shell script... for(int i,int j;i<5;i++,j++) { } Please reply soon Regards Navjot (1 Reply)
Discussion started by: navjotsingh
1 Replies

7. Shell Programming and Scripting

How to avoid multiple while loop?

Hi, How can I avoid multiple 'cat while read ....? in my script. In my script, I am taking the inputs from the temp text file and doing the ( cat while read input do ... ... done ) task and deleting later. I know it'll raise the perfomance issue. How to avoid this? (2 Replies)
Discussion started by: sharif
2 Replies

8. Shell Programming and Scripting

please help in multiple for loop

Hi I have these data 0.9>i>0.1 0.45>j>0.01 I want to execute the script file for different number of i and j using the command $ ./scriptfile i j I write this code #bin/bash for ((i>0.1; i<0.9; i++)) for ((j>0.01; j<0.45; j++)) do ./scriptfile $i $j done However, for loop is not... (3 Replies)
Discussion started by: snow
3 Replies

9. Shell Programming and Scripting

While loop with Multiple variables

Hi , I am trying to write a script in kshell with while loop ,its like count=1 count_cmp=1 while ; do tail -$count tempfile | head -1 > tempstring ....... done However i get CIF.sh: line 33: ' I have checked thetrailing spaces , not sure what is... (4 Replies)
Discussion started by: amit1_x
4 Replies

10. Shell Programming and Scripting

for loop with multiple variables ?

I have a script which selects two 'sets' of system LVM device files from a tabular file 'mapfile' using awk : LIVELV=`awk '{print($1)}' mapfile` BCVLV=`awk '{print($3)}' mapfile` I wanted to pass these 'sets' into an LVM command 'loop' along the lines of : lvmerge $BCVLV $LIVELV ie.... (3 Replies)
Discussion started by: fosterian
3 Replies
Login or Register to Ask a Question