From 4515d197d903405678cf26e8d4d7e1b12c13b810 Mon Sep 17 00:00:00 2001 From: XuWei Date: Tue, 23 May 2017 02:34:15 -0400 Subject: [PATCH 1/2] Use LWP to send https request in bmcdiscover instead of curl command --- xCAT-server/lib/xcat/plugins/bmcdiscover.pm | 25 +++++++++++++-------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/bmcdiscover.pm b/xCAT-server/lib/xcat/plugins/bmcdiscover.pm index 4f6bce095..d0e58e533 100644 --- a/xCAT-server/lib/xcat/plugins/bmcdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/bmcdiscover.pm @@ -29,6 +29,9 @@ use Data::Dumper; use File::Basename; use File::Path; use Cwd; +use LWP; +use HTTP::Cookies; +use HTTP::Response; use JSON; my $nmap_path; @@ -1040,14 +1043,20 @@ sub bmcdiscovery_openbmc{ my $node = sprintf("node-%08x", unpack("N*", inet_aton($ip))); my $node_data = $ip; - my $cjar_file = "/tmp/cjar_$ip"; + my $brower = LWP::UserAgent->new( ssl_opts => { verify_hostname => 0 }, ); + my $cookie_jar = HTTP::Cookies->new(); + my $header = HTTP::Headers->new('Content-Type' => 'application/json'); + my $url = "https://$ip/login"; my $data = '{"data": [ "' . $openbmc_user .'", "' . $openbmc_pass . '" ] }'; - - my $output = `curl -c $cjar_file -k -X POST -H \"Content-Type: application/json\" -d '$data' https://$ip/login`; + $brower->cookie_jar($cookie_jar); + my $request = HTTP::Request->new( 'POST', $url, $header, $data ); + my $response = $brower->request($request); - if ($output =~ /\"status\": \"ok\"/) { - my $req_output = `curl -b $cjar_file -k https://$ip/xyz/openbmc_project/inventory/system/chassis/motherboard`; - my $response = decode_json $req_output; + if ($response->is_success) { + my $req_url = "https://$ip/xyz/openbmc_project/inventory/system/chassis/motherboard"; + my $get_req = HTTP::Request->new('GET', $req_url, $header); + my $req_output = $brower->request($get_req); + my $response = decode_json $req_output->content; my $mtm; my $serial; @@ -1080,10 +1089,8 @@ sub bmcdiscovery_openbmc{ $node =~ s/(.*)/\L$1/g; $node =~ s/[\s:\._]/-/g; } - - unlink $cjar_file; } else { - if ($output =~ /\"description\": \"Invalid username or password\"/) { + if ($response->content =~ /\"description\": \"Invalid username or password\"/) { xCAT::MsgUtils->message("W", { data => ["Invalid username or password for $ip"] }, $::CALLBACK); } return; From 75cb09d112575c9cb5f56bcb49541d72665052c6 Mon Sep 17 00:00:00 2001 From: XuWei Date: Tue, 23 May 2017 03:41:30 -0400 Subject: [PATCH 2/2] modified depending on comments --- xCAT-server/lib/xcat/plugins/bmcdiscover.pm | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/xCAT-server/lib/xcat/plugins/bmcdiscover.pm b/xCAT-server/lib/xcat/plugins/bmcdiscover.pm index d0e58e533..28e0d951d 100644 --- a/xCAT-server/lib/xcat/plugins/bmcdiscover.pm +++ b/xCAT-server/lib/xcat/plugins/bmcdiscover.pm @@ -1049,13 +1049,14 @@ sub bmcdiscovery_openbmc{ my $url = "https://$ip/login"; my $data = '{"data": [ "' . $openbmc_user .'", "' . $openbmc_pass . '" ] }'; $brower->cookie_jar($cookie_jar); - my $request = HTTP::Request->new( 'POST', $url, $header, $data ); - my $response = $brower->request($request); + my $login_request = HTTP::Request->new( 'POST', $url, $header, $data ); + my $login_response = $brower->request($login_request); - if ($response->is_success) { + if ($login_response->is_success) { my $req_url = "https://$ip/xyz/openbmc_project/inventory/system/chassis/motherboard"; - my $get_req = HTTP::Request->new('GET', $req_url, $header); - my $req_output = $brower->request($get_req); + my $req = HTTP::Request->new('GET', $req_url, $header); + my $req_output = $brower->request($req); + return if ($req_output->is_error); my $response = decode_json $req_output->content; my $mtm; my $serial; @@ -1090,7 +1091,7 @@ sub bmcdiscovery_openbmc{ $node =~ s/[\s:\._]/-/g; } } else { - if ($response->content =~ /\"description\": \"Invalid username or password\"/) { + if ($login_response->status_line =~ /401 Unauthorized/) { xCAT::MsgUtils->message("W", { data => ["Invalid username or password for $ip"] }, $::CALLBACK); } return;