-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMACCheck.pl
110 lines (98 loc) · 3.06 KB
/
MACCheck.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use warnings;
use Net::Telnet();
use HTTP::Request;
use LWP::UserAgent;
use Socket;
use DBI;
use Data::Dumper;
use Config::Simple;
$cfg = new Config::Simple('variables.cnf');
my (@MAC,@arpTable);
my ($tmpMAC,$tmpIP, $tmpDNSname,$routerADDR,$variables);
my $dbtype = $cfg->param('dbtype');
my $dbname = $cfg->param('dbname');
my $dbhost = $cfg->param('dbhost');
my $dbuser = $cfg->param('dbuser');
my $dbpass = $cfg->param('dbpass');
my $dbh = DBI->connect("DBI:$dbtype:$dbname:$dbhost", $dbuser, $dbpass) or die "Can't connect to db";
$sql = "select v_value as value, v_name from variables";
$sth = $dbh->prepare($sql);
$sth->execute() or die "Error";
$variables = $sth->fetchall_hashref("v_name");
$routerADDR = "$variables->{host}{value}";
while (1) {
undef @MAC;
undef @arpTable;
@arpTable = getARPtable($routerADDR);
#Opening connection to the DB server to grab the devices table
$sql = "select distinct(d_mac) from devices";
$sth = $dbh->prepare($sql);
$sth->execute() or die "Error";
my $MACT = $sth->fetchall_arrayref();
while ( my ($key, $value) = each($MACT) ) {
push (@MAC, $value->[0]);
}
foreach my $line (@arpTable) {
if ($line =~ /(.{2}\:.{2}\:.{2}\:.{2}\:.{2}\:.{2})/) {
if (checkMAC($1)) {
$tmpMAC = $1;
if ($line =~ /(\d+\.\d+\.\d+\.\d+)/) {
$tmpIP = $1;
$tmpDNSname = gethostbyaddr(inet_aton($tmpIP), AF_INET);
$tmpDNSname = ($tmpDNSname) ? $tmpDNSname : "UNKNOWN";
}
my $devicestring = "$tmpMAC - $tmpIP ($tmpDNSname)";
if($variables->{pushingbox_api}{value}) {
pushingBox($variables->{pushingbox_api}{value}, $devicestring);
}
$tmpDNSname = ($tmpDNSname) ? $tmpDNSname : "UNKNOWN";
$sql = "insert into `devices` (`d_mac`, `d_ip`, `d_dns`) values (?,?,?)";
$sth = $dbh->prepare($sql);
$sth->execute($tmpMAC, $tmpIP, $tmpDNSname) or die "Error";
$sql = "insert into `log` (`l_mac`, `l_ip`, `l_dns`) values (?,?,?)";
$sth = $dbh->prepare($sql);
$sth->execute($tmpMAC, $tmpIP, $tmpDNSname) or die "Error";
`msg * New device $tmpMAC found called $tmpDNSname`;
print "New device $tmpMAC @ $tmpIP ($tmpDNSname) -".(localtime)."\n";
}
}
}
sleep (10);
}
sub getARPtable {
my $telnet;
my $msg;
my @lines;
$telnet = new Net::Telnet(Timeout=>10,Errmode=>'die');
if (! defined $telnet) {
die "Unable to create telnet object";
}
$telnet->open("$_[0]");
if ( $msg = $telnet->errmsg) {
die "Unable to open telnet to $msg";
}
sleep (2);
$telnet->login("$variables->{username}{value}","$variables->{password}{value}");
sleep (1);
if ($msg = $telnet->errmsg) {
die "Unable to login to $msg ";
}
my @arpOutput = $telnet->cmd("cat /proc/net/arp");
$telnet->close();
return @arpOutput;
}
sub checkMAC {
foreach my $MACaddr (@MAC) {
if ($MACaddr eq $_[0]) {
return 0;
}
}
return 1;
}
sub pushingBox {
my $URL = "http://api.pushingbox.com/pushingbox?devid=$_[0]&device=$_[1]";
my $agent = LWP::UserAgent->new(env_proxy => 1,keep_alive => 1, timeout => 30);
my $header = HTTP::Request->new(GET => $URL);
my $request = HTTP::Request->new('GET', $URL, $header);
my $response = $agent->request($request);
}