Get ip address and set as variable bash shell script
'The' IP address... servers have so many, default management, application, DB listener?
Does this do it for you?
IP_ADDR=$(getent hosts `hostname` | awk '{print $1}')
Related videos on Youtube
John Stapleton
Updated on September 18, 2022Comments
-
John Stapleton almost 2 years
I am trying to write a bash script to configure an app (unattended install). All I need to do is this. I need to get the IP address for the server and set it as a variable. I am writing this script to simplify that. What I have noticed is that when I test this in a vps though eth0 is listed its not the default, its something along the lines of venet0:0. Is there any way of getting this ip set as a vaiable (I know how to if I know the interface name, i am trying to do this without knowing as its hard to plan for every possible device naming). if not then i will just add a prompt for the ip address for the user right as they run the script.
This is not a duplicate question as I am asking for the server's IP not just the public IP of the server. Even though this is aimed at building a public mail server I am building it to work on an internal one as well.
-
John Stapleton about 11 yearsThis worked Perfectly for me! thanks! I am a better PHP programmer than a shell scripter i swear! :)
-
Danila Vershinin almost 10 yearsThis approach is quite nice on a server that was properly setup. However, it won't work until system administrator adds external IP address in /etc/hosts
-
Danila Vershinin almost 10 yearsHowever, note that on IPV6 enabled system, it returns IP V6.