如何从master找到一个jenkins节点的ip地址

How to find the ip address of a jenkins node from the master

运行 从主节点脚本控制台,或从系统 groovy 脚本(也在主节点上运行),如何获取从节点的 IP 地址节点?

我希望这个简单的脚本就足够了:

import java.net.*

for (slave in Jenkins.instance.slaves) {
  host = slave.computer.hostName
  addr = InetAddress.getAllByName(host)
  println slave.name + ": " + addr.hostAddress
}

但至少在我的安装中,它没有给我在具有多个网络接口的系统上想要的结果。

您可以在每个从属设备上使用从 the answer to "How to execute system command on remote node" 到 运行 的 "run a command on the slave" 技术,例如 /sbin/ifconfig。那肯定会给你详细信息,但我没有 Groovy 编写输出解析器来提取 IP 的智慧。

@Dave Bacher 的回答将为您提供给定主机的所有 IP。 假设您的奴隶是基于 SSH 的,这将严格地为您提供代理使用的 IP:

def jenkins = jenkins.model.Jenkins.instance
for (node in jenkins.nodes) {
  println "${node.nodeName}: node.toComputer().launcher.host"
}