阿里雲 Cloud Shell 初體驗
今日登陸阿里雲,發現 阿里雲 推出了 Cloud Shell,本以為是 一個 模擬 終端的一個小工具,體(zhe)驗(teng)一番,發現 竟然 是一個 真實的 被限制的 Linux環境,基於 Alpine Linux。好興奮,有免費的測試機玩了。。。。
常用命令都被軟關聯到了 busybox。測試了下 gcc、apt-get、yum、rpm統統沒有,這樣就有點雞肋了,除了自帶的工具啥都不能安裝。好在預設安裝了 Python、SSH、curl等工具,pip因為許可權太低不能安裝庫。

看了下 shell 中的核心命令 aliyun,功能還是很強大的,基本所有 業務都可以操作。
shell@Alicloud:~$ aliyun --help Alibaba Cloud Command Line Interface Version 3.0.6 Usage: aliyun <product> <operation> [--parameter1 value1 --parameter2 value2 ...] Flags: --modeuse `--mode {AK|StsToken|RamRoleArn|EcsRamRole|RsaKeyPair}` to assign authenticate mode --profile,-puse `--profile <profileName>` to select profile --languageuse `--language [en|zh]` to assign language --regionuse `--region <regionId>` to assign region --access-key-iduse `--access-key-id <AccessKeyId>` to assign AccessKeyId, required in AK/StsToken/RamRoleArn mode --access-key-secret use `--access-key-secret <AccessKeySecret>` to assign AccessKeySecret --sts-tokenuse `--sts-token <StsToken>` to assign StsToken --ram-role-nameuse `--ram-role-name <RamRoleName>` to assign RamRoleName --ram-role-arnuse `--ram-role-arn <RamRoleArn>` to assign RamRoleArn --role-session-name use `--role-session-name <RoleSessionName>` to assign RoleSessionName --private-keyuse `--private-key <PrivateKey>` to assign RSA PrivateKey --key-pair-nameuse `--key-pair-name <KeyPairName>` to assign KeyPairName --secureuse `--secure` to force https --forceuse `--force` to skip api and parameters check --endpointuse `--endpoint <endpoint>` to assign endpoint --versionuse `--version <YYYY-MM-DD>` to assign product api version --headeruse `--header X-foo=bar` to add custom HTTP header, repeatable --bodyuse `--body $(cat foo.json)` to assign http body in RESTful call --pageruse `--pager` to merge pages for pageable APIs --output,-ouse `--output cols=Field1,Field2 [rows=jmesPath]` to print output as table --waiteruse `--waiter expr=<jmesPath> to=<value>` to pull api until result equal to expected value --dryrunadd `--dryrun` to validate and print request without running. --quiet,-qadd `--quiet` to hide normal output --helpprint help Sample: aliyun ecs DescribeRegions Products: aegisServer Guard agencyagency alidnsAlibaba Cloud DNS armsApplication Real-Time Monitoring Service batchcomputeBatchCompute bssopenapibss ccccloud call center cdnAlibaba Cloud CDN cdsCodePipeline chatbotBeeBot cloudapiAPI Gateway cloudauthID Verification cloudphotoCloud Photos cloudwfCloudAP cmsCloud Monitor crContainer Registry csContainer Service csbCloud Service Bus ddsApsaraDB for MongoDB dmDirect Mail domaindomain domain-intldomain drdsDistributed Relational Database Service eciElastic Container Instances ecsElastic Compute Service edasEnterprise Distributed Application Service elasticsearch elasticsearch emrE-MapReduce essAuto Scaling greenAlibaba Content Security Service hpcAlibaba Cloud HPC httpdnsHttpDNS iotAlibaba Cloud IoT itaasCloud Display jaqMobile Security kmsKey Management Service liveApsaraVideo for Live mtsApsaraVideo for Media Processing nasNetwork Attached Storage onsMessage Queue otsTable Store polardbApsaraDB for POLARDB pushAlibaba Cloud Mobile Push qualitycheckSmart Conversation Analysis r-kvstoreApsaraDB for Redis ramResource Access Management rdsApsaraDB for RDS rosResource Orchestration Service sas-apiSituation Awareness Service slbServer Load Balancer stsResource Access Management vodApsaraVideo for VOD vpcVirtual Private Cloud wafWeb Application Firewall Use `aliyun --help` for more information.
之前 做過 域名API 相關的開發,拿域名入手,aliyun domain –help 可以看到 域名支援的操作,點選 連結可以進入 域名的API 文件。經過測試,API中的所有操作都可以 支援(很多在 命令幫助中 看不到),比如:QueryDomainList。
shell@Alicloud:~$ aliyun domain --help Alibaba Cloud Command Line Interface Version 3.0.6 Usage: aliyun Domain <ApiName> --parameter1 value1 --parameter2 value2 ... Product: Domain (domain) Version: 2016-05-11 Link: https://help.aliyun.com/api/domain Available Api List: CheckDomain CreateOrder DeleteContactTemplate GetWhoisInfo OSuborderDomain QueryBatchTaskDetailList QueryBatchTaskList QueryContact QueryContactTemplate QueryDomainBySaleId QueryDomainList QueryFailReasonList QueryOrder SaveContactTemplate SaveContactTemplateCredential SaveTaskForModifyingDomainDns SaveTaskForSubmittingDomainNameCredential SaveTaskForSubmittingDomainNameCredentialByTemplateId SaveTaskForUpdatingContactByTempateId SaveTaskForUpdatingContactByTemplateId WhoisProtection
阿里的域名匯出不太好用,用 QueryDomainList 介面,做個 域名匯出工具,練練手。根據文件,必須的引數 有 PageNum、PageSize。 執行 aliyun domain QueryDomainList –PageNum 1 –PageSize 10000,就獲取到了 每頁10000條,第一個 的域名資料。將 輸出的 JSON 資訊 上傳到 伺服器處理(比如PHP),生成 csv 下載。
經過測試 PageSize 最大值為 1000,我 公司賬號裡有上萬條域名資料,需要分多頁下載。考慮到 HTTP 請求的限制,每次只上傳 100 個域名資訊。經過一下午的摸索,終於 完美的實現了這個小功能。經過優化,已經比較完美了。按照 Linux 下的慣例, 只需要複製一行 命令即可使用。
http://www.miaoqiyuan.cn/products/alishell/domain.php
最終生成的檔案如下:

完整的PHP原始碼如下:
<?php /** * 檔案:domain.php * 作者:mqycn * 部落格:http://www.miaoqiyuan.cn * 原始碼:http://www.miaoqiyuan.cn/p/aliyun-cloud-shell * 說明:阿里雲 Cloud Shell 一鍵 匯出所有域名 */ define('APP_PATH', 'http://www.miaoqiyuan.cn/products/alishell/'); //安裝目錄 define('APP_URL', APP_PATH . 'domain.php'); //程式路徑 define('APP_ACTION', isset($_GET['action']) ? $_GET['action'] : ''); //請求方法 //讀取 YunShell 上傳的資訊 $data = json_decode(file_get_contents("php://input"), true); if (!empty($data) && isset($data['Data']) && isset($data['Data']['Domain'])) { switch (APP_ACTION) { case 'tocsv': //分頁上傳域名資料 if (!isset($_GET['key']) && !is_numeric($_GET['key'])) { die("Key Error!"); } $tmp_key = $_GET['key']; $csv_data = ''; foreach ($data['Data']['Domain'] as $item) { foreach ($item as $val) { $csv_data .= $val . "\t"; } $csv_data .= "\n"; } $file_name = $tmp_key . '.txt'; file_put_contents($file_name, $csv_data, FILE_APPEND); echo "Update: " . APP_PATH . "${file_name}"; break; default: //根據 總頁數 生成 指令碼 if (isset($data['TotalPageNum']) && is_numeric($data['TotalPageNum'])) { $page_all = (int) $data['TotalPageNum']; echo "echo \n\nAll:{$page_all}\n"; $tmp_key = date('Ymdhis'); for ($page_id = 1; $page_id <= $page_all; $page_id++) { echo "\necho Download {$page_id}/${page_all}\n"; echo 'curl "' . APP_URL . '?action=tocsv&key=' . $tmp_key . '" --data "`' . "aliyun domain QueryDomainList --PageNum ${page_id} --PageSize 100" . '`"'; } } break; } //結尾輸出換行,防止影響提示符 die("\n"); } else { //直接訪問,輸出呼叫命令 echo 'curl ' . APP_URL . ' --data "`aliyun domain QueryDomainList --PageNum 1 --PageSize 100`"|bash'; } ?>