blob: 365847ee227675db59ea0d929ea3d8bd83a6faee [file] [log] [blame]
Michael Walsha4d92102018-03-26 16:59:05 -05001#!/usr/bin/wish
2
3# This file provides valuable host and IP manipulation procedures such as
4# get_host_name_ip, etc.
5
Joy Onyerikwufcb8aea2018-03-21 14:57:44 -05006my_source [list cmd.tcl data_proc.tcl]
Michael Walsha4d92102018-03-26 16:59:05 -05007
8
9proc 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
Joy Onyerikwufcb8aea2018-03-21 14:57:44 -050024 set print_output [expr !$quiet]
Michael Walsha4d92102018-03-26 16:59:05 -050025 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
Joy Onyerikwufcb8aea2018-03-21 14:57:44 -050048
49proc get_host_domain {host username password {quiet 1}} {
50
51 # Return the domain name of the host.
52
53 # If this procedure is unable to get the requested information, it will
54 # print an error message to stderr and return a blank value.
55
56 # Description of argument(s):
57 # host The host name or IP address of the system
58 # from which the information is to be
59 # obtained.
60 # username The host username.
61 # password The host password.
62 # quiet Indicates whether status information
63 # should be printed.
64
65 set print_output [expr !$quiet]
66 lassign [cmd_fnc \
67 "sshpass -p $password ssh -o StrictHostKeyChecking=no -k $username@$host\
68 'dnsdomainname'" ${quiet} "" ${print_output}] rc domain
69 return $domain
70
71}
72
73
74proc get_host_name_servers {host username password {quiet 1}} {
75
76 # Return a list of hostname or IP addresses.
77
78 # If this procedure is unable to get the requested information, it will
79 # print an error message to stderr and return a blank value.
80
81 # Description of argument(s):
82 # host The host name or IP address of the system
83 # from which the information is to be
84 # obtained.
85 # username The host username.
86 # password The host password.
87 # quiet Indicates whether status information
88 # should be printed.
89
90 set print_output [expr !$quiet]
91 lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
92 $username@$host\
93 grep -E '^[ ]*nameserver[ ]+' /etc/resolv.conf | awk '{print \$2}'"\
94 ${quiet} "" ${print_output}] rc name_servers
95 return [split $name_servers "\n"]
96
97}
98
99
100proc get_host_mac_address {host username password {interface {}} {quiet 1}} {
101
102 # Return the mac address of the host given a specific interface.
103
104 # If "interface" is left blank, it is set to the default interface.
105
106 # If this procedure is unable to get the requested information, it will
107 # print an error message to stderr and return a blank value.
108
109 # Description of argument(s):
110 # host The host name or IP address of the system
111 # from which the information is to be
112 # obtained.
113 # username The host username.
114 # password The host password.
115 # interface The target interface. Defaults to default
116 # interface if not set.
117 # quiet Indicates whether status information
118 # should be printed.
119
120 set print_output [expr !$quiet]
121 set_var_default interface [get_host_default_interface $host $username\
122 $password $quiet]
123 lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
124 $username@$host 'cat /sys/class/net/$interface/address'" \
125 ${quiet} "" ${print_output}] rc mac_address
126 return $mac_address
127
128}
129
130
131proc get_host_gateway {host username password {quiet 1}} {
132
133 # Return the gateway of the host.
134
135 # If this procedure is unable to get the requested information, it will
136 # print an error message to stderr and return a blank value.
137
138 # Description of argument(s):
139 # host The host name or IP address of the system
140 # from which the information is to be
141 # obtained.
142 # username The host username.
143 # password The host password.
144 # quiet Indicates whether status information
145 # should be printed.
146
147 set print_output [expr !$quiet]
148 lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
149 $username@$host ip route | grep -i '^default' | awk '{print \$3}'" \
150 ${quiet} "" ${print_output}] rc gateway
151 return $gateway
152
153}
154
155
156proc get_host_default_interface {host username password {quiet 1} } {
157
158 # Return the default interface of the host interface.
159
160 # If this procedure is unable to get the requested information, it will
161 # print an error message to stderr and return a blank value.
162
163 # Description of argument(s):
164 # host The host name or IP address of the system
165 # from which the information is to be
166 # obtained.
167 # username The host username.
168 # password The host password.
169 # quiet Indicates whether status information
170 # should be printed.
171
172 set print_output [expr !$quiet]
173 lassign [cmd_fnc "sshpass -p $password ssh -o StrictHostKeyChecking=no -k\
174 $username@$host ip route | grep -i '^default' | awk '{print \$5}'" \
175 ${quiet} "" ${print_output}] rc interface
176 return $interface
177
178}
179
180
181proc get_host_netmask {host username password {interface {}} {quiet 1}} {
182
183 # Return the subnet mask for the host.
184
185 # If this procedure is unable to get the requested information, it will
186 # print an error message to stderr and return a blank value.
187
188 # Description of argument(s):
189 # host The host name or IP address of the system
190 # from which the information is to be
191 # obtained.
192 # username The host username.
193 # password The host password.
194 # interface The target interface. Defaults to default
195 # interface if not set.
196 # quiet Indicates whether status information
197 # should be printed.
198
199 set print_output [expr !$quiet]
200 set sshpass_cmd \
201 "sshpass -p $password ssh -o StrictHostKeyChecking=no -k $username@$host"
202 set_var_default interface [get_host_default_interface $host $username\
203 $password $quiet]
204 lassign [cmd_fnc \
205 "$sshpass_cmd ifconfig $interface | grep -i mask"\
206 ${quiet} "" ${print_output}] rc out_buf
207 if {[string match *broadcast* $out_buf]} {
208 set mask_cmd "ifconfig $interface | grep ask | awk '{print \$4}'"
209 } else {
210 set mask_cmd "ifconfig $interface | grep ask | cut -d ':' -f 4"
211 }
212 lassign [cmd_fnc "$sshpass_cmd $mask_cmd" $quiet] rc netmask
213 return $netmask
214
215}