Bash Script w/ IP Range


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Bash Script w/ IP Range
# 1  
Old 05-20-2009
Bash Script w/ IP Range

Is there a basic IP range script or a site that has basic script written?

I am making a script that is looking for a device on my network threw HTTP://. I and using a for loop with wget to download the page and using grep to search the code for the serial number.

My range is 172.16.x.x/16 I only have up to 172.16.10.x so I will probably run it by /24 subnets. How can I run this and the for loop continues to the next IP?

Honestly, I have only taken a few scripting classes and trying to do it for misc things. I am really try to get this ability and if you guys have any pointers, it would be much appreciated.
# 2  
Old 05-20-2009
Step 1: (Almost) check validity
Code:
$ echo "172.16.10.124" | grep -qE '172\.16\.10\.[0-9]{1,3}' ; echo $?
0
$ echo "172.16.10.abc" | grep -qE '172\.16\.10\.[0-9]{1,3}' ; echo $?
1

Note that this won't catch invalid addresses like '999.888.777.666'
Step 2: Try to ping
Code:
$ ping -c 3 -w 5 good_host >/dev/null 2>&1 ; echo $?
0
$ ping -c 3 -w 5 bad_host >/dev/null 2>&1 ; echo $?
1

Step 3: Check for an open port
Code:
$ netcat -z good_host 80 ; echo $?
0
$ netcat -z bad_host 80 ; echo $?
1

# 3  
Old 05-20-2009
I'm sorry for not being clear on the range. I have networks from 172.16.1.1 - 172.16.10.254.

$ echo "172.16.10.124" | grep -qE '172\.16\.10\.[0-9]{1,3}' ; echo $?
0
$ echo "172.16.10.abc" | grep -qE '172\.16\.10\.[0-9]{1,3}' ; echo $?
1

RANGE=172.16.1.0/20

FOR i IN '$RANGE'
do
wget http://$i
mv --force index test
SN=`cat test | grep $1 | awk -F"<" '{print $4}' | awk -F">" '{print $2}'`

if [ "$1" = "$SN"]
then
`cat test > HA.GOTYA`
else
continue
fi
done

The script is something like that. But need to run this on a range of IPs. Am I going to have to do a ping test and put the good in to a file then having to get that file over to the script?
# 4  
Old 05-20-2009
you just need to find a way to loop from 1 to 10, and then 1 to 254 eg if you have Python
Code:
#!/usr/bin/env python
import urllib2
for i in range(1,11):
    for j in range(1,255):
        sn="172.16.%d.%d" % (i,j)
        url="http://%s" % sn        
        page=urllib2.urlopen(url)
        result=page.read()
        if sn in result:
            open("HA.GOTYA","a").write(result)

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Bash script, find the next closed (not in use) port from some port range.

hi, i would like to create a bash script that check which port in my Linux server are closed (not in use) from a specific range, port range (3000-3010). the print output need to be only 1 port, and it will be nice if the output will be saved as a variable or in same file. my code is: ... (2 Replies)
Discussion started by: yossi
2 Replies

2. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "compileDeployStartWebServer.sh" script and a "StartBrowser.sh" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

3. Shell Programming and Scripting

Perl script to allocate next available IP from a range

Hi I am looking to automate the process in PERL of allocating IP addresses from a set range of addresses (for example a /22 network 10.10.224.1 - 10.10.227.254) I am able to query the IP addresses that are already in use in the above range, which will produce me a list like this for example... (3 Replies)
Discussion started by: hcclnoodles
3 Replies

4. Shell Programming and Scripting

Bash script - printing range of lines from text file

I'm working on a new exercise that calls for a script that will take in two arguments on the command line (representing the range of line numbers) and will subsequently print those lines from a a specified file. Command line would look like this: ./lines_script.bash 5 15 <file.txt. The script would... (8 Replies)
Discussion started by: ksmarine1980
8 Replies

5. Homework & Coursework Questions

Bash Script for Dice Game; Issue with if...else loop to verify user guess is within range

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have written a script for a dice game that: (1) tells user that each of the 2 die are 6 sided (Spots=6); (2)... (3 Replies)
Discussion started by: LaurenRose
3 Replies

6. Shell Programming and Scripting

Script on Date Range

Hi All, Can anybody help me out a Shell script which pulls the files based on date range Example ./test.sh start_date End_date (20110901 20110930) or ./test.sh ( if we don't provide any input) it should take sysdate-1 ( yesterdays date) it should have both conditions Plzz help me... (1 Reply)
Discussion started by: krux_rap
1 Replies

7. Shell Programming and Scripting

Select columns from a matrix given within a range in BASH

I have a huge matrix file which looks like this (example matrix): 1 2 3 5 4 5 6 7 7 6 8 9 1 2 4 2 7 6 5 1 3 2 1 9 As one can see, this matrix has 4 columns and 6 rows. But my original matrix has some 3 million rows and 6000 columns. For example, on this matrix I can define my task as... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

8. Shell Programming and Scripting

bash script range calculations

Hi, I have data in the following form: AB001 10 AB002 9 AB003 9 etc AB200 5 What I need to do is sum up the second value according to groups of the first, i.e. AB001 to AB030 the total being X, AB031 to AB050 the total being Y etc (there are 5 AB ranges of different sizes). I'm sure... (3 Replies)
Discussion started by: chrissycc
3 Replies

9. Shell Programming and Scripting

Bash script to test IP range on server

Hello, We have to configure servers with a range of IPs which is in itself a subject for another script assistance request -but- we have run into quite a few IP ranges with routing problems lately. I've been trying to figure out the best way to test a range of IPs, I mean, manually it's not... (4 Replies)
Discussion started by: boxgoboom
4 Replies

10. Shell Programming and Scripting

print range between two patterns if it contains a pattern within the range

I want to print between the range two patterns if a particular pattern is present in between the two patterns. I am new to Unix. Any help would be greatly appreciated. e.g. Pattern1 Bombay Calcutta Delhi Pattern2 Pattern1 Patna Madras Gwalior Delhi Pattern2 Pattern1... (2 Replies)
Discussion started by: joyan321
2 Replies
Login or Register to Ask a Question