-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinbo-modify-dhcpd.pl
executable file
·163 lines (147 loc) · 5.5 KB
/
linbo-modify-dhcpd.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
#!/usr/bin/perl -w
# linbo-[add|delete|modify]-dhcpd.pl
#
# Update dhcpd database entries for host after add/modify/delete device
#
use strict;
use JSON::XS;
my $result;
my $device_id;
my $tool;
my %host;
my $operation = $0;
$operation =~ s/.*linbo-([a-zA-Z]+)-dhcpd.pl$/$1/;
my $USE_DB = '0';
my $LOGFILE = '/root/linbo-'.$operation.'-dhcpd.pl.log';
my $DEBUG = '1';
sub get_bootfilename($)
{
my $gruppe = shift || 'netbook';
open(CONF,"</srv/tftp/start.conf.$gruppe");
my $systemtype = 'bios';
while(<CONF>){
chomp;
my ( $type ) = lc($_) =~ /^systemtype\s=\s(bios|bios64|efi32|efi64)/;
if( defined $type ){
$systemtype = $type;
last;
}
}
if( $systemtype =~ /bios64/ )
{
return "boot/grub/i386-pc/core.0";
} elsif( $systemtype =~ /efi32/ )
{
return "boot/grub/i386-efi/core.efi";
} elsif( $systemtype =~ /efi64/ )
{
return "boot/grub/x86_64-efi/core.efi";
} else {
return "boot/grub/i386-pc/core.0";
}
}
sub write_file($$) {
my $file = shift;
my $out = shift;
local *F;
open F, ">$file" || die "Couldn't open file '$file' for writing: $!; aborting";
binmode F, ':encoding(utf8)';
local $/ unless wantarray;
print F $out;
close F;
}
open(LOG,">>$LOGFILE") if $DEBUG;
print LOG "=============================\n" if $DEBUG;
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;
print LOG "INPUT: $name = $value\n" if $DEBUG;
}
if( not defined $host{'hwconf'} or not defined $host{'hwconfid'} or not defined $host{'name'} ){
exit(0);
}
# Linbo host ?
$tool = '';
my $str = `/usr/sbin/crx_api_text.sh GET clonetool/$host{'hwconfid'}/partitions`;
print LOG "partitions: $str\n" if $DEBUG;
my @a = split / /,$str;
for my $p (@a){
$result = `/usr/sbin/crx_api_text.sh GET clonetool/$host{'hwconfid'}/$p/ITOOL`;
print LOG "partition: $p tool: $result\n" if $DEBUG;
if( $result eq 'Linbo' ){
$tool = $result;
last;
}
}
if( $tool ne 'Linbo' and $operation ne 'modify' ){
exit(0);
}
# Device id?
$result = `/usr/sbin/crx_api.sh GET devices/byName/$host{'name'}`;
$result = eval { decode_json($result) };
if ($@) {
die( "decode_json failed, invalid json. error:$@\n" );
}
$device_id = $result->{'id'};
my $bootfile = get_bootfilename($host{'hwconf'});
if( $operation eq 'modify' or $operation eq 'delete' ){
$result = `/usr/sbin/crx_api.sh GET devices/$device_id/dhcp`;
print LOG "old dhcp entries: $result\n" if $DEBUG;
$result = eval { decode_json($result) };
for my $entry (@{$result}){
my $r = `/usr/sbin/crx_api.sh DELETE devices/$device_id/dhcp/$entry->{'id'}`;
}
`/usr/sbin/crx_api.sh PUT devices/refreshConfig` if $operation eq 'delete' or $tool ne 'Linbo';
print "old dhcp entries for $host{'name'} deleted.\n";
}
if( $tool ne 'Linbo' ){
exit(0);
}
if( $operation eq 'add' or $operation eq 'modify'){
my $file = `mktemp /tmp/linbo_write_dhcpdXXXX.txt`;
my $dhcppath;
my $dhcpboot;
if( $USE_DB ){
$dhcppath = {"objectType" => "Device", "objectId" => $device_id, "keyword" => "dhcpStatements", "value" => "option extensions-path \\\"$host{'hwconf'}\\\""};
$dhcpboot = {"objectType" => "Device", "objectId" => $device_id, "keyword" => "dhcpStatements", "value" => "filename \\\"$bootfile\\\""};
} else {
$dhcppath = {"objectType" => "Device", "objectId" => $device_id, "keyword" => "dhcpStatements", "value" => "option extensions-path \"$host{'hwconf'}\""};
$dhcpboot = {"objectType" => "Device", "objectId" => $device_id, "keyword" => "dhcpStatements", "value" => "filename \"$bootfile\""};
}
write_file("$file", encode_json($dhcppath));
if( $USE_DB eq '1' ){
`echo "INSERT INTO CrxMConfig(objectType,objectId,keyword,value,creator_id) VALUES('Device',$device_id,'dhcpStatements','$dhcppath->{value}',1);" |mysql CRX`;
print LOG "INSERT INTO CrxMConfig(objectType,objectId,keyword,value,creator_id) VALUES('Device',$device_id,'dhcpStatements','$dhcppath->{value}',1);\n" if $DEBUG;
$result = "OK";
} else {
$result = `/usr/sbin/crx_api_post_file.sh devices/$device_id/dhcp $file\n`;
print LOG "ADD dhcppath: $result\n" if $DEBUG;
$result = eval { decode_json($result) };
if ($@) {
die( "decode_json failed, invalid json. error:$@\n" );
}
$result = "OK" if( $result->{"code"} eq "OK" );
}
write_file("$file", encode_json($dhcpboot));
if( $USE_DB eq '1' ){
`echo "INSERT INTO CrxMConfig(objectType,objectId,keyword,value,creator_id) VALUES('Device',$device_id,'dhcpStatements','$dhcpboot->{value}',1);" |mysql CRX`;
print LOG "INSERT INTO CrxMConfig(objectType,objectId,keyword,value,creator_id) VALUES('Device',$device_id,'dhcpStatements','$dhcpboot->{value}',1);\n" if $DEBUG;
$result = "OK";
} else {
$result = `/usr/sbin/crx_api_post_file.sh devices/$device_id/dhcp $file\n`;
print LOG "ADD dhcpboot: $result\n" if $DEBUG;
$result = eval { decode_json($result) };
if ($@) {
die( "decode_json failed, invalid json. error:$@\n" );
}
$result = "OK" if $result->{"code"} eq "OK";
}
`/usr/sbin/crx_api.sh PUT devices/refreshConfig` if $USE_DB eq '1';
`rm -f $file`;
print "new dhcp entries for $host{'name'} created.\n";
close(LOG) if $DEBUG;
}