aliyun阿里云Instant计算服务IP地址查询脚本
2026/9/15 3:03:05 网站建设 项目流程

阿里云Instant计算服务IP地址查询脚本

简介

阿里云Instant计算服务,获取正在运行的作业实例的IP地址。快速批量查询所有运行中作业的IP地址。

脚本代码

#!/bin/bash# 阿里云Instant计算服务作业IP地址查询脚本REGION="cn-hangzhou"# 查询所有作业的IP地址jobs=$(aliyun ehpcinstant ListJobs --region"$REGION"2>/dev/null)if[-z"$jobs"];thenecho"查询失败或没有作业"exit1fijob_ids=$(echo"$jobs"|jq -r'.JobList[].JobId')forjob_idin$job_ids;doif[-z"$job_id"];thencontinuefiexecutors=$(aliyun ehpcinstant ListJobExecutors\--region"$REGION"\--JobId"$job_id"\--TaskName Task0\--PageSize100\2>/dev/null)echo"$executors"|jq -r'.Executors[] | .IpAddress[0]'2>/dev/nulldone|sort-u>running_ips.txtecho"IP地址已保存到 running_ips.txt"wc-l running_ips.txt

功能说明

1. 设置区域

REGION="cn-hangzhou"

脚本默认使用杭州区域,如需修改为其他区域,请更改此变量。

2. 查询所有作业

jobs=$(aliyun ehpcinstant ListJobs --region"$REGION"2>/dev/null)

调用阿里云CLI获取当前区域下的所有作业列表。

3. 提取作业ID

job_ids=$(echo"$jobs"|jq -r'.JobList[].JobId')

使用jq工具从返回的JSON数据中提取所有作业的ID。

4. 遍历查询每个作业的执行器IP

forjob_idin$job_ids;doexecutors=$(aliyun ehpcinstant ListJobExecutors\--region"$REGION"\--JobId"$job_id"\--TaskName Task0\--PageSize100\2>/dev/null)echo"$executors"|jq -r'.Executors[] | .IpAddress[0]'2>/dev/nulldone

对每个作业调用ListJobExecutors接口获取执行器信息,并提取IP地址。

5. 去重并保存

done|sort-u>running_ips.txt

将所有IP地址排序并去重,最终保存到running_ips.txt文件中。

使用方法

前置条件

  1. 安装阿里云CLI

    # Linux/Maccurl-s https://aliyuncli.alicdn.com/aliyun-cli.sh|bash
  2. 配置凭证

    aliyun configure
  3. 安装jq工具

    # Ubuntu/Debiansudoapt-getinstalljq# CentOS/RHELsudoyuminstalljq# macOSbrewinstalljq

运行脚本

# 添加执行权限chmod+x get_running_ips.sh# 运行脚本./get_running_ips.sh

输出示例

IP地址已保存到 running_ips.txt 25 running_ips.txt

查看IP地址文件:

catrunning_ips.txt

注意事项

  1. 区域配置:默认使用cn-hangzhou区域,如需查询其他区域,请修改REGION变量
  2. 任务名称:脚本默认查询Task0任务
  3. 分页大小:当前设置PageSize为100
  4. 错误处理:脚本会将错误输出重定向到/dev/null,确保静默运行

总结

阿里云CLI和jq工具,实现了批量获取Instant计算服务作业IP地址的功能。

参考

  1. 阿里云CLI凭证管理指南

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询