Small sh "framework" to test some server responses
sh
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.

ssh.sh 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #Copyright (C) 2016,2023 Weber Yann
  2. #
  3. #This program is free software; you can redistribute it and/or modify
  4. #it under the terms of the GNU General Public License as published by
  5. #the Free Software Foundation; either version 3 of the License, or
  6. #any later version.
  7. #
  8. #This program is distributed in the hope that it will be useful,
  9. #but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. #MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. #GNU General Public License for more details.
  12. #
  13. #You should have received a copy of the GNU General Public License
  14. #along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. #
  16. # SSH checks
  17. #
  18. check_ssh_nc() {
  19. host=$1
  20. port=$2
  21. if [ -z "$port" ]
  22. then
  23. port=22
  24. fi
  25. rep="$(nc -w1 "$host" "$port" </dev/null)"
  26. res=$?
  27. if [ "$res" -ne "0" ]
  28. then
  29. fail "Netcat unable to connect to $host:$port"
  30. return
  31. fi
  32. if echo "$rep" | grep "^SSH-2.0-OpenSSH" >/dev/null
  33. then
  34. success "OpenSSH replied on $host:$port"
  35. else
  36. fail "Bad reply from $host:$port : '$rep'"
  37. fi
  38. }
  39. check_ssh_key() {
  40. host="$1"
  41. testkey="$2"
  42. keytype="$3"
  43. port="$4"
  44. if [ -z "$port" ]
  45. then
  46. port=22
  47. fi
  48. if [ -z "$keytype" ]
  49. then
  50. keytype="rsa"
  51. fi
  52. key=$(ssh-keyscan -p $port -t "$keytype" "$host" 2>/dev/null | cut -d " " -f3)
  53. if [ -z "$key" ]
  54. then
  55. fail "SSH server not responding"
  56. return
  57. elif [ "$key" = "$testkey" ]
  58. then
  59. success "OpenSSH $host:$port key is $testkey"
  60. return
  61. else
  62. fail "OpenSSH $host:$port missmatch : "
  63. logdate ERR "Expected : $testkey" 1
  64. logdate ERR "Received : $key" 1
  65. return
  66. fi
  67. }