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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
|
#!/usr/bin/env perl
IO::Socket::SSL::set_ctx_defaults( SSL_verify_mode => SSL_VERIFY_NONE );
package INF::SuperMicro;
use HTTP::Status;
use HTTP::Cookies;
use IO::Socket::SSL qw();
use HTML::TreeBuilder;
use HTTP::Request::Common;
use LWP::UserAgent;
use URI::Escape;
use File::Temp qw/ tempfile tempdir /;
use XML::Simple;
use Data::Dumper;
use JSON::PP;
sub login($) {
my $self = shift;
my $post = POST( $self->{bmc_url} . '/cgi/login.cgi' );
my $form_content =
'name=' . $self->{user} . '&' . 'pwd=' . $self->{password};
$post->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
$post->header( 'Content-Length' => length($form_content) );
$post->content($form_content);
my $res = $self->{ua}->request($post);
unless ( $res->is_success ) {
print STDERR "Login failed - did not get 200\n";
print Dumper($res);
$self->{logged_in} = undef;
return -1;
}
$self->{logged_in} = 1;
#my $json = decode_json( $res->content );
#$self->{skey} = $json->{session_key};
#print "Session key ".$self->{skey}."\n";
return 0;
}
sub view($) {
my $self = shift;
$self->login() unless defined $self->{logged_in};
my $get = GET( $self->{bmc_url}
. '/cgi/url_redirect.cgi?url_name=ikvm&url_type=jwsk' );
my $res = $self->{ua}->request($get);
unless ( $res->is_success ) {
print STDERR "JWSK frequest failed - did not get 200\n";
return -1;
}
my $xml = new XML::Simple;
my $c = $res->content;
$c =~ s/^[\s\n\r]+//s;
my $data = $xml->XMLin($c);
return undef unless defined $data->{'application-desc'};
my $args = $data->{'application-desc'}->{'argument'};
return undef unless defined $args;
mkdir $ENV{HOME} . "/.supermicro_kvm";
chdir $ENV{HOME} . "/.supermicro_kvm";
my $prefix = "/usr/local/share/inf/supermicro/";
return undef unless $c =~ /iKVM__V([A-Za-z0-9\.]+).jar/;
my $v = $1;
if ( -d $prefix . $v ) {
printf "Known version\n";
$prefix .= $v . '/';
}
elsif ( scalar(@$args) == 8 ) {
printf "Unknow version $v, 8 args using 1.69.21\n";
$prefix .= '1.69.21.0x0/';
}
else {
printf "Unknow version $v, 10 args using 1.69.25\n";
$prefix .= '1.69.25.0x0/';
}
if ( defined $self->{proxy_host} ) {
my $tmp = File::Temp->new( UNLINK => 0, SUFFIX => '.cnf' );
select( ( select($tmp), $| = 1 )[0] );
print $tmp "server = 127.0.0.1\n";
print $tmp "server_port = " . $self->{proxy_port} . "\n";
print $tmp "local = 127.0.0.0/255.0.0.0\n";
$ENV{'LD_PRELOAD'} = 'libtsocks.so';
$ENV{'TSOCKS_CONF_FILE'} = $tmp->filename;
print "Filename is $tmp->filename\n";
}
my $jars = [ $prefix . "iKVM__V" . $v . ".jar" ];
my $cp = join( ':', @$jars );
push @$java_args, "-cp";
push @$java_args, $cp;
push @$java_args, "-Djava.library.path=" . $prefix;
push @$java_args, "tw.com.aten.ikvm.KVMMain";
system( "echo", "java", @$java_args, @$args );
system( "java", @$java_args, @$args );
}
sub get_host_power($) {
my ($self) = @_;
$self->login() unless defined $self->{logged_in};
my $post = POST( $self->{bmc_url} . '/cgi/ipmi.cgi' );
my $form_content = 'op=POWER_INFO.XML&r=(0%2C0)';
$post->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
$post->header( 'Content-Length' => length($form_content) );
$post->content($form_content);
my $res = $self->{ua}->request($post);
unless ( $res->is_success ) {
print STDERR " get host power - did not get 200\n";
return undef;
}
my $xml = new XML::Simple;
my $c = $res->content;
$c =~ s/^[\s\n\r]+//s;
my $data = $xml->XMLin($c);
return undef unless defined $data->{'POWER_INFO'};
return undef unless defined $data->{'POWER_INFO'}->{'POWER'};
return undef unless defined $data->{'POWER_INFO'}->{'POWER'}->{'STATUS'};
return $data->{'POWER_INFO'}->{'POWER'}->{'STATUS'};
}
sub set_host_power($$) {
my ( $self, $what ) = @_;
$self->login() unless defined $self->{logged_in};
my $post = POST( $self->{bmc_url} . '/cgi/ipmi.cgi' );
my $form_content = 'op=POWER_INFO.XML&r=(1%2C' . $what . ')';
$post->header( 'Content-Type' => 'application/x-www-form-urlencoded' );
$post->header( 'Content-Length' => length($form_content) );
$post->content($form_content);
my $res = $self->{ua}->request($post);
unless ( $res->is_success ) {
print STDERR " get host power - did not get 200\n";
return 0;
}
my $xml = new XML::Simple;
my $c = $res->content;
$c =~ s/^[\s\n\r]+//s;
my $data = $xml->XMLin($c);
return 0 unless defined $data->{'POWER_INFO'};
return 0 unless defined $data->{'POWER_INFO'}->{'POWER'};
return 0 unless defined $data->{'POWER_INFO'}->{'POWER'}->{'STATUS'};
return 1;
}
sub reset($) {
my $self = shift;
return $self->set_host_power('0');
}
sub port_off($) {
my $self = shift;
return $self->set_host_power('1');
}
sub orderly_shutdown($) {
my $self = shift;
return $self->set_host_power('2');
}
sub port_on($) {
my $self = shift;
return $self->set_host_power('3');
}
sub port_cycle($) {
my $self = shift;
return $self->set_host_power('5');
}
sub port_state_get($$) {
my $self = shift;
return $self->get_host_power();
}
sub port_name_get($$) {
my $self = shift;
return $self->{name};
}
sub name_get($) {
my $self = shift;
return $self->{name};
}
sub pdu_load_get($) {
return "N/A";
}
sub psu_status($) {
return "N/A";
}
sub port_count($) {
return 1;
}
sub port_id_get_by_number($$) {
return "K0";
}
sub logout($) {
my $self = shift;
}
sub new ($;$) {
my ( $class, $parm ) = @_;
my $self;
$self->{ua} = my $ua = LWP::UserAgent->new;
$self->{cookie_jar} = HTTP::Cookies->new();
$self->{ua}->cookie_jar( $self->{cookie_jar} );
$self->{host} = $parm->{host} || "127.0.0.1";
$self->{user} = $parm->{user} || "ADMIN";
$self->{password} = $parm->{password} || "ADMIN";
$self->{name} = $parm->{name} || $self->{host};
$self->{bmc_url} = $parm->{bmc_url}
|| 'https://' . $self->{host};
$self->{ua}->ssl_opts(
SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE,
verify_hostname => 0,
);
if ( defined $parm->{proxy_host} ) {
$self->{ua}->proxy( [qw(http https)] => "socks://"
. $parm->{proxy_host} . ":"
. $parm->{proxy_port} );
$self->{proxy_host} = $parm->{proxy_host};
$self->{proxy_port} = $parm->{proxy_port};
}
$self->{logged_in} = undef;
return bless $self, $class;
}
1;
|