Guest User

Untitled

a guest
Apr 26th, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ################################################################
  4. ## @usage telnet-wrapper.sh "uptime"
  5. ## @usage telnet-wrapper.sh "ping -c1 10.0.0.2"
  6. ## @description Install Alpine packages and run telnet command
  7. ## @license MIT
  8. ## @author Dale Higgs <@dale3h>
  9. ################################################################
  10.  
  11. # Set these as needed
  12. host="10.0.0.1"
  13. username="Admin"
  14. password="Admin"
  15. cmd="$1"
  16. logout="logout"
  17.  
  18. # You shouldn't need to edit anything below this line
  19. if [[ -z "$cmd" ]]; then
  20. cmd="uptime"
  21. fi
  22.  
  23. apk=$(command -v apk) || { echo "missing apk"; exit 0; }
  24. expect=$(command -v expect)
  25. telnet=$(command -v telnet)
  26.  
  27. if [[ -z "$expect" ]]; then
  28. $apk add --quiet --no-cache expect > /dev/null 2>&1
  29. expect=$(command -v expect)
  30. [[ -n "$expect" ]] || { echo "missing expect"; exit 0; }
  31. fi
  32.  
  33. if [[ -z "$telnet" ]]; then
  34. $apk add --quiet --no-cache busybox-extras > /dev/null 2>&1
  35. telnet=$(command -v telnet)
  36. [[ -n "$telnet" ]] || { echo "missing telnet"; exit 0; }
  37. fi
  38.  
  39. result=$(
  40. /usr/bin/expect <<EOD
  41. set timeout 1
  42. log_user 0
  43. spawn -noecho telnet ${host}
  44. expect -re "login:\r"
  45. send "${username}\r"
  46. expect -re "Password:\r"
  47. send "${password}\r"
  48. expect -re "#"
  49. log_user 1
  50. send "echo START_OUTPUT; ${cmd}; echo END_OUTPUT\r"
  51. set timeout 10
  52. expect -re "#"
  53. send "${logout}\r"
  54. expect eof
  55. EOD
  56. )
  57.  
  58. echo "$result" | sed ':a;N;$!ba;s/.*START_OUTPUT\s\+\(.*\)\s\+END_OUTPUT.*/\1/g'
Add Comment
Please, Sign In to add comment