Perl脚本来解析需要身份验证的Jenkins作业(config.xml)文件 [英] Perl script to parse Jenkins job (config.xml) files which requires authentication
问题描述
我正在尝试解析Jenkins作业"config.xml"文件.
I'm trying to parse Jenkins job "config.xml" files.
这是我尝试使用 HTTP :: Tiny :
use HTTP::Tiny;
my $page = "http://localhost:8080/job/Job_One_Name/config.xml";
system("start $page"); # this loads the xml file in browser successfully
my $wsdlResponse = HTTP::Tiny->new->get($page);
$wsdlResponse->{success} or die print $logger->error_die($!);
my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});
print $wsdlXML;
它死于错误错误的文件描述符" .
如果我注释掉该行,则脚本将在parse_file行处以此中断 错误消息:
If I comment out that line, the script breaks at the parse_file line with this error message:
Could not create file parser context for file "<html><head><meta http-equiv='refresh' content='1;url=/login?from=%2Fjo
b%2FJob_One_Name/config.xml'/><script>window.location.replace('/login?from=%2Fjob%2FJob_One_Name/config.xml');</script></head><body style='background-color:white; color:white;'>
Authentication required
<!--
You are authenticated as: anonymous
Groups that you are in:
Permission you need to have (but didn't): hudson.model.Hudson.Read
... which is implied by: hudson.security.Permission.GenericRead
... which is implied by: hudson.model.Hudson.Administer
-->
</body></html>
": Result too large at Job.pm line 67.
我还在网上的不同示例中尝试了 XML :: Twig 和 LibXML-> load_xml 和 load_html ,但是它们都死于与无法为文件创建文件解析器上下文" ,错误的文件描述符" 或指定位置,字符串或IO"类似的错误 em>.
I've also tried XML::Twig and LibXML->load_xml and load_html from different examples online, but they all die with a error similar to "Could not create file parser context for file", "Bad file descriptor", or "specify location, string, or IO".
关于可能是什么问题的任何想法?预先感谢.
Any ideas on what could be the issue? Thanks in advance.
更新:
遵循Jenkins
Following the 'Perl LWP example for a scripted client' from Jenkins' Authenticating scripted clients, I've successfully authenticated:
my $uagent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new());
my %options = {'myUserName' => 'myPassword'};
$req = HTTP::Request->new( GET => $serverHost );
$req->authorization_basic( 'myUserName', 'myPassword' );
my $res = $uagent->request($req);
# Check the outcome of the response
print "Result: " . $res->status_line . "\n";
print $res->headers->as_string;
print "\n";
if ( !$res->is_success ) {
print "Failed\n";
} else {
print "Success!\n";
# Parse XML
my $wsdlResponse = HTTP::Tiny->new->get($page, \%options);
#$dom = XML::LibXML->load_xml(location=>$page);
#print $dom . "\n";
my $wsdlXML = XML::LibXML->new->parse_file($wsdlResponse->{content});
print $wsdlXML;
#print $res->content, "\n";
}
它仍然给我同样的需要身份验证" 错误.我不知道如何使用成功的授权请求来解析文件.
It's still giving me the same 'Authentication required' error. I don't know how to use the successfully authorized request to then parse the file.
推荐答案
我想出了一种使用经过身份验证的请求输出结果的方法.由于请求会输出xml内容以及标头响应,即:
I figured out a way to use the authenticated request to output results. Since the request outputs the xml content as well as the headers response, i.e:
HTTP/1.1 200 OK
Connection: close
Server: Jetty(8.y.z-SNAPSHOT)
Content-Type: application/xml
Client-Date: Wed, 30 Jul 2014 14:49:12 GMT
Client-Peer: 127.0.0.1:8080
Client-Response-Num: 1
<?xml version...
<project>
....
....
</project>
我使用正则表达式提取 only 输出的xml部分并将其保存到文件中.从那里,我使用LibXML的 parse_file 方法来解析新编写的文件. (我有一个单独的子例程,该子例程使用 findnodes 方法提取要提取的特定节点.由于该解决方案处理了我遇到的身份验证问题,所以我没有发布它.)
I used a regular expression to extract only the xml part of the output and save it to file. From there I used LibXML's parse_file method to parse the newly written file. (I have a separate subroutine that uses the findnodes method to extract particular nodes I would like to extract. I didn't post it since this solution handles the authentication I was having trouble with).
解决方案:
use strict;
use warnings;
use XML::LibXML;
use Path::Class;
use LWP;
use HTTP::Cookies;
my $serverXmlUrl = "http\://localhost:8080/job/jobName/config.xml";
my $uagent = LWP::UserAgent->new(cookie_jar => HTTP::Cookies->new());
my $request = HTTP::Request->new( GET => $serverXmlUrl );
$request->authorization_basic( 'UserName', 'Password' );
my $result = $uagent->request($request);
if ( !$result->is_success ) {
print "Failed\n";
} else {
print "Success!\n";
# Create new file to write to
my $dir = dir(".");
my $file = $dir->file("job.xml");
my $file_handle = $file->openw();
my $xml_content = $result->as_string; # full result output
# Regex to store only xml content
$xml_content =~ s/.*(<\?xml.*)/$1/s;
# Save xml_content to file
$file_handle->print($xml_content);
$file_handle->close();
# Parse newly written file
print "Parsing file... \n";
my $parser = XML::LibXML->new();
my $doc = $parser->parse_file($file);
print $doc;
}
这篇关于Perl脚本来解析需要身份验证的Jenkins作业(config.xml)文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!