Click to See Complete Forum and Search --> : i need a script that....


FoBoT
08-06-2003, 11:31 AM
this can be in almost anything, i would prefer perl as i am slightly familiar with it, but beggers can't be choosers

I need a script that does the following:

searches predefined subnets (about 25 different ranges) for a device that has port 111 open/active.

it then records the IP address of this device into a flat file/text file.

it then either sets a flag or places a file onto a web server to indicate the status of that device. i.e. a webpage that displays the status of these devices either being present or not needs to be updated by the script.

next is hits a url based on the ip address, http://<IP Address> , and extracts a particular piece of information from the web page, here is an example of the source code from a typical page (applicable portion)

<HTML>
<HEAD>
<TITLE>Snap Server SNAP176101 [Home]</TITLE>
<SCRIPT LANGUAGE = "JavaScript">
<!-- Hide script from old browsers.


so i need the script to read the "SNAP176101" from that URL and put that into a flat/text file.


that is about it, i can hack/chop/mangle the rest of what i need to do

any advice or pointers to code snippets relating to this type of thing is appreciated. thank you

also if you can point me to other scripting sites where i may be able to post this question for greater exposure , that would be appreciated as well

scinerd
08-06-2003, 12:02 PM
I did some playing around with port testing a while back and I think this perl sub will help you. The only thing you need is IO::socket wich is on most machines I think. If I where going to write your script the first part of the script I would read in a cofig that looks like "192.168.1.:22:36" on each line. then split on the ":" to get your first part of ip and then the range. Then use a while loop to check for the port with the sub below. Then you if the sub returns true then you can add the ip to array. Once the array is done you can run the a foreach loop on the array and check for the web stuff.

hope this helps.


sub testport{
my $hostname = shift;
my $port = shift;
if (!defined $hostname || !defined $port){
warn "needed arguments to testsw not given";
return 0;
}
if ($port == 'ping'){
my $pingTimeout = 5;
if (system("ping $hostname $pingTimeout > /dev/null 2>&1") == 0) {
return 1;
}
return 0;
}

IO::Socket::INET->new("$hostname:$port") or return 0;
return 1;
}
#end testport

Stuka
08-06-2003, 12:57 PM
I thought this would be a decent test for my Python learning, so I put this together. The only thing I can't validate here is the regex part (and I didn't write the part to move the file to the webserver, since you didn't mention HOW you wanted to do that). #!/usr/bin/python

import socket
import urllib
import re

#Define IP ranges here - first part is the prefix, second is a range tuple
ranges = [('192.168.0', (1, 25)), ('192.168.0', (26, 100)), ('67.153.4', (154, 158))]

#Socket for test connections
mysock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#File that contains successfully connected IPs
valid_file = file('valid_ip.txt', 'w')
#And a file for the 'title' strings
title_file = file('titles.txt', 'w')
#List of valid IP addresses
ip_list = []
#List of 'title' strings
title_list = []
#Regular expression for extracting title string
title_re = re.compile('<title>(.*)<\\title>', re.IGNORECASE)

#Test each IP, write successful IPs to the file
for val in ranges:
for i in range(val[1][0], val[1][1] + 1):
try:
address = val[0] + '.' + str(i)
print 'Testing ' + address
mysock.connect((address, 110))
except:
print 'Failed'
continue
print 'Succeeded'
ip_list.append(address + '\n')

valid_file.writelines(ip_list)

#Send the file to the web server (FTP? NFS?)

#Hit the web pages, extract the info

for ip in ip_list:
web_page = urllib.urlopen('http://' + ip)
data = web_page.readlines()
match = title_re.search(data)
if (match != None):
title_list.append(match.expand('\1') + '\n')

title_file.writelines(title_list)

FoBoT
08-06-2003, 01:11 PM
you guys rock! :D
i'll start playing with this when i finish the other 3 priority one projects on my plate ;)

thank you

iDxMan
08-06-2003, 07:20 PM
..if the purpose is something other than learning IO::Socket, why not just use nmap and redirect its output to a file?

eg:

nmap -p 111 192.168.47.* >/www/foo/rpc_buggers


-r