Getting IP in a script

Deepak Saxena deepak@csociety.purdue.edu
Tue, 13 Mar 2001 13:39:51 -0700


strace is your friend. :)  Below is the strace for an 'ifconfig eth0'. 
Looks like they open a couple of sockets and do some ioctls on them.  
SIOIFADDR is probably what you want.  Not sure what the return parameter 
is.  Probably a sock_addr or something of that sort.

~Deepak

execve("/sbin/ifconfig", ["/sbin/ifconfig", "eth0"], [/* 24 vars */]) = 0
brk(0)                                  = 0x8053c64
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40014000
open("/etc/ld.so.preload", O_RDONLY)    = -1 ENOENT (No such file or directory)
open("/etc/ld.so.cache", O_RDONLY)      = 3
fstat(3, {st_mode=S_IFREG|0644, st_size=26453, ...}) = 0
old_mmap(NULL, 26453, PROT_READ, MAP_PRIVATE, 3, 0) = 0x40015000
close(3)                                = 0
open("/lib/libc.so.6", O_RDONLY)        = 3
fstat(3, {st_mode=S_IFREG|0755, st_size=4101324, ...}) = 0
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\210\212"..., 4096) = 4096
old_mmap(NULL, 1001564, PROT_READ|PROT_EXEC, MAP_PRIVATE, 3, 0) = 0x4001c000
mprotect(0x40109000, 30812, PROT_NONE)  = 0
old_mmap(0x40109000, 16384, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED, 3, 0xec000) = 0x40109000
old_mmap(0x4010d000, 14428, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0x4010d000
close(3)                                = 0
mprotect(0x4001c000, 970752, PROT_READ|PROT_WRITE) = 0
mprotect(0x4001c000, 970752, PROT_READ|PROT_EXEC) = 0
munmap(0x40015000, 26453)               = 0
personality(PER_LINUX)                  = 0
getpid()                                = 1893
brk(0)                                  = 0x8053c64
brk(0x8053c84)                          = 0x8053c84
brk(0x8054000)                          = 0x8054000
uname({sys="Linux", node="xanadu", ...}) = 0
access("/proc/net", R_OK)               = 0
access("/proc/net/unix", R_OK)          = 0
socket(PF_UNIX, SOCK_DGRAM, 0)          = 3
socket(PF_INET, SOCK_DGRAM, IPPROTO_IP) = 4
access("/proc/net/if_inet6", R_OK)      = -1 ENOENT (No such file or directory)
access("/proc/net/ax25", R_OK)          = -1 ENOENT (No such file or directory)
access("/proc/net/nr", R_OK)            = -1 ENOENT (No such file or directory)
access("/proc/net/rose", R_OK)          = -1 ENOENT (No such file or directory)
access("/proc/net/ipx", R_OK)           = -1 ENOENT (No such file or directory)
access("/proc/net/appletalk", R_OK)     = -1 ENOENT (No such file or directory)
access("/proc/sys/net/econet", R_OK)    = -1 ENOENT (No such file or directory)
access("/proc/sys/net/ash", R_OK)       = -1 ENOENT (No such file or directory)
open("/proc/net/dev", O_RDONLY)         = 5
fstat64(0x5, 0xbffff5f4)                = -1 ENOSYS (Function not implemented)
fstat(5, {st_mode=S_IFREG|0444, st_size=0, ...}) = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40015000
read(5, "Inter-|   Receive               "..., 1024) = 578
brk(0x8055000)                          = 0x8055000
read(5, "", 1024)                       = 0
open("/usr/share/locale/locale.alias", O_RDONLY) = 6
fstat(6, {st_mode=S_IFREG|0644, st_size=2265, ...}) = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40016000
read(6, "# Locale name alias data base.\n#"..., 4096) = 2265
read(6, "", 4096)                       = 0
close(6)                                = 0
munmap(0x40016000, 4096)                = 0
open("/usr/share/i18n/locale.alias", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en_US/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
open("/usr/share/locale/en/LC_MESSAGES/net-tools.mo", O_RDONLY) = -1 ENOENT (No such file or directory)
ioctl(4, SIOCGIFCONF, 0xbffff73c)       = 0
close(5)                                = 0
munmap(0x40015000, 4096)                = 0
ioctl(4, SIOCGIFFLAGS, 0xbffff93c)      = 0
ioctl(4, SIOCGIFHWADDR, 0xbffff93c)     = 0
ioctl(4, SIOCGIFMETRIC, 0xbffff93c)     = 0
ioctl(4, SIOCGIFMTU, 0xbffff93c)        = 0
ioctl(4, SIOCGIFMAP, 0xbffff93c)        = 0
ioctl(4, SIOCGIFMAP, 0xbffff93c)        = 0
ioctl(4, 0x8942, 0xbffff93c)            = 0
ioctl(4, SIOCGIFADDR, 0xbffff93c)       = 0
ioctl(4, SIOCGIFDSTADDR, 0xbffff93c)    = 0
ioctl(4, SIOCGIFBRDADDR, 0xbffff93c)    = 0
ioctl(4, SIOCGIFNETMASK, 0xbffff93c)    = 0
fstat(1, {st_mode=S_IFREG|0664, st_size=4059, ...}) = 0
old_mmap(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x40015000
open("/proc/net/if_inet6", O_RDONLY)    = -1 ENOENT (No such file or directory)
close(4)                                = 0
write(1, "eth0      Link encap:Ethernet  H"..., 419) = 419
munmap(0x40015000, 4096)                = 0
_exit(0)                                = ?

-- 
Deepak Saxena - deepak@csociety.purdue.edu - phone://602.790.0500

Code Monkey, MontaVista Software, Inc. - THE Embedded Linux Experts

call me 'evil' call me 'tide is on your side' anything that you want
anybody knows you can conjure anything by the dark of the moon
  - Tori Amos, "Suede"