Initial add of defaria.com
[clearscm.git] / defaria.com / Computers / code / adm / bin / pingnet
1 #!/bin/bash
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   echo -e "$me: Usage: $me { count }"
23   echo -e "\twhere count = number of times to ping a machine before
24 considering"
25   echo -e "\tthe machine to be not responding (default count = 2)."
26   exit 1
27 } # usage
28
29 os=
30
31 function get_os {
32   #for now skip get_os
33   os=Unknown
34   return
35   machine=$1
36   # Attempt to determine the OS. First attempt to remsh to the machine and
37   # do a uname(1). This assumes Unix. We are unable to remsh then we'll
38   # assume that it's a PC (it could also be a line printer or any other of
39   # a number of network pingable devices!)
40   os=$(rsh $machine -n uname 2>&1)
41
42   # We're gonna make some guesses here...
43   if [[ "$os" = "HP-UX" ||
44         "$os" = "Linux" ]]; then
45     : Do nothing!
46   elif [[ "$os" = *Lost\ connection ]]; then
47     os="Linux? (Lost connection)"
48   elif [[ "$os" = *Permission\ denied. ]]; then
49     os="Linux? (Permission denied)"
50   elif [[ "$os" = *Login\ incorrect ]]; then
51     os="HP-UX? (Login incorrect)"
52   elif [[ "$os" = *Connection\ refused ]]; then
53     os="NT? (Connection refused)"
54   else
55     os="Unknown"
56   fi
57 } # get_os
58
59 declare -i count=2
60
61 if [ $# -eq 1 ]; then
62   count=$1
63 elif [ $# -gt 1 ]; then
64   usage
65 fi
66
67 esc=$(echo "\033")
68
69 if [ "$TERM" = "dtterm" -o -z DTTERM ]; then
70   export normal="$esc[39m"
71   export red="$esc[31m"
72   export green="$esc[32m"
73 elif [ "$TERM" = "hp" -o "$TERM" = "hpterm" ]; then
74   export normal="$esc&d@$esc&v0S"
75   export red="$esc&v1S"
76   export green="$esc&v2S"
77 fi
78
79 # Print heading
80 echo -e "       Machine\t\t      IP\tState\t  OS"
81 echo -e "-----------------------\t-------------\t-----\t-------"
82
83 subnet=${subnet:-192.168}
84 declare -i starting_subnet_octect=${starting_subnet_octect:-0}
85 declare -i ending_subnet_octect=${ending_subnet_octect:-0}
86 declare -i subnet_octect=starting_subnet_octect
87 declare -i starting_octect=${starting_octect:-0}
88 declare -i ending_octect=${ending_octect:-255}
89 declare -i octect=$starting_octect
90
91 while [ $subnet_octect -le $ending_subnet_octect ]; do
92   while [ $octect -le $ending_octect ]; do
93     ip=$subnet.$subnet_octect.$octect
94     machine=$ip
95     nslookup $ip 2>&1 | grep Non-exist > /dev/null 2>&1
96     if [ $? -ne 0 ]; then
97       machine=$(nslookup $ip | grep Name: | awk '{print $2}' | cut -f1 -d.)
98     fi
99     echo -e "$machine\t\c"
100     if [ ${#machine} -lt 8 ]; then
101       echo -e "\t\t\c"
102     elif [ ${#machine} -lt 16 ]; then
103       echo -e "\t\c"
104     fi
105     echo -e "$ip\t\c"
106     ping_result=$(ping $ip -n $count | tail -1 | grep "100% loss")
107     if [ -z "$ping_result" ]; then
108       echo -e "${green}UP${normal}\t\c"
109       get_os $machine
110       echo -e "$os"
111     else
112       echo -e "${red}DOWN${normal}\tUnknown"
113     fi
114     let octect=octect+1
115   done
116   let subnet_octect=subnet_octect+1
117   let octect=starting_octect
118 done