【问题标题】:Error Cisco Prime HTTP GET request错误 Cisco Prime HTTP GET 请求
【发布时间】:2017-06-27 18:14:32
【问题描述】:

我正在尝试使用 Cisco Prime 发出 HTTP GET 请求:

#!/opt/local/bin/perl -w

use strict;

use JSON-support_by_pp;
use LWP 5.64;
use LWP::UserAgent;

$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;

my $ua = LWP::UserAgent->new;

my $BASE_URL = 'https://Host_name/webacs/api/v1/';
my $UN       = "Username";
my $PW       = "Password";

sub fetch ($) {
    my ( $url ) = @_;
    my $req = HTTP::Request->new( GET => $BASE_URL . $url );
    $req->authorization_basic( $UN, $PW );
    return $ua->request( $req )->content or die( "Cannot read from " . $BASE_URL . $url );
}

my $content = fetch( 'data/AccessPoints.json?.full=true' );

my $json = new JSON;

# these are some nice json options to relax restrictions a bit:
my $json_text =
        $json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode( $content );

foreach my $ap ( @{ $json_text->{queryResponse}->{'entity'} } ) {

    print "------------------------\nAccess Point " . $ap->{'accessPointsDTO'}->{'@id'} . "\n";
    print "Model:" . $ap->{'accessPointsDTO'}->{'model'} . "\n";
    print "MAC Address:" . $ap->{'accessPointsDTO'}->{'macAddress'} . "\n";
    print "Serial Number:" . $ap->{'accessPointsDTO'}->{'serialNumber'} . "\n";
    print "Software Version:" . $ap->{'accessPointsDTO'}->{'softwareVersion'} . "\n";
    print "Status:" . $ap->{'accessPointsDTO'}->{'status'} . "\n";
    print "Location:" . $ap->{'accessPointsDTO'}->{'location'} . "\n";

我做错了什么?我已经在 shell 中尝试过 curl 并且它可以工作:

    curl --tlsv1 --user USER:PASSWORD--insecure https://Host_name/webacs/api/v1/data/AccessPoints.json?.full=true

但我的 Perl 脚本不起作用。

我有这个错误:

格式错误的 JSON 字符串,既不是数组、对象、数字、字符串或原子,也不是 ersteProbe.pl 第 28 行的字符偏移量 0(在“无法连接到 10....”之前)。

已经修复。谢谢鲍罗丁 :)

新问题: 我需要对 Cisco Prime 进行身份验证。 代码已经可以工作,但身份验证不起作用。

我有错误

    500 Can't connect to 10.10.10.10:443 (certificate verify failed) at ersteProbeAuth.pl line 27.

第 27 行:

      die $res->status_line unless $res->is_success;

我是 Perl 的新手,无法自己解决这个问题。如果你有Idee,我会很高兴:)

    #!/opt/local/bin/perl -w
    use strict;
    use warnings;
    use JSON -support_by_pp;
    use LWP 5.64;
    use LWP::UserAgent;
    use MIME::Base64;
    use REST::Client;

    $ENV{PERL_LWP_SSL_VERIFY_HOSTNAME} = 0;
    my $ua = LWP::UserAgent->new;

    my $BASE_URL = 'https://10.10.10.10/webacs/api/v1/';
    my $UN='admin';
    my $PW='admin';
    # coding with Base 64
    my $sys_id='Balalalalalal';
    my $encoded_auth = encode_base64("$UN:$PW", '');

    sub fetch {
        my ($url) = @_;
        my  $res = $ua->get($BASE_URL . $url,
             {'Authorization' => "Basic $encoded_auth",
              'Accept' => 'application/json'});
       die $res->status_line unless $res->is_success;
       my $json = $res->decoded_content;
       return $json
    }
    my $content = fetch('data/AccessPoints.json?.full=true/$sys_id');
    my $json = new JSON;
    # these are some nice json options to relax restrictions a bit:  my$json_text=$json->allow_nonref->utf8->relaxed->escape_slash->loose->allow_singlequote->allow_barekey->decode($content);
   foreach my $ap (@{$json_text->{queryResponse}->{'entity'}}){
   print "------------------------\nAccess Point ".$ap->{'accessPointsDTO'}->{'@id'}."\n";
   print "Model:".$ap->{'accessPointsDTO'}->{'model'}."\n";
   print "MAC Address:".$ap->{'accessPointsDTO'}->{'macAddress'}."\n";
   print "Serial Number:".$ap->{'accessPointsDTO'}->{'serialNumber'}."\n";
   print "Software Version:".$ap->{'accessPointsDTO'}->{'softwareVersion'}."\n";
   print "Status:".$ap->{'accessPointsDTO'}->{'status'}."\n";
   print "Location:".$ap->{'accessPointsDTO'}->{'location'}."\n";
   }

【问题讨论】:

  • 你的 Perl 代码是一团糟,你不可能确信它是正确的。这次我已经为您修复了它,以便我可以阅读它,但请在将来发布清晰的代码。缺少右大括号 } 并生成警告 Possible precedence issue with control flow operator 您没有告诉我们
  • 永远不要使用像 sub fetch ($) { ... } 这样的 Perl 子例程原型。它们不会按照您的想法去做,而 sub fetch { ... } 是正确的。
  • 缺少右括号真的很愚蠢} 抱歉。你能看一下我的身份验证吗?
  • 这是一个单独的问题,应该作为一个新问题发布。但是看看LWP::UserAgent and Basic Authentication。顺便说一句,请通过编辑问题添加新信息,尤其是代码;几乎不可能在评论中阅读。
  • 已经完成。但在这种情况下,我应该发布新问题还是只编辑旧问题?

标签: perl


【解决方案1】:

在控制台中从服务器打印您的原始答案。

格式错误的 JSON 字符串,既不是数组、对象、数字、字符串也不是原子,在字符偏移量 0 处(在“无法连接到 10....”之前)

“无法连接到 10....”

也许,你的代码没有连接

【讨论】:

    【解决方案2】:

    如果不访问该网页,就很难判断出了什么问题,但几乎可以肯定您的请求失败了

    我建议你用这个替换你的 fetch 子例程

    sub fetch {
        my ( $url ) = @_;
    
        my $res = $ua->get( $BASE_URL . $url );
        die $res->status_line unless $res->is_success;
    
        my $json = $res->decoded_content;
    
        return $json;
    }
    

    【讨论】:

      猜你喜欢
      • 2021-07-31
      • 1970-01-01
      • 2015-11-18
      • 2021-09-12
      • 2014-08-18
      • 1970-01-01
      • 1970-01-01
      • 2019-02-10
      • 1970-01-01
      相关资源
      最近更新 更多