Advertisement
MestreLion

hostlist - lists all live hosts in local network

Mar 2nd, 2012
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.41 KB | None | 0 0
  1. #!/bin/bash
  2. #
  3. # hostlist - Scans LAN and lists hostnames from live hosts, saving to file
  4. #
  5. #    Copyright (C) 2011 Rodrigo Silva (MestreLion) <[email protected]>
  6. #
  7. #    This program is free software: you can redistribute it and/or modify
  8. #    it under the terms of the GNU General Public License as published by
  9. #    the Free Software Foundation, either version 3 of the License, or
  10. #    (at your option) any later version.
  11. #
  12. #    This program is distributed in the hope that it will be useful,
  13. #    but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. #    GNU General Public License for more details.
  16. #
  17. #    You should have received a copy of the GNU General Public License
  18. #    along with this program.  If not, see <http://www.gnu.org/licenses/gpl.html>.
  19. #
  20. # Quick-and-dirty script to feed bash completion for known hosts, using the
  21. # currently-unused-and-soon-to-be-deprecated ~/ssh/known_hosts2 file. It uses
  22. # smbtree, avahi-browse and ping to generate the list of live hosts
  23.  
  24. exec 3> /dev/null
  25. self="${0##*/}"
  26.  
  27. # default settings
  28. config="${HOME}/ssh/known_hosts2"
  29.  
  30. usage()
  31. {
  32. cat <<- USAGE
  33.     Usage: $self [--verbose] [--purge] [--config FILE]
  34. USAGE
  35. if [[ "$1" ]] ; then
  36.     cat <<- USAGE
  37.         Try '$self --help' for more information.
  38.     USAGE
  39.     exit 1
  40. fi
  41. cat <<-USAGE
  42.     Generates a list of live hostnames on LAN, adding new results to a file
  43.    
  44.     Options:
  45.     -v|--verbose     - print more details about what is being done
  46.     -p|--purge       - deletes all entries and re-creates the list with current scan results
  47.     -c|--config FILE - use FILE for storing results instead of the default file
  48.                        NOTE: -cFILE or --config=FILE syntax is NOT supported
  49.  
  50.     If used without any options, default behavior is to add new hostnames to
  51.     $config . Only the first "hosts" entry (if any) is read and changed.
  52.     All other settings in the file are ignored.
  53. USAGE
  54. exit 0
  55. }
  56. invalid()
  57. {
  58.     echo "$self: unrecognized option '$1'"
  59.     usage 1
  60. }
  61.  
  62. while [[ $# -gt 0 ]]; do
  63.     arg="$1"; shift
  64.     case "$arg" in
  65.     -h|--help   ) usage               ;;
  66.     -v|--verbose) verbose=1           ;;
  67.     -p|--purge  ) purge=1             ;;
  68.     -c|--config ) config="$1" ; shift ;;
  69.     *           ) invalid "$arg"      ;;
  70.     esac
  71. done
  72.  
  73. myip=$(ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | cut -d' ' -f1)
  74. subnet=${myip%.*}
  75.  
  76. hosts=$(
  77.     {
  78.         for ip in {1..254} ; do
  79.             { ping -n -c 1 -W 1 "${subnet}.${ip}" >&3 && { getent hosts "${subnet}.${ip}" | awk '{print $2}' ; } ; } &
  80.         done
  81.         smbtree -NS | awk -F '\t' '$2{ gsub(/^\\\\| +/,"",$2); print tolower($2) }'
  82.         avahi-browse --terminate --parsable --no-db-lookup --resolve _workstation._tcp | awk -F ';' '$1=="="{ print $(NF-3) }'
  83.     } | sort -u | awk '$0 != prev".local" { print ; prev=$0 }'
  84. )
  85.  
  86. echo "$hosts"
  87.  
  88. # References
  89. #avahi-browse --terminate --parsable --no-db-lookup _workstation._tcp | awk -F ';' '$3=="IPv4"{ gsub(/\\.*/,"",$4) ; print $4"."$(NF) }'
  90. #perl -ne 'print "$1 " if /^Host (.+)$/' ~/.ssh/config ; complete -W "$(_ssh_completion)" ssh
  91. #complete -W "$(echo `cat ~/.ssh/known_hosts | cut -f 1 -d ' ' | sed -e s/,.*//g | uniq | grep -v "\["`;)" ssh
  92. #complete -W "$(echo $(grep '^ssh ' .bash_history | sort -u | sed 's/^ssh //'))" ssh
  93. #reverse netbios: nmblookup
  94. #reverse avahi: avahi-resolve -a ipaddress
  95.  
  96. #desktop
  97. #helena
  98. #vb-debian604.local
  99. #vb-fedora16.local
  100. #vb-lfs-build
  101. #vb-xp
  102. #vb-win7
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement