-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinbo-update-ips.pl
executable file
·61 lines (56 loc) · 1.91 KB
/
linbo-update-ips.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
#!/usr/bin/perl -w
# linbo-update-ips.pl
# if found: update ip, wlanip in workstations file
# otherwise create line
# workstation line: serverraum;cpqgymhim12;ipadmax;48:F1:7F:8F:F2:6E;10.0.22.68;;;;classroom-studentcomputer;;1;
use strict;
use JSON::XS;
my $workstations = "/etc/linbo/workstations";
my $temp = `mktemp /tmp/linbo-update-ipsXXXXXXXX`;
chomp $temp;
my %host = ();
my $IP = 4;
my $WLANIP = 6;
my $found = 0;
while(<STDIN>){
chomp;
my ($name, $value) = split /:/,$_,2;
next if(not defined $name or not defined $value);
$name =~ s/^\s+|\s+$//g;
$value =~ s/^\s+|\s+$//g;
$host{$name} = $value;
}
open(WORKSTATIONS, "<$workstations");
open(TEMP, ">$temp");
while(<WORKSTATIONS>){
chomp;
if(/^[^;]*;$host{'name'};.*$/){
my (@line) = split /;/,$_,-1;
$line[$IP] = $host{'ip'};
$line[$WLANIP] = $host{'wlanIp'} if defined $host{'wlanIp'};
$_ = join ';', @line;
$found = 1;
}
print TEMP "$_\n";
}
if( not $found and defined $host{'name'} and defined $host{'hwconf'} and defined $host{'mac'} and defined $host{'ip'} ){
my $result = `/usr/sbin/crx_api.sh get devices/byName/$host{'name'}`;
$result = eval { decode_json($result) };
if ($@){
print "decode_json failed, invalid json. error:$@\n";
} else {
$result = `/usr/sbin/crx_api.sh get rooms/$result->{'roomId'}`;
$result = eval { decode_json($result) };
if ($@){
print "decode_json failed, invalid json. error:$@\n";
} elsif( defined $result->{'name'} ){
print TEMP $result->{'name'}.";".$host{'name'}.";".$host{'hwconf'}.";".$host{'mac'}.";".$host{'ip'}.";".$host{'wlanMac'}.";".$host{'wlanIp'}.";;classroom-studentcomputer;;1;\n";
}
}
}
close(TEMP);
close(WORKSTATIONS);
system("rm -f $workstations");
system("mv $temp $workstations");
system("chown root:root $workstations");
system("chmod 644 $workstations");