Michael Walsh | a4d9210 | 2018-03-26 16:59:05 -0500 | [diff] [blame] | 1 | #!/usr/bin/wish |
| 2 | |
| 3 | # This file provides valuable host and IP manipulation procedures such as |
| 4 | # get_host_name_ip, etc. |
| 5 | |
| 6 | my_source [list cmd.tcl] |
| 7 | |
| 8 | |
| 9 | proc get_host_name_ip {host {quiet 1}} { |
| 10 | |
| 11 | # Get the host name, short host name and the IP address and return them as |
| 12 | # a list. |
| 13 | # If this procedure is unable to get the requested information, it will |
| 14 | # print an error message to stderr and return blank values. |
| 15 | |
| 16 | # Example call: |
| 17 | # lassign [get_host_name_ip $host] host_name short_host_name ip_address |
| 18 | |
| 19 | # Description of argument(s): |
| 20 | # host The host name or IP address to be obtained. |
| 21 | # quiet Indicates whether status information |
| 22 | # should be printed. |
| 23 | |
| 24 | if { ${quiet} } { set print_output 0 } else { set print_output 1 } |
| 25 | lassign [cmd_fnc "host $host" "${quiet}" "" "${print_output}"] rc out_buf |
| 26 | if { $rc != 0 } { return [list "" "" ""]} |
| 27 | |
| 28 | if { [regexp "has address" $out_buf] } { |
| 29 | # Host is host name. |
| 30 | # Format of output: |
| 31 | # hostname.bla.com has address n.n.n.n. |
| 32 | lassign [split $out_buf " "] host_name fill1 fill2 ip_address |
| 33 | } elseif { [regexp "domain name pointer" $out_buf] } { |
| 34 | # Host is IP address. |
| 35 | # Format of output: |
| 36 | # n.n.n.n.in-addr.arpa domain name pointer hostname.bla.com. |
| 37 | set ip_address ${host} |
| 38 | lassign [split $out_buf " "] fill0 fill1 fill2 fill3 host_name |
| 39 | set host_name [string trimright $host_name {.}] |
| 40 | } |
| 41 | # Create the short name from the host name. |
| 42 | lassign [split $host_name "."] short_host_name |
| 43 | |
| 44 | return [list ${host_name} ${short_host_name} ${ip_address}] |
| 45 | |
| 46 | } |
| 47 | |