Guest User

Untitled

a guest
Jan 16th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. gcp () {
  2. local inp1 inp2
  3. inp1=$1
  4. inp2=$2
  5. DEFAULT_USER=SOME_USER
  6. if (( ! $# ))
  7. then
  8. inp1="-h"
  9. fi
  10. if [[ $inp1 == "-h" || $inp1 == "--help" ]]
  11. then
  12. echo "$0 - google compute connect\n"
  13. echo "usage: $0 -h"
  14. echo "usage: $0 -l"
  15. echo "usage: $0 -c default\n\nOptions:"
  16. echo "\t-h, --help\t\t\t display help message and exit"
  17. echo "\t-l, --list\t\t\t list instances"
  18. echo "\t-c, --connect=user@name\t\t ssh to instance"
  19. return 1
  20. elif [[ $inp1 == "-l" || $inp1 == "--list" ]]
  21. then
  22. VM_USER=""
  23. VM_INSTANCE="${inp2#*@}"
  24. FILTER=""
  25. if [[ $inp2 != "" ]]
  26. then
  27. read -r -d '' FILTER <<EOF
  28. --filter="name=${VM_INSTANCE}"
  29. EOF
  30. VM_USER="${inp2%@*}"
  31. VM_INSTANCE="${inp2#*@}"
  32. fi
  33. eval gcloud compute instances list $FILTER
  34. elif [[ ( $inp1 == "-c" || $inp1 == "--connect" ) && $inp2 != "" ]]
  35. then
  36. VM_USER="${inp2%@*}"
  37. VM_INSTANCE="${inp2#*@}"
  38. FILTER=""
  39. read -r -d '' FILTER <<EOF
  40. --filter="name=${VM_INSTANCE}"
  41. EOF
  42. OUTPUT=$(eval gcloud compute instances list $FILTER)
  43. if [[ $(echo $OUTPUT|grep -c '.') > 1 ]]
  44. then
  45. IP=$(echo $OUTPUT | tail -n 1 | awk '{print $5}')
  46. echo $3
  47. echo "Connecting to $VM_INSTANCE ($IP)..."
  48. sleep 1
  49. if [[ $VM_USER != $VM_INSTANCE ]]
  50. then
  51. ssh ${VM_USER}@${IP} || gcloud compute ssh ${VM_USER}@${VM_INSTANCE}
  52. else
  53. ssh weatherbell@${IP} || gcloud compute ssh ${DEFAULT_USER}@${VM_INSTANCE}
  54. fi
  55. fi
  56. else
  57. echo "idk wtf"
  58. fi
  59. }
  60. gcp is /usr/local/bin/gcp
Add Comment
Please, Sign In to add comment