LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Networking (https://www.linuxquestions.org/questions/linux-networking-3/)
-   -   IP Variable name? (https://www.linuxquestions.org/questions/linux-networking-3/ip-variable-name-885/)

yank_ 02-15-2001 04:41 PM

Is there some way I can use my ip address in a bash program. My ip address changes, as I am conencted to a DHCP cable modem. I wish to write a script that uses the ip address in it, but i can not just put xxx.xxx.xxx.xxx because the x's change when I restart the computer. I also need to know how to designate wich network card im using because the comp has two net cards.

is there a command like +eth0ip or +eth1ip that i can substitute for [ip of eth0] or [ip of eth1]

Please let me know.. im new to this linux stuff.

devnull 02-16-2001 12:45 PM

Hi,

first do a "ifconfig -a". Then you will see, what cards are active (look for UP....RUNNING...).

In a shell-script you can do the following:

eth0=`ifconfig eth0 | grep 'inet addr'| awk '{print $2}'|sed -e "s/addr\://"`

Watch out for the ticks and back-ticks!!

Hope it will help you.

yank_ 02-16-2001 03:20 PM

either this didnt work, or im not sure exactly what it is suppose to do

Please tell me the script to do the following

echo ipaddress (where ipaddress is the not known ip)

in a half-life server, i have to tell it what the ip is

./hlds_run +ip xxx.xxx.xxx.xxx (where xxx... is the ip addresss)

what would put for xxx.xxx.xxx.xxx if i dont know what the ip is because its different every time the script is run.

yank_ 02-16-2001 03:32 PM

ok, i got that to work..

now how do you allow the use of ifconfig over telnet?
it says command not found
but when i run it directly from that computer.. it works fine.

Thanks for the help

devnull 02-16-2001 04:02 PM

i have just tested it (haven't done it while writing it) and it works fine.
type it in a shell and then do an "echo eth0".
It will show you the ip-address of the interface eth0!

step by step:
with "eth0=...." you define a variable (your current ip-address). You can call it also ipaddress or something else.
"ifconfig eth0" will get all infos of the interface eth0 (or eth1 eth2 ...). These infos are piped " | " to "grep". grep cuts out the "inet addr"-part from the ifconfig-command.
This part is then handed over to "awk" who is responsible for sorting out the second part (..print $2) of the the grep results. this will result in something like "...addr:192.168.xxx.xxx:".
You can't work with this, so this part is piped to "sed".
sed now is doing some filtering and the result is the pure ip-address of the interface you want to know!

i don't know that half-life game, but if you start it with "hlds_run", you can now just add the name of the vairable (eth0 or ipaddress or whatever you've called it).

Have you done shell-scripting before or do you want to start it by hand?

btw: i think you can't copy and paste the command-line i have written because the shell might interpret the pipe-sign ( | ) al the letter "l". And that will never work ;-)

jeremy 02-16-2001 06:40 PM

/sbin is probably not in your path. Either type /sbin/ifconfig or add /sbin to your path.


All times are GMT -5. The time now is 01:14 AM.