Various scripts in various languages.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

capture-sample.sh 6.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. #!/bin/sh
  2. #
  3. # capture-sample.sh, Copyright © 2011 Matteo Cypriani <mcy@lm7.fr>
  4. #
  5. ########################################################################
  6. # This program is licensed under the terms of the Expat license.
  7. #
  8. # Permission is hereby granted, free of charge, to any person obtaining
  9. # a copy of this software and associated documentation files (the
  10. # "Software"), to deal in the Software without restriction, including
  11. # without limitation the rights to use, copy, modify, merge, publish,
  12. # distribute, sublicense, and/or sell copies of the Software, and to
  13. # permit persons to whom the Software is furnished to do so, subject to
  14. # the following conditions:
  15. #
  16. # The above copyright notice and this permission notice shall be
  17. # included in all copies or substantial portions of the Software.
  18. #
  19. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  22. # IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  23. # CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  24. # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  25. # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. ########################################################################
  27. #
  28. # Captures a few packets on a Wi-Fi interface, using Tcpdump, and
  29. # writes them to a Pcap file along with some information about the
  30. # system. A tarball containing all these files is finally created.
  31. #set -x
  32. set -e
  33. ## Parameters (tune them if you want) ##
  34. # Number of packet to capture:
  35. NB_PKT=10
  36. # Maximal time of a capture on a given channel:
  37. TIMEOUT=15
  38. # Temporary directory:
  39. TMP=/tmp
  40. # Name of the capture file:
  41. FILE=capture.pcap
  42. # First channel to scan:
  43. CHANNEL=1
  44. ## Functions ##
  45. # Displays the message in argument on the error output and exits
  46. error()
  47. {
  48. printf '%s\n' "$1" >&2
  49. clean
  50. exit 1
  51. }
  52. # Deletes the temporary files
  53. clean()
  54. {
  55. rm -fr "$DESTDIR"
  56. }
  57. # Verifies the presence of the needed programs
  58. check_dependencies()
  59. {
  60. command -v tcpdump >/dev/null \
  61. || error "tcpdump is required to run this program!"
  62. if [ "$OS" = Linux ] ; then
  63. [ -x /sbin/iwconfig ] \
  64. || error "iwconfig is required to run this program on Linux!"
  65. fi
  66. }
  67. # Switches the capture interface to monitor mode, on the channel in
  68. # argument
  69. iface_set_channel()
  70. {
  71. echo "Setting interface $IFACE on channel $1..."
  72. case "$OS" in
  73. Linux)
  74. iface_down
  75. iwconfig "$IFACE" mode managed
  76. iface_up
  77. iwconfig "$IFACE" channel "$1"
  78. ;;
  79. NetBSD | OpenBSD)
  80. iface_down
  81. ifconfig "$IFACE" media autoselect chan "$1"
  82. iface_up
  83. ;;
  84. *)
  85. error "Your OS is not supported."
  86. ;;
  87. esac \
  88. || error "Cannot set the channel!"
  89. echo "Channel set."
  90. iface_monitor
  91. }
  92. # Switches the capture interface to monitor mode
  93. iface_monitor()
  94. {
  95. iface_down
  96. printf 'Switching interface %s to monitor mode... ' "$IFACE"
  97. case "$OS" in
  98. Linux)
  99. iwconfig "$IFACE" mode monitor
  100. ;;
  101. NetBSD | OpenBSD)
  102. ifconfig "$IFACE" media autoselect mediaopt monitor
  103. ;;
  104. *)
  105. error "Your OS is not supported."
  106. ;;
  107. esac \
  108. || error "Cannot switch the interface to monitor mode!"
  109. echo "OK."
  110. iface_up
  111. }
  112. # Shuts down the capture interface
  113. iface_down()
  114. {
  115. printf 'Shuting down interface %s... ' "$IFACE"
  116. ifconfig "$IFACE" down \
  117. || error "Cannot shut down the interface!"
  118. echo "OK."
  119. }
  120. # Turns on the capture interface
  121. iface_up()
  122. {
  123. printf 'Turning up interface %s... ' "$IFACE"
  124. ifconfig "$IFACE" up \
  125. || error "Cannot turn the interface up!"
  126. echo "OK."
  127. }
  128. # Invokes tcpdump and displays the number of packets captured
  129. invoke_tcpdump()
  130. {
  131. NCAP=$(tcpdump -i "$IFACE" -c "$NB_PKT" -w "$FILE" 2>&1 \
  132. | sed -nr 's/([[:digit:]]+) packets received by filter$/\1/p')
  133. [ -z "$NCAP" ] \
  134. && error "Error parsing the tcpdump messages! (NCAP=\"$NCAP\")"
  135. echo "$NCAP"
  136. }
  137. # Waits for a number of seconds, then kills any tcpdump process
  138. wait_tcpdump()
  139. {
  140. sleep "$1"
  141. echo "$1 seconds passed, killing all tcpdump processes..."
  142. pkill tcpdump
  143. }
  144. # Gets some information about the running system
  145. gather_system_information()
  146. {
  147. printf "Gathering system information... "
  148. # Kernel & other information:
  149. uname -a >"$DESTDIR"/uname-a
  150. # Wi-Fi interface information:
  151. ifconfig "$IFACE" >"$DESTDIR/ifconfig_$IFACE"
  152. [ "$OS" = Linux ] \
  153. && iwconfig "$IFACE" >"$DESTDIR/iwconfig_$IFACE"
  154. # PCI devices:
  155. command -v lspci >/dev/null \
  156. && lspci >"$DESTDIR"/lspci \
  157. || echo "lspci not available! Please install pciutils. "
  158. # USB devices:
  159. gather_usb_devices
  160. # Loaded kernel modules:
  161. gather_kernel_modules
  162. echo "OK."
  163. }
  164. # Gets information about the plugged usb devices
  165. gather_usb_devices()
  166. {
  167. case "$OS" in
  168. Linux)
  169. command -v lsusb >/dev/null \
  170. && lsusb >"$DESTDIR"/lsusb \
  171. || echo "lsusb not available! Please install usbutils. "
  172. ;;
  173. NetBSD | OpenBSD | DragonFly)
  174. command -v usbstats >/dev/null \
  175. && usbstats >"$DESTDIR"/usbstats \
  176. || echo "usbstats not available! Please install usbutil. "
  177. ;;
  178. esac
  179. }
  180. # Gets information about the loaded kernel modules
  181. gather_kernel_modules()
  182. {
  183. case "$OS" in
  184. Linux)
  185. lsmod >"$DESTDIR"/lsmod
  186. ;;
  187. NetBSD | OpenBSD)
  188. modstat >"$DESTDIR"/modstat
  189. ;;
  190. DragonFly)
  191. kldstat >"$DESTDIR"/kldstat
  192. ;;
  193. esac
  194. }
  195. # Compresses the destination directory to a tarball
  196. create_archive()
  197. {
  198. TARBALL="${DESTDIR}.tar.gz"
  199. DIR=$(basename "$DESTDIR")
  200. tar -C "$TMP" -czf "$TARBALL" "$DIR"
  201. echo "Archive \"$TARBALL\" created."
  202. }
  203. ## Main program ##
  204. [ $# -eq 1 ] \
  205. || error "Usage: $0 <wifi_interface>"
  206. # Interface to capture from:
  207. IFACE=$1
  208. # Machine information:
  209. OS=$(uname)
  210. OS_RELEASE=$(uname -r)
  211. HOSTNAME=$(uname -n)
  212. # Current date:
  213. DATE=$(date +%FT%H%M%S)
  214. # Temporary destination directory:
  215. DESTDIR=$(mktemp -d \
  216. "$TMP/capture_${OS}-${OS_RELEASE}_${IFACE}_${HOSTNAME}_${DATE}_XXX")
  217. # Update capture file with full path:
  218. FILE="$DESTDIR/$FILE"
  219. check_dependencies
  220. echo "Trying to capture $NB_PKT packets..."
  221. CAPTURED=0
  222. while [ "$CAPTURED" -eq 0 ] && [ "$CHANNEL" -le 14 ] ; do
  223. echo
  224. iface_set_channel "$CHANNEL"
  225. wait_tcpdump "$TIMEOUT" &
  226. CAPTURED=$(invoke_tcpdump)
  227. [ "$CAPTURED" -eq 0 ] \
  228. && echo "No packet captured on channel $CHANNEL." \
  229. || echo "$CAPTURED packets captured on channel $CHANNEL."
  230. CHANNEL=$((CHANNEL + 1))
  231. done
  232. echo
  233. [ "$CAPTURED" -gt 0 ] \
  234. || error "Failed to capture any packet!"
  235. echo "Capture file \"$FILE\" created."
  236. gather_system_information
  237. create_archive
  238. clean
  239. printf '\nYou can now shut down the interface %s if you would like:\n' "$IFACE"
  240. printf '\tifconfig %s down\n' "$IFACE"