云助手命令可以在多台ECS实例中批量执行Shell、Bat或者PowerShell脚本。相较于SSH/RDP模式,您可以使用云助手通过Java开发环境直接为云端的ECS实例进行运维操作。

前提条件

  • 目标ECS实例已安装云助手客户端。具体步骤,请参见云助手客户端
  • 已更新Java开发环境中的SDK依赖aliyun-java-sdk-ecs至4.18.3或以上版本。更多详情,请前往Maven项目获取最新版本。

操作步骤

  1. 根据ECS实例的配置,以及您需要执行的操作,编写Shell、Bat或者PowerShell命令。
    例如,以下Shell命令示例可以升级ECS实例中的Python开发环境。
    wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz
    rm -rf /usr/bin/python3 
    tar -xzvf Python-3.7.0.tgz
    cd Python-3.7.0
    /configure && make && make install
    ln -s /usr/local/bin/python3.7 /usr/bin/python3
    ln -s /usr/local/bin/python3.7-config /usr/bin/python-config
    python3 -V

    更多命令示例,请参见查看实例系统配置修改实例配置与安装应用

  2. 查询满足条件的目标ECS实例。
    ECS实例状态必须为运行中Running)。查询实例的Java SDK示例请参见查询ECS实例
  3. 获取账号AccessKey,以及查询地域ID。
    具体步骤,请参见地域和可用区创建AccessKey
  4. 新建一个RunCommandSample.java类,为一台或多台系统配置相同的ECS实例执行云助手命令。
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.ecs.model.v20140526.RunCommandRequest;
    import com.aliyuncs.ecs.model.v20140526.RunCommandResponse;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    import java.util.ArrayList;
    import java.util.List;
    
    public class RunCommandsSmaple {
        public static void main(String[] args) {
            // 初始化profile对象,并设置地域ID(例如cn-hangzhou)以及API密钥(accessKeyId和accessKeySecret)。
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
    
            IAcsClient client = new DefaultAcsClient(profile);
            RunCommandRequest request = new RunCommandRequest();
    
            // request.setRegionId("cn-hangzhou");
            request.setName("upgradePython");
            request.setType("RunShellScript");
            // 您编写的云助手命令内容,支持Shell、Bat以及PowerShell命令。
            request.setCommandContent("wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tgz && rm -rf /usr/bin/python3 \n tar -xzvf Python-3.7.0.tgz  \n cd Python-3.7.0 \n ./configure && make && make install\nln -s /usr/local/bin/python3.7 /usr/bin/python3\nln -s /usr/local/bin/python3.7-config /usr/bin/python-config\npython3 -V");
            // 如果您在CommandContent中使用的是明文命令显示,请设置为PlainText。
            request.setContentEncoding("PlainText");
            // 由于需要下载安装包可能耗费较长时间,建议您设置合适的超时时间,避免远程命令由于超时失败。默认超时时间为60s。
            request.setTimeout(600L);
    
            // 一台或多台ECS实例组成的ID列表。
            List<String> instanceIdList = new ArrayList<String>();
            instanceIdList.add("i-bp155lkyo1ut0d******");
            request.setInstanceIds(instanceIdList);
    
            // 处理报错和应答。
            try {
                RunCommandResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }
    返回结果如下所示,并记录InvokeId
    {
        "RequestId": "473469C7-AA6F-4DC5-B3DB-A3DC0DE3C83E",
        "InvokeId": "t-hz0b22o6******",
        "CommandId": "c-b224dc5072f3460fbb10fc2912******"
    }
  5. 新建一个DescribeInvocationsSample.java类,查询命令运行是否成功。
    import com.aliyuncs.DefaultAcsClient;
    import com.aliyuncs.IAcsClient;
    import com.aliyuncs.ecs.model.v20140526.DescribeInvocationsRequest;
    import com.aliyuncs.ecs.model.v20140526.DescribeInvocationsResponse;
    import com.aliyuncs.exceptions.ClientException;
    import com.aliyuncs.exceptions.ServerException;
    import com.aliyuncs.profile.DefaultProfile;
    import com.google.gson.Gson;
    
    public class DescribeInvocationsSample {
        public static void main(String[] args) {
            DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "<accessKeyId>", "<accessSecret>");
            IAcsClient client = new DefaultAcsClient(profile);
    
            DescribeInvocationsRequest request = new DescribeInvocationsRequest();
            // request.setRegionId("cn-hangzhou");
            // 填写云助手命令的执行ID。
            request.setInvokeId("t-hz0b22o6******");
    
            try {
                DescribeInvocationsResponse response = client.getAcsResponse(request);
                System.out.println(new Gson().toJson(response));
            } catch (ServerException e) {
                e.printStackTrace();
            } catch (ClientException e) {
                System.out.println("ErrCode:" + e.getErrCode());
                System.out.println("ErrMsg:" + e.getErrMsg());
                System.out.println("RequestId:" + e.getRequestId());
            }
        }
    }
    返回结果如下所示,通过InvokeInstances,您可以查看命令的运行状态和结果。
    {
        "RequestId": "9A281A3E-0CE1-4D26-83F2-20F52EAC567D",
        "TotalCount": 56,
        "PageNumber": 1,
        "PageSize": 1,
        "Invocations": [{
            "InvokeId": "t-hz0b22o6******",
            "CreationTime": "2020-02-17T11:28:01Z",
            "CommandId": "c-b224dc5072f3460fbb10fc2912******",
            "CommandType": "RunShellScript",
            "CommandName": "upgradePython",
            "CommandContent": "******",
            "Frequency": "",
            "Timed": false,
            "InvokeStatus": "Finished",
            "Parameters": "{}",
            "InvokeInstances": [{
                "InstanceId": "i-bp155lkyo1ut0d******",
                "Repeats": 1,
                "InstanceInvokeStatus": "Finished",
                "InvocationStatus": "Success",
                "Output": "",
                "ExitCode": 0,
                "Dropped": 0,
                "ErrorCode": "",
                "ErrorInfo": "",
                "CreationTime": "2020-02-17T11:28:01Z",
                "StartTime": "2020-02-17T11:28:02Z",
                "StopTime": "",
                "FinishTime": "2020-02-17T11:28:02Z",
                "UpdateTime": "2020-02-17T11:28:02Z"
            }]
        }]
    }