LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Environment variables (https://www.linuxquestions.org/questions/linux-newbie-8/environment-variables-34288/)

kdnt 10-31-2002 06:30 PM

Environment variables
 
is there a $ variable that I can call and return the local IP address? I printenv and saw only one for REMOTEHOST

acid_kewpie 10-31-2002 06:42 PM

well there can be any number of local ip addresses... use ifconfig to pull out the one you want.

born4linux 10-31-2002 07:04 PM

try this:

my_ip=$(/sbin/ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1)
echo $my_ip

kdnt 10-31-2002 07:33 PM

Thanks but what I want to do is to add a little IP information
in the /etc/issue file in my situation which several servers
share a single monitor through a switcher, and all machines
have the same text mode login screen, confuses users
occasionally.

kdnt 10-31-2002 07:35 PM

Wow..... thanks. Lemme try....

kdnt 10-31-2002 08:02 PM

Yes it works. Thanks born4linux!

But, sorry for my ignorance.... where does the printenv output
sits? Want to hardcode the definition of my_ip into the system.
Thanks again.

kdnt 10-31-2002 08:54 PM

and... upon adding that $my_ip to /etc/issue, what thing do i have to restart so the change takes effect without rebooting? Thanks big time once again in advance.

jdc2048 10-31-2002 09:17 PM

Quote:

Originally posted by kdnt
Yes it works. Thanks born4linux!

But, sorry for my ignorance.... where does the printenv output
sits? Want to hardcode the definition of my_ip into the system.
Thanks again.

The question is a little vague. If you are asking how to make the system IP an environment variable, then you could take the code that born4linux gave you and add it to your /etc/profile. Make sure to export the variable as well. Best practice is to make any env vars in all caps (i.e. MY_IP instead of my_ip).

Jeremiah

kdnt 10-31-2002 10:53 PM

yes thanks jeremiah. "/etc/profile" is what I want. And how to make the changes effective without rebooting? I did all you
experts said, but upon loading up another terminal and establishing connection (I'm doing all in secureCRT and have root permission) the change didn't show up. And what does "export the variable" mean? Thanks again.

jdc2048 11-01-2002 01:31 AM

Code:

MY_IP=$(/sbin/ifconfig eth0 | grep "inet addr" | cut -d : -f 2 | cut -d " " -f 1)
export $MY_IP

Shown above is how you can export the variable. This is done to make it available to all logged in users.

To make the IP show up on the login screen, you would want to add the IP into the file /etc/issue. There is no need to reboot or restart anything. All future connections will read this file when they make the connection.

You won't be able to use the $MY_IP inside the /etc/issue file though, because the /etc/profile is not read until after login. Whereas the /etc/issue file is read before login.

Jeremiah

kdnt 11-01-2002 02:49 AM

Thanks but don't fully understand. I guess you know what I'm trying to accomplish. What's your advice? It can be or can not be done?

unSpawn 11-01-2002 08:19 AM

Another way if your IP address is static, and you've got network access at boot and you only need it for/etc/issue you process it this way as well. Add this line to for instance to rc.local, (w/o outer quotes) after any other lines that change issue: "host $(hostname) | awk '{print "Our IP address is:", $4}' >> /etc/issue"
If your IP address changes often then you better to add the line to the script that runs after you re-establish your connection.

You could also try "host $(hostname) | awk '{print "MY_IP=",$4}' > /etc/.myip" and pick it up in profile using if [ -f "/etc/.myip" ]; then . /etc/.myip; else MY_IP="unknown"; fi


All times are GMT -5. The time now is 05:37 PM.