阿里云API学习

本次使用的是python的SDK包来调用阿里云的API的。

 

1、 添加一台ecs必要的参数和使用sdk方法

创建ECS需要用到CreateInstanceRequest对象

代码如下:


import json
from aliyunsdkcore import client
from aliyunsdkecs.request.v20140526 import CreateInstanceRequest

clt = client.AcsClient(‘xxxxxxx’,’xxxxxxxxxxx’,’cn-hangzhou’) #指定accesskey
request = CreateInstanceRequest.CreateInstanceRequest()#调用创建ecs实例对象
request.set_accept_format('json') #选择数据交互为json
request.set_ImageId('m-23f60esz1') #调用加载镜像的ID
request.set_InstanceType('ecs.s3.large') #实例的具体类型,此为4C8G的ECS
request.set_SecurityGroupId('sg-235vv3b08') #添加的安全组ID
request.set_InternetChargeType('PayByTraffic') #指定网络计费类型,此为按流量计费
request.set_InternetMaxBandwidthIn('1') #指定内网带宽,取值是[0,100]
request.set_InternetMaxBandwidthOut('1') #指定外网带宽,取值是[0,100]
request.set_Password('Wang=1234') #指定ECS实例密码,需要8位含大小写字符加数字
result = clt.do_action(request) #执行以上调用对象操作
newecsinfo = json.loads(result) #转换数据类型

Continue reading

perl使用 Net::SSH::Expect修改主机密码

#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Expect;
my @ssh_list;
while (<>) {
@ssh_list=split/\t+/,$_;
print $ssh_list[0]."正在修改密码\n";
&ssh_test("$ssh_list[0]","$ssh_list[1]","$ssh_list[2]","$ssh_list[3]","$ssh_list[4]");
}
sub ssh_test(){
my ( $host, $port, $user, $pass) = @_;
my $ssh = Net::SSH::Expect->new(
host => $host,
port => $port,
password => $pass,
user => $user,
no_terminal => 0,
raw_pty => 1,
timeout => 6,
);
$ssh->debug(0);
$ssh->run_ssh() or die "SSH process couldn't start: $!";
$ssh->waitfor( '\(yes\/no\)\?$', 2 );
$ssh->send("yes\n");
$ssh->waitfor( 'password:\s*$/', 2);
$ssh->send("$ssh_list[3]");
$ssh->waitfor( '#\s', 2 );
$ssh->send("passwd $ssh_list[2]");
$ssh->waitfor( 'password:\s*$', 2 );
$ssh->send("$ssh_list[4]");
$ssh->waitfor( 'password:\s*$', 2 );
$ssh->send("$ssh_list[4]");
$ssh->waitfor( '#\s*', 2 );
$ssh->close();
print "修改完成\n";
print "-" x 30, "\n";
}