2017-02-09 113 views
0

我試圖與思科Prime HTTP GET請求:錯誤思科P​​rime 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"; 

我該怎麼辦錯了嗎?我已經嘗試在殼上捲曲,它的工作原理如下:

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

但我的Perl腳本不起作用。

我有這樣的錯誤:

malformed JSON string, neither array, object, number, string or atom, at character offset 0 (before "Can't connect to 10....") at ersteProbe.pl line 28.

修復了。謝謝鮑羅廷:)

新問題: 我需要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 UND而新水溼解決這個問題我自己。如果你有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"; 
    } 
+0

你的Perl代碼是一個可怕的混亂,你不可能確信它是正確的。我這次爲你解決了這個問題,這樣我就可以閱讀它,但是請將來發布可讀的代碼。有一個缺失的大括號'}',它會生成警告'控制流操作符可能的優先級問題',您沒有告訴我們有關 – Borodin

+0

不要使用像'sub fetch($){...}這樣的Perl子例程原型'他們不會做你認爲他們做的事情,只是「sub fetch {...}」是正確的。 – Borodin

+0

非常感謝! – StayCalm

回答

-1

從控制檯打印你的原始答案。

畸形JSON字符串,既不陣列,對象,數字,字符串或原子,在字符偏移0(前「不能連接到10 ......」)

"Can't connect to 10...."

也許,您的代碼是沒有連接

+0

請不要顯示你的JSON數據 – Andrey

0

這很難說什麼是錯的,而不訪問網頁,但幾乎可以肯定你的請求已失敗

我建議你用這個

取代你 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; 
}