LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-07-2003, 06:16 AM   #1
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Rep: Reputation: 30
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

Last edited by zeppelin; 08-07-2003 at 06:20 AM.
 
Old 08-07-2003, 06:21 AM   #2
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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?
 
Old 08-07-2003, 06:32 AM   #3
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
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..

Last edited by zeppelin; 08-07-2003 at 06:33 AM.
 
Old 08-07-2003, 06:50 AM   #4
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
Old 08-07-2003, 07:24 AM   #5
zeppelin
Member
 
Registered: Apr 2003
Location: Athens, Greece
Distribution: Arch
Posts: 182

Original Poster
Rep: Reputation: 30
ok never mind repeating..
 
Old 08-07-2003, 07:55 AM   #6
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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

Last edited by kev82; 08-07-2003 at 07:57 AM.
 
Old 08-07-2003, 10:40 AM   #7
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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 !

Last edited by Hko; 08-07-2003 at 10:42 AM.
 
Old 08-07-2003, 11:07 AM   #8
kev82
Senior Member
 
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
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.
 
  


Reply



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
script to block IPs from Korea, China, Taiwan?? latino Linux - Security 4 09-05-2005 09:57 PM
Random Name Shell Script tebucky Linux - Newbie 2 04-27-2005 12:47 PM
tcsh script: how to see if an env var is defined? BrianK Programming 1 06-07-2004 10:12 PM
Shell script for insert ip address into an ordered list of IPs inTrouble? Linux - Newbie 2 10-27-2003 02:21 AM
Can ping MyDNS1.FQDN and IPs but no other hostnames? leecason Linux - Networking 3 10-06-2003 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:41 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
Open Source Consulting | Domain Registration