Results 1 to 9 of 9

Thread: Best way to obtain the IP?

  1. #1
    Join Date
    Mar 2005
    Location
    Menlo Park, CA
    Posts
    22

    Best way to obtain the IP?

    I can look in /proc/net/arp, or parse the output
    from ifconfig or dhclient, is there a better way?
    Last edited by woodie; 07-19-2005 at 06:51 PM.

  2. #2
    Join Date
    Apr 2003
    Posts
    2,402
    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

  3. #3
    Join Date
    Mar 2005
    Location
    Menlo Park, CA
    Posts
    22
    I want to know my own IP, so I can use it
    in XML I have coming from a tivoweb module.

  4. #4
    Join Date
    Mar 2005
    Location
    Menlo Park, CA
    Posts
    22

    my get_ip_by_ifconfig command

    Code:
    #!/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]

  5. #5
    Join Date
    Aug 2004
    Posts
    4,086
    Quote Originally Posted by woodie
    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:
    Code:
    % 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.
    Last edited by Jamie; 07-19-2005 at 07:26 PM.

  6. #6
    Join Date
    Mar 2005
    Location
    Menlo Park, CA
    Posts
    22
    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.

    Code:
    #!/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]

  7. #7
    Join Date
    Aug 2004
    Posts
    4,086
    Quote Originally Posted by woodie
    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.
    Last edited by Jamie; 07-19-2005 at 08:42 PM.

  8. #8
    Join Date
    Mar 2005
    Location
    Menlo Park, CA
    Posts
    22
    Quote Originally Posted by Jamie
    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
    Last edited by woodie; 07-19-2005 at 09:26 PM.

  9. #9
    Join Date
    Jun 2005
    Posts
    3
    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://"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •