LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Ping Random IPs using a tcsh script.. (https://www.linuxquestions.org/questions/programming-9/ping-random-ips-using-a-tcsh-script-79794/)

zeppelin 08-07-2003 06:16 AM

Ping Random IPs using a tcsh script..
 
I want a script so as to be able to ping 255 ip addresses but I want to produce these addresses dynamically (or even random)
any help?

the ip should be 192.168.1.xxx

kev82 08-07-2003 06:21 AM

are they all on the same subnet eg is it 192.168.2.x where x varies between 1 and 254 or is it just 255 totally random addresses, also does it have to do each address once or can it repeat the same address?

zeppelin 08-07-2003 06:32 AM

They all are on the same subnet eg is it 192.168.2.x where x varies between 1 and 254
it would be better if it won't repeat the same address.
thanks for the quick reply..

kev82 08-07-2003 06:50 AM

i cant see how to do it in a shell script without repeating the same address, it would be much easier to do from a c program.

zeppelin 08-07-2003 07:24 AM

ok never mind repeating..

kev82 08-07-2003 07:55 AM

this basically spews out 254 random ip addresses you just have to modify it to ping them.

#!/bin/bash

COUNTER=254
RANDMAX=3600
RANDOM=$(( $(date '+%S') * $(date '+%M') ))

while [ $COUNTER -gt 0 ]; do
RANDOM=$(( $RANDOM * 137 + 258 % RANDMAX ))
echo 192.168.2.$(( $RANDOM % 255 ))
COUNTER=$(( $COUNTER-1 ))
done;

i dont know how good my random number generation is but it seems to work.

<edit>just realised it can produce 192.168.2.0 but im sure you can modify it to your needs

Hko 08-07-2003 10:40 AM

Quote:

Originally posted by kev82
i dont know how good my random number generation is but it seems to work.
I certainly can imagine that it "seems to work"...
But it will produce much less random numbers when you use another name for "RANDOM" !

Bash has it's own random generator, and it uses the RANDOM variable for this.
Quote:

from "man bash"
Shell Variables
The following variables are set by the shell:

[..snip..]

RANDOM Each time this parameter is referenced, a random integer between 0 and 32767 is generated. The sequence of random numbers may be initialized by assigning a value to RANDOM. If RANDOM is unset, it loses its special properties, even if it is subsequently reset.

So when you do:

echo 192.168.2.$(( $RANDOM % 255 ))

its not your number-fiddling producing the randomness, but bash's own random generator !

kev82 08-07-2003 11:07 AM

wow, i didnt know that. guess i should have looked at the man page, and i thought my number fiddling was doing really well. never mind.


All times are GMT -5. The time now is 02:24 AM.