如何在Perl中模仿wget或curl的行为

时间:2017-09-06 12:32:15

标签: perl activemq wget nagios

我是Perl和ActiveMQ的新手。

我已下载此Perl nagios program以检查ActiveMQ队列。问题是程序存在于主要的Perl行中:

my $page = get "http://admin:admin\@$address:$port/admin/xml/queues.jsp" or die "Cannot get XML file: $!\n";;

我用其他行代替该行来检查返回码:

 my $ua = LWP::UserAgent->new;
 $ua->timeout(10);
 $ua->env_proxy;

 my $page = $ua->get("http://admin:admin\@$address:$port/admin/xml/queues.jsp");

 if ($page->is_success) {
     print $page->decoded_content;  # or whatever
 }
 else {
     die $page->status_line;
 }

现在报道:

401 Unauthorized

但是wget仍然可以下载页面:

Connecting to 127.0.0.1:8161... connected.
HTTP request sent, awaiting response... 401 Unauthorized
Reusing existing connection to 127.0.0.1:8161.
HTTP request sent, awaiting response... 200 OK
Length: 2430 (2.4K) [text/xml]
Saving to: `queues.jsp'

如何配置UserAgent以使get来电模仿wget行为?

您是否知道另一个用于监控de ActiveMQ队列的脚本/程序?

有没有办法以纯文本格式获取队列值?然后我会写下我自己的bash脚本。

更新1

正如@mob所要求的那样,这是wget --debug

的输出
DEBUG output created by Wget 1.12 on linux-gnu.

--2017-09-06 19:27:15--  http://admin:*password*@127.0.0.1:8161/admin/xml/queues.jsp
Connecting to 127.0.0.1:8161... connected.
Created socket 3.
Releasing 0x0000000002586c10 (new refcount 0).
Deleting unused 0x0000000002586c10.

---request begin---
GET /admin/xml/queues.jsp HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: 127.0.0.1:8161
Connection: Keep-Alive

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 401 Unauthorized
WWW-Authenticate: basic realm="ActiveMQRealm"
Cache-Control: must-revalidate,no-cache,no-store
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 1293
Connection: keep-alive
Server: Jetty(7.6.9.v20130131)

---response end---
401 Unauthorized
Registered socket 3 for persistent reuse.
Skipping 1293 bytes of body: [<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1"/>
<title>Error 401 Unauthorized</title>
</head>
<body>
<h2>HTTP ERROR: 401</h2>
<p>Problem accessing /admin/xml/queues.jsp. Reason:
<pre>    Unauthorized</pre></p>
<hr /><i><small>Powered by Jetty://</small></i>


</body>
</html>
] done.
Reusing existing connection to 127.0.0.1:8161.
Reusing fd 3.

---request begin---
GET /admin/xml/queues.jsp HTTP/1.0
User-Agent: Wget/1.12 (linux-gnu)
Accept: */*
Host: 127.0.0.1:8161
Connection: Keep-Alive
Authorization: Basic xxxxxxxxxxxxxxxxxxxx

---request end---
HTTP request sent, awaiting response...
---response begin---
HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=o7kaw1kbzcy91dozx82c8dq2j;Path=/admin
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: text/xml;charset=ISO-8859-1
Content-Length: 2430
Connection: keep-alive
Server: Jetty(7.6.9.v20130131)

---response end---
200 OK

Stored cookie 127.0.0.1 8161 /admin <session> <insecure> [expiry none] JSESSIONID o7kaw1kbzcy91dozx82c8dq2j
Length: 2430 (2.4K) [text/xml]
Saving to: `queues.jsp'

100%[================================================================================>] 2,430  --.-K/s in 0s

2017-09-06 19:27:15 (395 MB/s) - `queues.jsp' saved [2430/2430]

两次尝试的---request begin---部分的唯一区别是

 Authorization: Basic xxxxxxxxxxxxxxxxxxxx

仅在第二次尝试中找到。

1 个答案:

答案 0 :(得分:3)

LWP :: UserAgent不解析URL的username:password部分。我怀疑这是为了阻止将用户名:密码放入URL中的不安全做法,然后很容易从程序和服务器日志中窃取它。

您可以覆盖get_basic_credentials以从网址中提取用户名和密码。这并不能解决安全问题。

或者您可以在authorization_basic对象上调用HTTP::Request来设置此特定请求的用户名和密码。

  my $ua = LWP::UserAgent->new;
  my $req = HTTP::Request->new(GET => $url);
  $req->authorization_basic($user, $password);
  my $res = $ua->request($req);

或者您可以在UserAgent上调用credentials来设置各种主机/端口组合的密码,而不是仅针对一个请求。这就像在浏览器中存储您的密码一样,$ua是浏览器。

  my $ua = LWP::UserAgent->new;
  $ua->credentials("$host:$port", $realm, $user, $password);
  my $req = $ua->get($url);

或者您可以切换到功能较少但设计得更好的HTTP::Tiny或稍微更具特色的HTTP::Tiny::UA。它将解析并使用URL的username:password部分。

  use HTTP::Tiny;
  my $ua = HTTP::Tiny->new;
  my $res = $ua->get($url);