-
Notifications
You must be signed in to change notification settings - Fork 4
/
ping_by_ip.pl
55 lines (42 loc) · 1.51 KB
/
ping_by_ip.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
#!/usr/bin/perl
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
# Author: epierre
use v5.14;
use LWP::Simple; # From CPAN
use JSON qw( decode_json ); # From CPAN
use Data::Dumper; # Perl core module
use strict; # Good practice
use warnings; # Good practice
use utf8;
use feature qw< unicode_strings >;
my $trendsurl = "http://192.168.0.24:8080/json.htm?type=devices&filter=all&used=true&order=Name";
my %IP=(39=>'192.168.0.23',
40=>'192.168.0.22',
10=>'192.168.0.25');
my $json = get( $trendsurl );
die "Could not get $trendsurl!" unless defined $json;
# Decode the entire JSON
my $decoded = JSON->new->utf8(0)->decode( $json );
my @results = @{ $decoded->{'result'} };
my @tab;
foreach my $f ( @results ) {
if ($f->{"SwitchType"}) {
$tab[$f->{"idx"}]=$f->{"Status"};
}
}
foreach my $k (keys %IP) {
my $ip=$IP{$k};
my $res=system("sudo ping $ip -w 3 2>&1 > /dev/null");
print "-->".$k." ".$res." ".$tab[$k]."\n";
if (($res==0)&&($tab[$k] eq 'Off')) {
print "$k is On\n";
`curl -s "http://192.168.0.24:8080/json.htm?type=command¶m=switchlight&idx=$k&switchcmd=On"`;
} elsif (($res!=0)&&($tab[$k] eq 'On')) {
print "$k is Off\n";
`curl -s "http://192.168.0.24:8080/json.htm?type=command¶m=switchlight&idx=$k&switchcmd=Off"`;
} else {
print "do nothing: $k is ".$tab[$k]."\n";
}
}