82

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?

deepak
  • 3,074
  • 2
  • 20
  • 29

8 Answers8

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
Ilmo Euro
  • 4,925
  • 1
  • 27
  • 29
deepak
  • 3,074
  • 2
  • 20
  • 29
  • 53
    To get the actual IP address (as requested originally) I used the Script Console, but executed `println InetAddress.localHost.hostAddress` instead. – Myles Steinhauser Aug 06 '13 at 15:46
  • Neither of these seem to work for Linux... or at least not my Ubuntu VM node. – kayleeFrye_onDeck Jun 15 '16 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 '16 at 05: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 '18 at 22:25
  • 2
    This will always print the name / ip of the master, not the slave node – Mzzl Dec 18 '19 at 13:29
34

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.

Alex O
  • 7,746
  • 2
  • 25
  • 38
  • 2
    As far as I can see, this is the only solution that runs on the master. Right? – hagello Feb 06 '17 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 06 '17 at 13:32
  • This requires whitelisting for security, so not useable on locked down jenkins :( – ablarg Feb 13 '19 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](https://jenkins.io/doc/book/managing/script-approval/). – Alex O Feb 14 '19 at 06:02
22

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
Adam Kalnas
  • 1,188
  • 2
  • 11
  • 25
  • 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 '16 at 20:47
  • This requires whitelisting for security, so not useable on locked down jenkins :( – ablarg Feb 13 '19 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 '23 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

Sridhar Sarnobat
  • 25,183
  • 12
  • 93
  • 106
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

grepit
  • 21,260
  • 6
  • 105
  • 81
1

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

subbu
  • 11
  • 3
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
benathon
  • 7,455
  • 2
  • 41
  • 70
-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

SCoyle
  • 97
  • 8
  • 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. – Sridhar Sarnobat Nov 19 '18 at 19:06
  • This also won't show the node name or ip address of the master. – Brian Minton May 17 '19 at 02: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 '19 at 12:40