Added client work scripts
[clearscm.git] / clients / HP / pingnet
1 #!/bin/ksh
2 ################################################################################
3 #
4 # File:         pingnet
5 # RCS:          $Header: pingnet,v 1.3 98/03/04 00:42:49 defaria Exp $
6 # Description:  A script to ping all machines and report status. This script
7 #               uses /etc/hosts and selects only machines in the IP range of
8 #               15.0.96.x to 15.0.99.x.
9 # Author:       Andrew DeFaria <Andrew@DeFaria.com>
10 # Created:      Sat Oct 26 10:04:28 PDT 1996
11 # Modified:     Sat Oct 26 12:05:26 PDT 1996 Andrew DeFaria <defaria@cup.hp.com>
12 # Parameters:   count (default 2): number of times to ping a machine before
13 #               considering it not responding.
14 # Language:     Korn Shell
15 #
16 # (c) Copyright 2001, Andrew@DeFaria.com, all rights reserved
17 #
18 ################################################################################
19 me=$(basename $0)
20
21 function usage {
22   print "$me: Usage: $me { count }"
23   print "\twhere count = number of times to ping a machine before
24 considering"
25   print "\tthe machine to be not responding (default count = 2)."
26   exit 1
27 } # usage
28
29 os=
30
31 function get_os {
32   machine=$1
33   # Attempt to determine the OS. First attempt to remsh to the machine and
34   # do a uname(1). This assumes Unix. We are unable to remsh then we'll
35   # assume that it's a PC (it could also be a line printer or any other of
36   # a number of network pingable devices!)
37   os=$(remsh $machine -n uname 2>&1)
38
39   # We're gonna make some guesses here...
40   if [[ "$os" = "HP-UX" ||
41         "$os" = "Linux" ]]; then
42     : Do nothing!
43   elif [[ "$os" = *Lost\ connection ]]; then
44     os="Linux? (Lost connection)"
45   elif [[ "$os" = *Permission\ denied. ]]; then
46     os="Linux? (Permission denied)"
47   elif [[ "$os" = *Login\ incorrect ]]; then
48     os="HP-UX? (Login incorrect)"
49   elif [[ "$os" = *Connection\ refused ]]; then
50     os="NT? (Connection refused)"
51   else
52     os="Unknown: $os"
53   fi
54 } # get_os
55
56 integer count=2
57
58 if [ $# -eq 1 ]; then
59   count=$1
60 elif [ $# -gt 1 ]; then
61   usage
62 fi
63
64 esc=$(print "\033")
65
66 if [ "$TERM" = "dtterm" -o -z DTTERM ]; then
67   export normal="$esc[39m"
68   export red="$esc[31m"
69   export green="$esc[32m"
70 elif [ "$TERM" = "hp" -o "$TERM" = "hpterm" ]; then
71   export normal="$esc&d@$esc&v0S"
72   export red="$esc&v1S"
73   export green="$esc&v2S"
74 fi
75
76 # Print heading
77 print "       Machine\t\t      IP\tState\t  OS"
78 print - "-----------------------\t-------------\t-----\t-------"
79
80 subnet=${subnet:-15.28}
81 integer starting_subnet_octect=${starting_subnet_octect:-96}
82 integer ending_subnet_octect=${ending_subnet_octect:-103}
83 integer subnet_octect=starting_subnet_octect
84 integer starting_octect=${starting_octect:-0}
85 integer ending_octect=${ending_octect:-255}
86 integer octect=$starting_octect
87
88 while [ $subnet_octect -le $ending_subnet_octect ]; do
89   while [ $octect -le $ending_octect ]; do
90     ip=$subnet.$subnet_octect.$octect
91     machine=$ip
92     nslookup $ip 2>&1 | grep Non-exist > /dev/null 2>&1
93     if [ $? -ne 0 ]; then
94       machine=$(nslookup $ip | grep Name: | awk '{print $2}' | cut -f1 -d.)
95     fi
96     print "$machine\t\c"
97     if [ ${#machine} -lt 8 ]; then
98       print "\t\t\c"
99     elif [ ${#machine} -lt 16 ]; then
100       print "\t\c"
101     fi
102     print "$ip\t\c"
103     ping_result=$(ping $ip -n $count | tail -1 | grep "100% packet loss")
104     if [ -z "$ping_result" -eq 0 ]; then
105       print "${green}UP${normal}\t\c"
106       get_os $machine
107       print "$os"
108     else
109       print "${red}DOWN${normal}\tUnknown"
110     fi
111     let octect=octect+1
112   done
113   let subnet_octect=subnet_octect+1
114   let octect=starting_octect
115 done