1#!/usr/bin/wish
2
3# This file provides valuable host and IP manipulation procedures such as get_host_name_ip, etc.
4
5my_source [list cmd.tcl data_proc.tcl]
6
7
8proc get_host_name_ip {host {quiet 1}} {
9
10  # Get the host name, short host name and the IP address and return them as a list.
11  # If this procedure is unable to get the requested information, it will print an error message to stderr
12  # and return blank values.
13
14  # Example call:
15  # lassign [get_host_name_ip $host] host_name short_host_name ip_address
16
17  # Description of argument(s):
18  # host                            The host name or IP address to be obtained.
19  # quiet                           Indicates whether status information should be printed.
20
21  set print_output [expr !$quiet]
22  lassign [cmd_fnc "host $host" "${quiet}" "" "${print_output}"] rc out_buf
23  if { $rc != 0 } { return [list "" "" ""]}
24
25  if { [regexp "has address" $out_buf] } {
26    # Host is host name.
27    # Format of output:
28    # hostname.bla.com has address n.n.n.n.
29    lassign [split $out_buf " "] host_name fill1 fill2 ip_address
30  } elseif { [regexp "domain name pointer" $out_buf] } {
31    # Host is IP address.
32    # Format of output:
33    # n.n.n.n.in-addr.arpa domain name pointer hostname.bla.com.
34    set ip_address ${host}
35    lassign [split $out_buf " "] fill0 fill1 fill2 fill3 host_name
36    set host_name [string trimright $host_name {.}]
37  }
38  # Create the short name from the host name.
39  lassign [split $host_name "."] short_host_name
40
41  return [list ${host_name} ${short_host_name} ${ip_address}]
42
43}
44
45
46proc get_host_domain {host username password {quiet 1}} {
47
48  # Return the domain name of the host.
49
50  # If this procedure is unable to get the requested information, it will
51  # print an error message to stderr and return a blank value.
52
53  # Description of argument(s):
54  # host                            The host name or IP address of the system
55  #                                 from which the information is to be
56  #                                 obtained.
57  # username                        The host username.
58  # password                        The host password.
59  # quiet                           Indicates whether status information
60  #                                 should be printed.
61
62  set print_output [expr !$quiet]
63  lassign [cmd_fnc \
64    "sshpass -p $password ssh -o StrictHostKeyChecking=no -k $username@$host\
65    'dnsdomainname'" ${quiet} "" ${print_output}] rc domain
66  return $domain
67
68}
69
70
71proc get_host_name_servers {host username password {quiet 1}} {
72
73  # Return a list of hostname or IP addresses.
74
75  # If this procedure is unable to get the requested information, it will
76  # print an error message to stderr and return a blank value.
77
78  # Description of argument(s):
79  # host                            The host name or IP address of the system
80  #                                 from which the information is to be
81  #                                 obtained.
82  # username                        The host username.
83  # password                        The host password.
84  # quiet                           Indicates whether status information
85  #                                 should be printed.
86
87  set print_output [expr !$quiet]
88  lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
89    $username@$host\
90    grep -E '^[ ]*nameserver[ ]+' /etc/resolv.conf | awk '{print \$2}'"\
91    ${quiet} "" ${print_output}] rc name_servers
92  return [split $name_servers "\n"]
93
94}
95
96
97proc get_host_mac_address {host username password {interface {}} {quiet 1}} {
98
99  # Return the mac address of the host given a specific interface.
100
101  # If "interface" is left blank, it is set to the default interface.
102
103  # If this procedure is unable to get the requested information, it will
104  # print an error message to stderr and return a blank value.
105
106  # Description of argument(s):
107  # host                            The host name or IP address of the system
108  #                                 from which the information is to be
109  #                                 obtained.
110  # username                        The host username.
111  # password                        The host password.
112  # interface                       The target interface. Defaults to default
113  #                                 interface if not set.
114  # quiet                           Indicates whether status information
115  #                                 should be printed.
116
117  set print_output [expr !$quiet]
118  set_var_default interface [get_host_default_interface $host $username\
119    $password $quiet]
120  lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
121    $username@$host 'cat /sys/class/net/$interface/address'" \
122    ${quiet} "" ${print_output}] rc mac_address
123  return $mac_address
124
125}
126
127
128proc get_host_gateway {host username password {quiet 1}} {
129
130  # Return the gateway of the host.
131
132  # If this procedure is unable to get the requested information, it will
133  # print an error message to stderr and return a blank value.
134
135  # Description of argument(s):
136  # host                            The host name or IP address of the system
137  #                                 from which the information is to be
138  #                                 obtained.
139  # username                        The host username.
140  # password                        The host password.
141  # quiet                           Indicates whether status information
142  #                                 should be printed.
143
144  set print_output [expr !$quiet]
145  lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
146    $username@$host ip route | grep -i '^default' | awk '{print \$3}'" \
147    ${quiet} "" ${print_output}] rc gateway
148  return $gateway
149
150}
151
152
153proc get_host_default_interface {host username password {quiet 1} } {
154
155  # Return the default interface of the host interface.
156
157  # If this procedure is unable to get the requested information, it will
158  # print an error message to stderr and return a blank value.
159
160  # Description of argument(s):
161  # host                            The host name or IP address of the system
162  #                                 from which the information is to be
163  #                                 obtained.
164  # username                        The host username.
165  # password                        The host password.
166  # quiet                           Indicates whether status information
167  #                                 should be printed.
168
169  set print_output [expr !$quiet]
170  lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
171    $username@$host ip route | grep -i '^default' | awk '{print \$5}'" \
172    ${quiet} "" ${print_output}] rc interface
173  return $interface
174
175}
176
177
178proc get_host_netmask {host username password {interface {}} {quiet 1}} {
179
180  # Return the subnet mask for the host.
181
182  # If this procedure is unable to get the requested information, it will
183  # print an error message to stderr and return a blank value.
184
185  # Description of argument(s):
186  # host                            The host name or IP address of the system
187  #                                 from which the information is to be
188  #                                 obtained.
189  # username                        The host username.
190  # password                        The host password.
191  # interface                       The target interface. Defaults to default
192  #                                 interface if not set.
193  # quiet                           Indicates whether status information
194  #                                 should be printed.
195
196  set print_output [expr !$quiet]
197  set sshpass_cmd \
198    "sshpass -p $password ssh -o StrictHostKeyChecking=no -k $username@$host"
199  set_var_default interface [get_host_default_interface $host $username\
200    $password $quiet]
201  lassign [cmd_fnc \
202    "$sshpass_cmd ifconfig $interface | grep -i mask"\
203    ${quiet} "" ${print_output}] rc out_buf
204  if {[string match *broadcast* $out_buf]} {
205    set mask_cmd "ifconfig $interface | grep ask | awk '{print \$4}'"
206  } else {
207    set mask_cmd "ifconfig $interface | grep ask | cut -d ':' -f 4"
208  }
209  lassign [cmd_fnc "$sshpass_cmd $mask_cmd" $quiet] rc netmask
210  return $netmask
211
212}
213