83

A windows slave node connected to Jenkins server through "Java web start". The system information of the node doesn't have it's IP address.

I had to run through all the slaves node we had, and find which machine (ip address) corresponds to the slave node in Jenkins.

Is there a way to find the IP address of a slave node from Jenkins itself?

8 Answers 8

86

Through the Script Console (Manage Jenkins -> Nodes -> Select a node -> Script Console) of the node we can execute groovy script. Run the following command to get the IP address.

println InetAddress.localHost.canonicalHostName
5
  • 54
    To get the actual IP address (as requested originally) I used the Script Console, but executed println InetAddress.localHost.hostAddress instead. Aug 6, 2013 at 15:46
  • Neither of these seem to work for Linux... or at least not my Ubuntu VM node. Jun 15, 2016 at 23:55
  • 2
    This works fine for a single node (at least when it's running Linux), but you cannot get an overview on all nodes with that, since it requires selecting each node's script console individually. There's a more generic solution in my answer, you may want to give that a try.
    – Alex O
    Sep 29, 2016 at 5:27
  • 1
    this one giving error Scripts not permitted to use staticMethod java.net.InetAddress getLocalHost. Administrators can decide whether to approve or reject this signature. [Pipeline] End of Pipeline org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use staticMethod java.net.InetAddress getLocalHost at org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.StaticWhitelist.rejectStaticMethod(StaticWhitelist.java:189)
    – sfgroups
    Jun 22, 2018 at 22:25
  • 2
    This will always print the name / ip of the master, not the slave node
    – Mzzl
    Dec 18, 2019 at 13:29
35

The most efficient and platform-independent way to find out the IP is probably the following groovy code for the "global" Script Console on the master:

import hudson.model.Computer.ListPossibleNames

def node = jenkins.model.Jenkins.instance.getNode( "myslave" )
println node.computer.getChannel().call(new ListPossibleNames())

In the console, this yields (for example)

Result
[192.168.0.17]

The result is a list of strings, as there's potentially multiple IP addresses on one machine.

Since this does not require the node-specific consoles, it's easy to add a loop around the code that covers all nodes.

4
  • 2
    As far as I can see, this is the only solution that runs on the master. Right?
    – hagello
    Feb 6, 2017 at 12:10
  • Yes, that's correct. (To be precise, the script runs on the master; it delegates listing of IPs to the slave node via remoting).
    – Alex O
    Feb 6, 2017 at 13:32
  • This requires whitelisting for security, so not useable on locked down jenkins :(
    – ablarg
    Feb 13, 2019 at 19:51
  • @ablarg: Yes, you need permission to run groovy scripts. At least for the global script console, this will only be possible when granted admin privileges. However, permissions to execute specific scripts might be granted to normal users via in-process script approval.
    – Alex O
    Feb 14, 2019 at 6:02
23

To answer this same question on a non-windows Jenkins slave:

Get the IP address:

println "ifconfig".execute().text

Get the hostname:

println "hostname".execute().text
3
  • 1
    Excellent addition to the post. It took me a while to look through ALL of the answers to a question on SO.
    – Shadoninja
    Jun 13, 2016 at 20:47
  • This requires whitelisting for security, so not useable on locked down jenkins :(
    – ablarg
    Feb 13, 2019 at 19:51
  • All the other answers are BS and return hostname or 127.0.1.1 etc. ifconfig is what I really want
    – Frak
    Jan 26 at 16:26
12

From the Web Interface

Go to the node's Log link:

http://jenkins.mycompany.com:8080/computer/my_node_name/log

The first line should say something like:

JNLP agent connected from /10.11.12.123

screenshot

screenshot

9

This is very similar to what deepak explained but I added images along the short steps.

In Jenkins UI click:

Manage Jenkins -> Nodes -> Select a node -> Script Console

then run println InetAddress.localHost.canonicalHostName

enter image description here

1

In your Jenkins job if its in groovy or else echo the ifonfig sh "/sbin/ifconfig -a | grep inet"

1

To get the ip on a Windows slave:

Navigate to the Script Console (Manage Jenkins -> Nodes -> Select a node -> Script Console)

println "ipconfig".execute().text
-1

Can also be found through the Jenkins UI: Manage Jenkins --> Manage Nodes --> Click Node name --> Configure

This should display both the public and private ip address of that node

3
  • 1
    I'm guessing people downvoted this because if you're using Java web start, it won't show it; only if you use SSH. I wish people would explain when they downvote. Nov 19, 2018 at 19:06
  • This also won't show the node name or ip address of the master. May 17, 2019 at 2:06
  • 1
    Original question doesn't ask for the IP address of the master, only the slaves "Is there away to find the IP address of a slave node from Jenkins itself?" - hence why this answer was provided
    – SCoyle
    May 20, 2019 at 12:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.