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

后端 未结 2 444
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-15 09:11

Running from the master node scripting console, or from the a system groovy script (that also runs on the master), how do I get the ip address(es) of a slave node?

相关标签:
2条回答
  • 2021-01-15 09:30

    I was hoping that this simple script would suffice:

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

    But at least with my installation, it does not give me the result I want on the systems that have multiple network interfaces.

    You could use the "run a command on the slave" technique from the answer to "How to execute system command on remote node" to run something like /sbin/ifconfig on each slave. That would certainly give you the details, but I don't have the Groovy savvy to write up an output parser to extract the IPs.

    0 讨论(0)
  • 2021-01-15 09:36

    @Dave Bacher's answer will give you all IP's for the given host. Assuming your slaves are SSH based, this will give you strictly the IP that the agent uses:

    def jenkins = jenkins.model.Jenkins.instance
    for (node in jenkins.nodes) {
      println "${node.nodeName}: node.toComputer().launcher.host"
    }
    
    0 讨论(0)
提交回复
热议问题