PDA

View Full Version : Best way to obtain the IP?



woodie
07-19-2005, 04:26 PM
I can look in /proc/net/arp, or parse the output
from ifconfig or dhclient, is there a better way?

eastwind
07-19-2005, 05:00 PM
Not sure the purpose. What better way would you like? An ipscan might do what you want, but you need to explain better what you want.

ew

woodie
07-19-2005, 05:11 PM
I want to know my own IP, so I can use it
in XML I have coming from a tivoweb module.

woodie
07-19-2005, 05:38 PM
#!/tvbin/tivosh

proc get_ip_by_ifconfig {} {
set ifconfig [exec "ifconfig"]
set lines [split $ifconfig "\n"]
set ip ""
foreach l $lines {
set raw [split [string trim $l] " "]
if {[string compare [lindex $raw 0] "inet"] >= 0} {
set addr [split [lindex $raw 1] ":"]
if {[string compare [lindex $addr 0] "addr"] >= 0} {
if {[string compare [lindex $addr 1] "127.0.0.1"] != 0} {
set ip [lindex $addr 1]
}
}
}
}
return $ip
}

puts [get_ip_by_ifconfig]

Jamie
07-19-2005, 06:12 PM
I can look in /proc/net/arp, or parse the output
from ifconfig or dhclient, is there a better way?http://wiki.tcl.tk/3015

Note that a tivo may have more than one public ip address if it has more than one network adapter attached. If you are sure it is the eth0 address you want, this should work:
% set myip [regexp -inline {\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}} [exec /sbin/ifconfig eth0]]The link shows some better ways.

woodie
07-19-2005, 07:25 PM
Hmm, that (above) didn't work for me.

Here is what I ended up with... same idea, fewer keystrokes.
It will return the first IP that isn't the loopback address.


#!/tvbin/tivosh

proc get_ip_by_ifconfig {} {
foreach raw [split [exec "ifconfig"] " "] {
if {[string match "addr:*" $raw]} {
if {! [string match "*127.0.0.1" $raw]} {
return [lindex [split $raw ":" ] 1]
}
}
}
}

puts [get_ip_by_ifconfig]

Jamie
07-19-2005, 07:39 PM
Hmm, that (above) didn't work for me.I tested it and it worked on an S2. Not sure what would be different on an S1. The interface name? Notice I used a hardwired 'eth0'.

If you have a socket back to the host you are communicating with, there are better ways, as described in the link.

woodie
07-19-2005, 08:18 PM
If you have a socket back to the host you are communicating with, there are better ways, as described in the link.

Yes, it seems like tivoweb should have an environment variable
that indicates which interface is communicatiing with the user.

I assume that the SA2 has more IP mojo. There is no place in the SA1 UI,
even the tivoweb UI, that indicates what IP address the tivo is on.

I noticed that my bogus TIVO_SVR_ADDR environment var
is also listed in this file. I wonder if they are valid in the SA2.

/tvlib/tcl/tv/Addr.itcl

caplan8293
07-27-2005, 02:13 PM
This is probably not the most efficient, but it works for me and you can do it all on one line. Just make sure you have grep and sed installed on your Tivo:


/sbin/ifconfig eth0 | grep "inet addr"| sed "s/ Bcas.*$//"|sed "s/^.*inet addr://"