perl 获取阿里云主机信息

use  LWP::UserAgent;
use Data::UUID;
use Data::GUID;
use HTTP::Date qw(time2iso str2time time2iso time2isoz);  
use HTTP::Cookies;
use POSIX;
use Data::Dumper;
use JSON qw/encode_json decode_json/;

 sub _init{
my $uuid  = Data::GUID->new->as_string;
#print "\$uuid is $uuid\n";
$region=shift;
$Action=shift;
use Encode;
my $SDATE = strftime("%Y-%m-%dT%H:%M:%SZ",localtime());
use URI::Escape;  
$SDATE=uri_escape("$SDATE"); 
#print "\$SDATE is $SDATE\nX";
$SDATEX = $SDATE;
$SDATEX =~ s#%#%25#g;
#print "\$SDATEX is $SDATEX\n";


use URI::Escape qw(uri_unescape);
use MIME::Base64;
use Digest::HMAC_SHA1;
$xx="GET&%2F&AccessKeyId%3DZ7bph8ZgNR4oMNVh%26Action%3D$Action%26Format%3DJSON%26RegionId%3D$region%26SignatureMethod%3DHMAC-SHA1%26SignatureNonce%3D$uuid%26SignatureVersion%3D1.0%26TimeStamp%3D$SDATEX%26Version%3D2014-05-26";
$xx = encode_utf8($xx);
#print "\$xx is $xx\n";
$str=Digest::HMAC_SHA1::hmac_sha1("$xx","xxxx;
$Signature = encode_base64($str,"");
#print "$Signature\n";
$Signature = encode_utf8($Signature);
 

$head='http://ecs.aliyuncs.com/?';
#+ ͦۻΪ%20 =ͦۻΪ%3d
$Signature = uri_escape("$Signature");

$api="SignatureVersion=1.0&Format=JSON&TimeStamp=$SDATE&RegionId=$region&AccessKeyId=Z7bph8ZgNR4oMNVh&SignatureMethod=HMAC-SHA1&Version=2014-05-26&Signature=$Signature&Action=$Action&SignatureNonce=$uuid";
$url=$head.$api;
#print "\$url is $url\n";
};

sub get_url{
$url=shift;
$ua = LWP::UserAgent->new;
$ua->timeout(10);
$ua->env_proxy;
$ua->agent("Mozilla/8.0");
my $cookie_jar = HTTP::Cookies->new(
    file           => 'lwp_cookies.txt',
    autosave       => 1,
    ignore_discard => 1
);
$ua->cookie_jar($cookie_jar);

#
#
my $response = $ua->get($url);


if ($response->is_success) {
$yy=$response->decoded_content;
# print $yy;  # or whatever
  }
  else {
#     print $response->decoded_content;  # or whatever
        die $response->status_line;
           }
           };
&_init(region1,DescribeRegions);
&get_url($url);

&_init('cn-hangzhou',DescribeInstances);
&get_url($url);
my $d= $yy;
$d = decode_json($d);
$xx=$d->{Instances}->{Instance}
;
foreach (@{$xx}) {
print "InstanceID is $_->{InstanceId}\n";
print "ExpiredTime is $_->{ExpiredTime}\n";
print "IPaddr is $_->{PublicIpAddress}->{IpAddress}->[0]\n"};

posted @ 2016-02-18 18:09  czcb  阅读(202)  评论(0编辑  收藏  举报