Get Local IP address using Sed, Awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get Local IP address using Sed, Awk
# 1  
Old 05-31-2011
Get Local IP address using Sed, Awk

Hi All,

how to get solaris box local ip addresss in variable, using sed or awk utlities.

Thanks,
Mani Muthu
# 2  
Old 05-31-2011
what is the output of the following command
Code:
/sbin/ifconfig -a

# 3  
Old 05-31-2011
Hi i am getting the following ouput
Code:
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
e1000g0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 10.100.19.98 netmask fffffe00 broadcast 10.110.9.255

i want to hold the '10.100.19.98' in a variable
# 4  
Old 05-31-2011
try this...
Code:
ip_addresss=`/sbin/ifconfig -a | awk ' /inet.*broadcast/ "{print $2}"'`

This User Gave Thanks to itkamaraj For This Post:
# 5  
Old 05-31-2011
Code:
user@host> (/home/user) $ /sbin/ifconfig -a
lo0: flags=2001000849<UP,LOOPBACK,RUNNING,MULTICAST,IPv4,VIRTUAL> mtu 8232 index 1
        inet 127.0.0.1 netmask ff000000
hme0: flags=1000843<UP,BROADCAST,RUNNING,MULTICAST,IPv4> mtu 1500 index 2
        inet 158.155.34.123 netmask ffff0000 broadcast 158.155.255.255
user@host> (/home/user) $ /sbin/ifconfig -a|awk '{if($1=="hme0:"){interface=1} if(interface==1 && $1=="inet"){print $2}}'
158.155.34.123
user@host> (/home/user) $

Here is the output of Solaris ifconfig and logic to get the IP of required interface, in my case hme0 is the interface
# 6  
Old 05-31-2011
thanks @ itkamaraj
I am getting the output as few modification based on your code.
Code:
ifconfig -a | awk ' /(inet)(.*)broadcast/ {print $2}'

thanks @ kumaran_5555. But i don't under stand your logic and i am not able to get the output as per you mention method
# 7  
Old 05-31-2011
Code:
 /sbin/ifconfig -a|awk '{if($1=="hme0:"){interface=1} if(interface==1 && $1=="inet"){print $2}}'

you have to change hme0 to e1000g0. The logic you have used may not work when you have more interface with broadcast enabled.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. IP Networking

Can't get local IP address in getaddrinfo

Hi all, I am working with SIP protocol and am using SIPp to generate SIP traffic. But the call is not going through since I get the error; 2015-02-24 14:09:39:330 1424804979.330517: Can't get local IP address in getaddrinfo, local_host='NODE-01', local_ip=''. My ifconfig output is; ... (2 Replies)
Discussion started by: Junaid Subhani
2 Replies

2. IP Networking

netstat local and foreign address relationship.

Hi All, Can you please help me in understanding the relationship between local and foreign address in the output of netstat -an. Output 1 ---------- 162.103.162.37.50224 162.103.162.35.9511 49640 0 49640 0 ESTABLISHED 162.103.162.37.50263 162.103.162.35.9512 49640 0... (1 Reply)
Discussion started by: Girish19
1 Replies

3. Shell Programming and Scripting

IP Address Modification through awk/sed

Hi, I have to modify the 2nd and 3rd octet of the IP address through awk/sed. For Example: Given IP is : 10.205.22.254, it should be modified as 10.105.100.254 through awk/sed. Kindly help me on this and let me know if you have any questions. Thanks in advances. (2 Replies)
Discussion started by: kumarbka
2 Replies

4. Debian

How to change local IP address?

I have a new Ethernet device that has a default IP address on a different subnet and need to change it. I have a Debian 6.0 host connected to the device with a crossover cable and have changed the host /etc/hosts and /etc/networks files to the same subnet as the new device and rebooted. Ping... (2 Replies)
Discussion started by: snorkack59
2 Replies

5. Shell Programming and Scripting

HELP with awk or sed. Need to replace all IP address by 2

Hi, In a file, I have several time <IP>232.0.1.164</IP> ... <IP>232.0.1.135</IP> I need to replace all the random IP addresses , by 239.0.0.1 and 239.0.0.2 , alternatively. I try this grep "<IP>" tsp.xml | awk '{if(NR % 2)print $0}' | cut -d"<" -f2 | cut -d">" -f2 ... (3 Replies)
Discussion started by: FredMo
3 Replies

6. IP Networking

How to know local IP address in X-Terminal?

Im using a X-Terminal in my windows pc to connect to a Linux server. Is there a way to know my local IP address in my x-terminal console? Here are few commands which didnt help me: ss_cc@MGTS5026-13sh1:~> finger Login Name Tty Idle Login Time Where loadhlr ... (6 Replies)
Discussion started by: Arun_Linux
6 Replies

7. BSD

Link Local IPv6 Address

Hi, Am using FreeBSD7.4/i386 During IPv6 configuration, I added the following in rc.conf as Restarted IPv6 network using /etc/rc.d/network_ipv6 restart.. My problem is I need to set link local IPv6 address auto-configured.. Is my proceeding right?? I feel something missing to make... (0 Replies)
Discussion started by: Priya Amaresh
0 Replies

8. Shell Programming and Scripting

Local system IP address in UNIX

We have requirement to get the local system IP address of whoever logined the DataBase(sqlplus) or any process in application server. Actually I connected to application server thru putty and then conencted to Database using sqlplus command. we have tried below commands : who -u|grep... (4 Replies)
Discussion started by: siri_886
4 Replies

9. Shell Programming and Scripting

Easy way to get local IP address

I needed to get a machine's local IP address when not root (so no ifconfig). Eventually, I arrived at this convoluted solution that grabs the unique local IP info from netstat... netstat -n -t | awk '{print $4}' | grep -o "*\.*\.*\.*" |\ grep -v "127.0.0.1" | sort -u ...however I... (5 Replies)
Discussion started by: simonb
5 Replies

10. UNIX for Dummies Questions & Answers

How do I get the IP address of my local unix box???

Hiya all, How do I get the IP address of my local unix box??? IPconfig - don't exist nslookup - only for other boxes.... I have my Fedora Server now on the office network. Can ping others, can get onto internet via web proxy.... Also: How do I create a Drive Share, so our windows... (9 Replies)
Discussion started by: marty 600
9 Replies
Login or Register to Ask a Question