what is the shell command for getting dynamic ip??
hi,
i have a dynamic ip for accessing my internet. i want to get the ip in my shell script. what is the proper command to get a ip? thanks |
Sure ;)
Code:
curl -s http://www.whatismyip.com/ | grep '<TITLE>' | sed -e 's/.*TITLE>.*is //' -e 's/ W.*//' |
do you mean you wish to retrive the ip automatically? if so try ifconfig|grep "P-t-P" and then narrow it down with a regular expression.
|
ifconfig will not help as it can't view the public ip from the internal network, only the private.
[edit] of course forget that if the computer IS the gateway |
This seems to work for me:
# ifconfig eth0 | grep "inet addr:" | cut -d ":" -f 2 | cut -d " " -f1 192.168.1.100 Basically, it uses the output of "ifconfig eth0" and pipes it through grep and cut to get to the field in the output that displays the ip. Here's my output for ifconfig eth0: # ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:50:BA:CC:A7:A2 inet addr:192.168.1.100 Bcast:192.168.1.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:205433 errors:0 dropped:0 overruns:0 frame:0 TX packets:186536 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:226189854 (215.7 Mb) TX bytes:27294230 (26.0 Mb) Interrupt:21 Base address:0x6c00 If your format is similar, the above command should work. But if your on a lan, you'll only get your local ip in the network as I did above. In that case, you can get your ip with: $ curl -s http://www.whatismyip.com/ | grep '<TITLE>' | cut -d " " -f 4 which outputs the text between the third and fourth spaces which is your ip. |
talking about writting shell script, i am pretty new.
how can i assigning the command above to a variable such that it can used as a string to the subsequent script? |
such as
#!/bin/sh ip=ifconfig ........of command line # i want to echo it for screen echo $ip am i correct???? pls advise... i am pretty urge... thanks |
yes but export the variable to get it to go on to a new script with
export ip |
but, when i echo my variable... it is nothing...
i write it in a proper manner #!/bin/sh ip=ifconfig | grep "inet" | cut -c 0-36 | sed -e 's/[a-zA-Z:]//g" echo $ip but, i get nothing on the screen when i execute it... however, if i type "ifcofig,....." in the command prompt, it will give me the ip. how to assign the command into a variable... thanks help......really need help |
Try this:
#!/bin/sh ip=$(ifconfig | grep "inet" | cut -c 0-36 | sed -e 's/[a-zA-Z:]//g") echo $ip In general $(command) gives the output of command which is what you want to assign to your variable. Might want to change your variable name since "ip" is also a command in most linux distros used for routing. Try something like "getip". |
it should be
ip=`ifconfig | grep "inet" | cut -c 0-36 | sed -e 's/[a-zA-Z:]//g"` now i know.. to execute a shell command and then, assign it to a variable then, we should put ` but, not ' or " thanks guys...... |
the $(command) works only in bash not sh, so if your /bin/sh is not a symlink to /bin/bash you have to add at the top :
#!/bin/bash To $(command) syntax works |
Good catch keefaz. Didn't notice that.
|
All times are GMT -5. The time now is 04:38 PM. |