forked from hummypkg/webif
New versions of {system hostname}, {system ip} using Jim API
hostname: use [os.gethostname]. ip: get a datagram socket for [os.gethostname]:0 and return the IP part of its ip:port, or 127.0.0.1.
This commit is contained in:
parent
d7c15163e1
commit
2057c7ac75
|
@ -21,19 +21,22 @@ proc {system model} {} {{model ""}} {
|
|||
}
|
||||
|
||||
proc {system hostname} {} {
|
||||
if {[catch {set hostname [string trim [exec hostname]]}]} {
|
||||
if {[catch {set hostname [os.gethostname]}]} {
|
||||
set hostname "humax"
|
||||
}
|
||||
return $hostname
|
||||
}
|
||||
|
||||
proc {system ip} {} {
|
||||
if {[catch {set fp [open /etc/hosts r]}]} {
|
||||
set ip "127.0.0.1"
|
||||
} else {
|
||||
set ipl [lindex [split [$fp read] "\n"] 1]
|
||||
regsub -- {[[:space:]].*} $ipl "" ip
|
||||
$fp close
|
||||
# try for a usable IP address of the machine (w/o parsing hosts file)
|
||||
set ip "127.0.0.1"
|
||||
if {![catch {set mysock [socket dgram "[os.gethostname]:0"]]}]} {
|
||||
set myip ""
|
||||
catch { set myip [lindex [split [$mysock sockname] ":"] 0]
|
||||
$mysock close }
|
||||
if {$myip ne ""} {
|
||||
set ip $myip
|
||||
}
|
||||
}
|
||||
return $ip
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue