[IOML] Shell/Perl/Whatever script wanted

Dylan Smith dyls at alioth.net
Wed Jul 20 22:57:13 BST 2005


On 20 Jul 2005, at 16:10, Simon Booth wrote:

> Hi Guys,
>
> I'm not sure any of you guys will be able to help me with this but 
> it's worth a try.  I am in need of some sort of script or app which is 
> small and can be run from a linux shell which will discover a route 
> out to the internet from whatever network it is put on.

I take it there's nothing more straightforward you can use to discover 
this sort of stuff (such as a combo of DHCP+DNS)...

The default gateway is easy enough to discover. Just have your script 
run 'route -n' then parse the output (i.e. look for the route to 
0.0.0.0). I used to have to do this back when Linux used FreeSWAN for 
ipsec and KLIPS couldn't figure out the default route on its own. Once 
you find the line with 0.0.0.0 in it you can just split it (using 
whitespace as the delimiter) and pull out the appropriate element of 
the array you get back which contains the IP address of your default 
gateway.

Testing for proxies can probably be done pretty easily using Perl's LWP 
(libwww perl) module, which I think handles proxies just fine - just 
set a proxy address and see if you get something back, and try 
something else if you don't. The perldoc for LWP has an example of 
using LWP::UserAgent to do something like wget (although the proxy is 
set by the environment - there may be a method to override this). For 
something lower level just use IO::Socket::INET - you can connect to a 
remote socket with two lines of code.

Scanning a network for a proxy on the usual ports would also be pretty 
easy (well for a /24 at least) using IO::Socket::INET so long as all 
the machines on the subnet aren't filtering ports (i.e. you get 
Connection Refused if you try and connect and there's nothing 
listening, instead of having to wait for it to time out).




More information about the IOM mailing list