]> git.zerfleddert.de Git - rsbs2/blob - supermicro-kvm.pl
supermicro-kvm: handle redirects to https
[rsbs2] / supermicro-kvm.pl
1 #!/usr/bin/perl -w
2
3 use LWP::UserAgent;
4 use LWP::ConnCache;
5 use IO::Socket::SSL;
6 use XML::Simple;
7 use Data::Dumper;
8 use File::Temp;
9
10 my $ua = LWP::UserAgent->new(cookie_jar => {},
11 ssl_opts => {verify_hostname => 0,
12 SSL_verify_mode => IO::Socket::SSL::SSL_VERIFY_NONE});
13 $ua->default_header('Referer' => 'http://localhost');
14 push @{ $ua->requests_redirectable }, 'POST';
15
16 sub login {
17 my $url = shift;
18 my $user = shift;
19 my $pass = shift;
20
21 my $login = { 'name' => $user, 'pwd' => $pass };
22 my $response = $ua->post("${url}/cgi/login.cgi", $login);
23 die $response->status_line if (!($response->is_success));
24 }
25
26 sub read_inifile {
27 my $filename = shift;
28
29 open(INIFILE,"<${filename}") || die("can't open config: ${filename}: $!");
30 my %Ini = ();
31 my @sections = ();
32 while(<INIFILE>) {
33 chomp;
34
35 next if (m/^#/);
36
37 if (m/^\s*\[(.*)\]\s*$/) {
38 push @sections, $1;
39 next;
40 }
41
42 if (@sections) {
43 if (m/^\s*([^=]+)\s*=\s*(.*)\s*$/) {
44 ${$Ini{$sections[$#sections]}}{$1} = $2;
45 }
46 }
47 }
48 close(INIFILE);
49
50 %Ini;
51 }
52
53 sub add_url_to_java_exceptions {
54 my $url = shift || die;
55 my $file = "$ENV{HOME}/.java/deployment/security/exception.sites";
56 return unless -f ${file};
57 open my $fh, '+<', $file || die;
58 unless (grep{m#${url}#} <$fh>){
59 close $fh;
60 if (open my $fh, '>>', $file) {
61 print $fh "${url}\n";
62 close $fh;
63 }
64 } else {
65 close $fh;
66 }
67 }
68
69 my %Config = read_inifile("$ENV{HOME}/.rsbs2rc");
70
71 my $hostalias = $ARGV[0];
72
73 if (!defined($hostalias) || !defined($Config{$hostalias})) {
74 print STDERR "Usage: $0 card-alias\n\n";
75 print STDERR "card-alias\tone of:\n";
76 foreach my $alias (sort keys(%Config)) {
77 print STDERR "\"${alias}\"\n";
78 }
79 print STDERR "(see ~/.rsbs2rc)\n";
80 exit(1);
81 }
82
83 my $url = "http://" . ${$Config{$hostalias}}{"host"};
84 login($url, ${$Config{$hostalias}}{"user"}, ${$Config{$hostalias}}{"pass"});
85 add_url_to_java_exceptions($url);
86
87 my $response = $ua->get("${url}/cgi/url_redirect.cgi?url_name=ikvm&url_type=jwsk");
88 die $response->status_line if (!($response->is_success));
89
90 my $jnlp = $response->decoded_content;
91
92 $jnlp =~ s/(<resources os=\"Linux\" arch=\"amd64\">)/$1<property name=\"jnlp.packEnabled\" value=\"true\"\/><property name=\"jnlp.versionEnabled\" value=\"true\"\/>/;
93
94 my $fh = File::Temp->new(SUFFIX => '.jnlp');
95 $fh->unlink_on_destroy(1);
96
97 print $fh $jnlp;
98
99 $ENV{'AWT_TOOLKIT'} = 'MToolkit';
100 system("javaws", $fh->filename);
101 sleep(1);
Impressum, Datenschutz