shell dot on steroids https://pagure.io/shellfu

recon.sh 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/bin/bash
  2. ffoo import core
  3. ffoo import fun
  4. age_of_leaf() {
  5. #
  6. # Print age of each filename on STDIN
  7. #
  8. local now=$1
  9. local ctime
  10. sed 's/.*/"&"/' \
  11. | xargs stat -c %Z \
  12. | while read ctime;
  13. do echo $(($now - $ctime));
  14. done
  15. }
  16. age_of_tree() {
  17. #
  18. # Age of the youngest leaf in the tree
  19. #
  20. local now=$(date +%s)
  21. find -L "$1" \
  22. | age_of_leaf $now \
  23. | sort -n \
  24. | head -1
  25. }
  26. cat_uri() {
  27. #
  28. # Cat for URIs
  29. #
  30. wget -q $1 -O - || echo "ERROR: $?" >&2
  31. }
  32. pids_matching() {
  33. #
  34. # Print list of PIDs where command matches regex(es)
  35. #
  36. # Note that *comm* field of *ps* is used, which may be
  37. # truncated, so don't rely on name ending
  38. #
  39. local all_pids=""
  40. local pids=""
  41. local pfx=""
  42. local pattern
  43. for pattern in "$@";
  44. do
  45. local pids=$(
  46. ps -e -o pid= -o comm= \
  47. | perl -ne "
  48. chomp;
  49. s/^ +//;
  50. my (\$pid, \$comm) = split m/ +/;
  51. next unless \$comm =~ m/$pattern/;
  52. print qq/\$pid /;
  53. "
  54. )
  55. debug -v pattern pids
  56. all_pids="$all_pids$pfx$pids"
  57. pfx=" "
  58. done
  59. debug -v all_pids
  60. test -n "$all_pids" && echo $all_pids
  61. }
  62. listening_on() {
  63. #
  64. # Check if something is listening on local TCP port $1
  65. #
  66. local host="localhost"
  67. local port="$1"
  68. debug -v host port
  69. test -n "$port" || usage_is "PORT"
  70. netstat -ntlp \
  71. | sed -e 's/ */ /g' \
  72. | cut -d\ -f 4 \
  73. | grep -qs :$port\$
  74. }
  75. reachable_by_ping() {
  76. #
  77. # Check if host $1 is reachable by ICMP ping
  78. #
  79. local host=$1
  80. debug -v host
  81. test -n "$host" || usage_is "HOST"
  82. ping -w 1 $host >& /dev/null
  83. }
  84. reachable_by_ssh() {
  85. #
  86. # Check if host $1 is reachable by SSH
  87. #
  88. local host=$1
  89. debug -v host
  90. test -n "$host" || usage_is "HOST"
  91. ssh -n -o ConnectTimeout=3 $host -- true >/dev/null 2>&1;
  92. }
  93. reachable_by_tcp() {
  94. #
  95. # Check if TCP port $1 on host $2 is reachable (open)
  96. #
  97. local port=$1
  98. local host=$2
  99. debug -v port host
  100. test -n "$host" -a -n "$port" || usage_is "PORT HOST"
  101. nc -w 1 $host $port >/dev/null 2>&1;
  102. }
  103. filter_hosts() {
  104. #
  105. # Filter hosts on STDIN, leaving those reachable
  106. #
  107. # Default check is by ICMP only, but you can use arguments
  108. # to enable/disable checks:
  109. #
  110. # -t|--tcp PORT enable TCP check on PORT
  111. # -p|--ping enable ICMP ping (default)
  112. # -P|--no-ping disable ICMP ping
  113. # -s|--ssh enable SSH (check by calling `true`)
  114. # -S|--no-ssh disable SSH
  115. #
  116. local ping_fun="reachable_by_ping"
  117. local ssh_fun=true
  118. local tcp_fun=true
  119. # note: here the ^ true builtin acts as placeholder (i.e. no check)
  120. local tcp_port=0
  121. while true; do case $1 in
  122. -t|--tcp) tcp_fun="reachable_by_tcp $2"; shift 2 ;;
  123. -p|--ping) ping_fun="reachable_by_ping"; shift ;;
  124. -P|--no-ping) ping_fun=true; shift ;;
  125. -s|--ssh) ssh_fun="reachable_by_ssh"; shift ;;
  126. -S|--no-ssh) ssh_fun=true; shift ;;
  127. "") break ;;
  128. *) usage_is "[-p|P] [-s|-S] [-t port]"; return 2 ;;
  129. esac done
  130. filter $ping_fun | filter $tcp_fun | filter $ssh_fun
  131. }
  132. sort_paths_by_age() {
  133. #
  134. # Read paths on STDIN and sort them by age of youngest leaf
  135. #
  136. while read path;
  137. do echo "$(age_of_tree $path) $path"
  138. done \
  139. | sort -n \
  140. | cut -d\ -f 2-
  141. }