]> git.sesse.net Git - nms/blob - clients/switchfix.pl
Merge.
[nms] / clients / switchfix.pl
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use DBI;
5 use Getopt::Long;
6 use LWP::UserAgent;
7 use lib '../include';
8 use nms;
9
10 {
11         package RequestAgent;
12         our @ISA = qw(LWP::UserAgent);
13
14         sub new {
15                 return LWP::UserAgent::new(@_);
16         }
17
18         sub get_basic_credentials {
19                 return ("admin", "removed");
20         }
21 }
22
23 # CONFIG
24 my $read = 0;
25 my $write = 0;
26 GetOptions ("read|r" => \$read, "write|w" => \$write);
27 my $target_mask = $ARGV[0] ? $ARGV[0] : 'e%-%sw';
28
29
30 # ACTION
31 my $dbh = nms::db_connect();
32 my $sth = $dbh->prepare("SELECT sysname,ip FROM switches WHERE sysname LIKE ? ORDER BY ip");
33 $sth->execute($target_mask);
34
35 while (my ($sysname,$ip) = $sth->fetchrow_array) {
36         my ($gw, $mgmnt);
37
38         # Autodefine
39         if ($ip =~ /(\d+\.\d+\.\d*)(\d)\.\d+/) {
40                 $gw = $1.$2.".1";
41                 $mgmnt = "90$2";
42         } else {
43                 print "$sysname\t($ip):\tAieeh! IP parsing fuckup!\n";
44                 next;
45         }
46
47         # Define
48         my $url = "http://$ip/Forms/rpip_1";
49         my %shit = (
50                         "rpip_RpgDHCP" => "1",
51                         "rpip_IptIPAddr" => "$ip",
52                         "rpip_IptSubnetMask" => "255.255.255.0",
53                         "rpip_IptDefaultGateway" => "$gw",
54                         "rpip_IptDNS" => "0.0.0.0",
55                         "rpip_IptVID" => "$mgmnt",
56                         "rpip_HidBtnNum" => "1"
57                    );
58
59         # Check 
60         my $found = 0;
61         open(NMAP, "nmap -p80 $ip|");
62         while(<NMAP>) {
63                 if(/^80\/tcp\s+open/) {
64                         $found = 1;
65                 }
66         }
67         if(!$found) {
68                 print "$sysname\t($ip):\tNo reply on port 80\n";
69                 next;
70         }
71
72         # Read
73         if ($read) {
74                 print "$sysname\t($ip):\t";
75                 
76                 my $ua = RequestAgent->new;
77                 my $res = $ua->get("http://$ip/rpip.html");
78                 if ($res->is_success) {
79                         foreach (split("\n", $res->content)) {
80                                 if (/.*rpip_IptDefaultGateway" .* VALUE="([^"]+)">/) {
81                                         print "default route $1\n";
82                                 }
83                         }
84                 }  else {
85                         print $res->status_line, "\n";
86                 }
87         }
88         
89         # Rape
90         if ($write) {
91                 print "$sysname\t($ip):\t";
92                 
93                 my $ua = RequestAgent->new;
94                 my $res = $ua->post($url, \%shit);
95                 
96                 print $res->status_line, "\n";
97         }
98 }