如何模仿wget或卷曲的行为



我是perl和activemq中的新手。

我已经下载了此Perl Nagios程序来检查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

仅在第二次尝试中找到。

lwp :: Useragent不会解析URL的username:password部分。我怀疑这是为了劝阻将用户名放置在URL中的不安全惯例,然后将其轻松从程序和服务器日志中偷走。

您可以覆盖get_basic_credentials以将用户名和密码从URL中拉出。这不能解决安全问题。

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

  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);

相关内容

  • 没有找到相关文章

最新更新