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