-
Notifications
You must be signed in to change notification settings - Fork 0
/
getKey.sh
executable file
·108 lines (84 loc) · 2.29 KB
/
getKey.sh
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
#!/usr/bin/perl
use LWP::Simple;
use Sys::Hostname;
if ( ($#ARGV + 1) != 1) {
print Timestamp() . " Default config: plc.conf\n";
$configFile="plc.conf";
} else {
print Timestamp() . "Using " . $ARGV[0] . " as config.\n";
$configFile=$ARGV[0];
}
$platform = hostname();
print "Config: $configFile, ";
$identified=0;
my $content;
open(my $fh, '<', $configFile) or die "cannot open file $configFile";
{
local $/;
$content = <$fh>;
}
close($fh);
if ( grep { /myKEY=1/ } $content ) {
print "and an uninitialized key found.\n";
do "./$configFile";
$URLbase="http://$server_ipaddress/ntas/";
print "$URLbase\n";
$url="$URLbase/listkeys.php";
$reply=get $url;
if ( $reply =~ $platform ) {
print "Platform found.\n";
@replyline = split(/\R/,$reply);
foreach $line (@replyline) {
if ($line=~ $platform) {
if ($identified==1) {
print "$name -- $key -- $comment \n";
}
@data=split(/\s+/,$line);
$name=$data[0];
$key=$data[1];
$comment=$data[2];
#print "$name -- $key -- $comment \n";
$identified++;
if ($identified>1) {
print "$name -- $key -- $comment \n";
}
}
}
if ( $identified>1 ) {
print "Problems multiple platforms found.\n";
print "Things needs to be fixed on the server, $URLbase.\n";
exit(1);
}
##
print "Updating $configFile with $name, $key .\n";
$fileContent=read_file($configFile);
$fileContent =~ s/myKEY=1/myKEY=$key/g;
write_file($configFile,$fileContent);
} else {
print "Platform is missing, make sure its added.\n"
}
} else {
print "seems to contain a valid key.\n"
}
sub Timestamp {
my ($sec,$min, $hour, $mday, $mon,$year,$wday,$yday,$isdst);
($sec,$min, $hour, $mday, $mon,$year,$wday,$yday,$isdst)=localtime(time);
$year+=1900;
$mon+=1;
return "$year $mon $mday $hour:$min:$sec ";
}
sub read_file {
my ($filename) = @_;
open my $in, '<:encoding(UTF-8)', $filename or die "Could not open '$filename' for reading $!";
local $/ = undef;
my $all = <$in>;
close $in;
return $all;
}
sub write_file {
my ($filename, $content) = @_;
open my $out, '>:encoding(UTF-8)', $filename or die "Could not open '$filename' for writing $!";;
print $out $content;
close $out;
return;
}