Compare commits

...

2 Commits

Author SHA1 Message Date
df 743ca7b75f New versions of {system hostname}, {system ip} using Jim API 2020-11-14 13:22:12 +00:00
df 2057c7ac75 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.
2020-11-14 13:22:12 +00:00
1 changed files with 10 additions and 7 deletions

View File

@ -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
}