LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices


Reply
  Search this Thread
Old 07-07-2004, 10:05 AM   #1
yenonn
Member
 
Registered: Feb 2003
Location: Malaysia
Distribution: Redhat 8.0, 9, Slackware 9.1
Posts: 511

Rep: Reputation: 30
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
 
Old 07-07-2004, 12:55 PM   #2
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Sure
Code:
curl -s http://www.whatismyip.com/ | grep '<TITLE>' | sed -e 's/.*TITLE>.*is //' -e 's/ W.*//'
 
Old 07-07-2004, 12:57 PM   #3
wolfe2554
Member
 
Registered: Apr 2003
Location: denver co
Distribution: redhat9
Posts: 156

Rep: Reputation: 30
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.
 
Old 07-07-2004, 01:01 PM   #4
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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

Last edited by keefaz; 07-07-2004 at 01:02 PM.
 
Old 07-07-2004, 03:35 PM   #5
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,986

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
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.

Last edited by kilgoretrout; 07-07-2004 at 06:02 PM.
 
Old 07-07-2004, 08:44 PM   #6
yenonn
Member
 
Registered: Feb 2003
Location: Malaysia
Distribution: Redhat 8.0, 9, Slackware 9.1
Posts: 511

Original Poster
Rep: Reputation: 30
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?
 
Old 07-07-2004, 08:46 PM   #7
yenonn
Member
 
Registered: Feb 2003
Location: Malaysia
Distribution: Redhat 8.0, 9, Slackware 9.1
Posts: 511

Original Poster
Rep: Reputation: 30
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
 
Old 07-07-2004, 09:14 PM   #8
wolfe2554
Member
 
Registered: Apr 2003
Location: denver co
Distribution: redhat9
Posts: 156

Rep: Reputation: 30
yes but export the variable to get it to go on to a new script with
export ip
 
Old 07-07-2004, 09:29 PM   #9
yenonn
Member
 
Registered: Feb 2003
Location: Malaysia
Distribution: Redhat 8.0, 9, Slackware 9.1
Posts: 511

Original Poster
Rep: Reputation: 30
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
 
Old 07-08-2004, 02:02 AM   #10
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,986

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
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".
 
Old 07-09-2004, 01:35 AM   #11
yenonn
Member
 
Registered: Feb 2003
Location: Malaysia
Distribution: Redhat 8.0, 9, Slackware 9.1
Posts: 511

Original Poster
Rep: Reputation: 30
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......
 
Old 07-09-2004, 08:48 AM   #12
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
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
 
Old 07-09-2004, 11:29 AM   #13
kilgoretrout
Senior Member
 
Registered: Oct 2003
Posts: 2,986

Rep: Reputation: 388Reputation: 388Reputation: 388Reputation: 388
Good catch keefaz. Didn't notice that.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script and Dynamic variable xanthium Programming 11 07-12-2011 06:05 AM
not able to pass dynamic path to fopen command in C gauravsahni Programming 3 11-03-2004 01:28 PM
Shell command (Try this out!!!!!!!) vbhatia_81 Programming 6 06-23-2004 02:43 AM
what is the shell command to.... praefex Linux - Newbie 2 08-18-2003 05:25 PM
shell command i_is_cat Linux - General 1 08-13-2003 03:09 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Networking

All times are GMT -5. The time now is 07:27 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration