Adding subdirectories for remote commands to control ILO and DRAC cards over
[monitor.git] / cmdhttps / locfg.pl
1 #!/usr/bin/perl
2 ###########################################################################
3 ##
4 ## Simplified perl version of CPQLOCFG
5 ## Copyright 2003,2007 Hewlett Packard Development Company, L.P.
6 ##
7 ## To use this program, you must have Net::SSLeay and IO::Socket::SSL
8 ## installed.  You may obtain these modules from http://www.cpan.org/
9 ##
10 ## You may use and modify this program to suit your needs.
11 ##
12 ###########################################################################
13
14 use IO::Socket::SSL;
15 use Getopt::Long;
16
17
18 sub usage
19 {
20         print "Usage:\n";
21         print "    locfg -s server [-l logfile] -f inputfile [-u username -p password]\n";
22         print "    Note: Use -u and -p with caution as command line options are\n";
23         print "          visible on Linux.\n";
24         exit 0;
25 }
26
27 sub usage_err
28 {
29     print "Note:\n";
30         print "  Both username and password must be specified with the -u and -p switches.\n";
31         print "  Use -u and -p with caution as command line options are visible on Linux.\n";
32         exit 0;
33 }
34 ###########################################################################
35 ##
36 ## Process options
37 ##
38 ###########################################################################
39
40 my $host, $logfile, $file, $verbose, $help, $uname, $pword;
41 $verbose = 0;
42 $r = GetOptions("server|s=s" => \$host,
43                 "logfile|l=s" => \$logfile,
44                 "input|f=s" => \$file,
45                 "u=s" => \$uname,
46                 "p=s" => \$pword,
47                 "verbose" => \$verbose,
48                 "help|?" => \$help
49                 );
50
51 if ($help || !$host || !$file) {
52         usage();
53 }
54
55 # Username and Password must be entered together
56 if( ($uname && !($pword)) || (!($uname) && $pword) ) {
57     usage_err();
58 }
59
60 if ($logfile) {
61         # If a logfile is specified, open it and select it as the default
62         # filehandle
63         open(L, ">$logfile") || die "Can't open $logfile\n";
64         select(L);
65 }
66
67 # Set the default SSL port number if no port is specified
68 $host .= ":443" unless ($host =~ m/:/);
69
70 # Open the SSL connection and the input file
71 my $client = new IO::Socket::SSL->new(PeerAddr => $host);
72 if (!$client) {
73         print "ERROR: Failed to establish SSL connection with $host.\n";
74         exit 1;
75 }
76
77 open(F, "<$file") || die "Can't open $file\n";
78
79 # Send the XML header and begin processing the file
80 print $client '<?xml version="1.0"?>' . "\r\n";
81 while($ln=<F>) {
82         # Chomp of any EOL characters
83         $ln =~ s/\r|\n//g;
84
85     # Find LOGIN tag.
86     if ((($ln =~ ?<[    ]*LOGIN[        ]?) || ($ln =~ ?<[      ]*LOGIN$?)) && ($pword) && ($uname)) {
87        while( !($ln =~ m/\>/i) ) {
88           $ln = <F>;
89        }
90        print $client "<LOGIN USER_LOGIN=\"$uname\" PASSWORD=\"$pword\">\n";
91        print "\n<LOGIN USER_LOGIN=\"$uname\" PASSWORD=\"$pword\">\n" if ($verbose);
92        # print "\nOverriding credentials in scripts with those from command line.\n" if ($verbose);
93        next;
94     }
95
96         # Special case: UPDATE_RIB_FIRMWARE violates XML.  Send the full
97         # UPDATE firmware tag followed by the binary firmware image
98         if ($ln =~ m/UPDATE_RIB_FIRMWARE/i) {
99                 if ($ln =~ m/IMAGE_LOCATION=\"(.*)\"/i) {
100                         $firmware = $1;
101                         open(G, "<$firmware") || die "Can't open $firmware\n";
102                         $len = (stat(G))[7];
103                         print $client "\r\n<UPDATE_RIB_FIRMWARE IMAGE_LOCATION=\"$firmware\" IMAGE_LENGTH=\"$len\"/>\r\n";
104                         print "\r\n<UPDATE_RIB_FIRMWARE IMAGE_LOCATION=\"$firmware\" IMAGE_LENGTH=\"$len\"/>\r\n" if ($verbose);
105                         $x = read(G, $buf, $len);
106                         print "Read $x bytes from $firmware\n" if ($verbose);
107                         $x = $client->write($buf, $x);
108                         print "Wrote $x bytes\n" if ($verbose);
109                         close(G);
110                         next;
111                 }
112                 # print "\nError: syntax error detected in $ln\n" if ($verbose);
113         }
114         # Send the script to the iLO board
115         print $ln . "\n" if ($verbose);
116         print $client $ln . "\r\n" ;
117 }
118 close(F);
119
120 print "----\n" if ($verbose);
121
122 # Ok, now read the responses back from iLO
123 while($ln=<$client>) {
124         last if (length($ln) == 0);
125
126         # This isn't really required, but it makes the output look nicer
127         $ln =~ s/<\/RIBCL>/<\/RIBCL>\n/g;
128         print $ln;
129 }
130
131 # All done
132 exit 0;