Guest User

Untitled

a guest
Nov 21st, 2018
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.50 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. ROOT_UID=0
  4.  
  5. LFTP_DIR=/usr/local/etc/lftp
  6.  
  7. alias=
  8. user=
  9. password=
  10. protocol=
  11. dir=
  12.  
  13. usage() {
  14. cat <<EOF
  15. $0 [OPTIONS]
  16.  
  17. Connect to genero known host with ssh.
  18.  
  19. Options:
  20. -n|--new Create a new host stored genero-wide
  21. -A|--add Add a host to your local configs
  22. -l|--lftp Connect to a host through lftp
  23. -s|--ssh Connect to a host through ssh
  24. -H|--host Host name (-n)
  25. -a|--alias Host alias (-n)
  26. -u|--user Host username (-n)
  27. -p|--password Host password (-n)
  28. -P|--protocol Host protocol (sftp/ftp) (-n)
  29. -d|--dir Host root directory (-n)
  30. -i|--interactive Interactive prompt (-n)
  31. -h|--help Display this help and exit
  32.  
  33. EOF
  34. }
  35.  
  36. die() {
  37. printf '%s\n' "$@" >&2
  38. exit 1
  39. }
  40.  
  41. confirm() {
  42. echo
  43. read -p "$1 [y/n] " -n 1
  44. [[ $REPLY =~ ^[Yy]$ ]]
  45. }
  46.  
  47. get_pass() {
  48. if [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
  49. local bookmark_file="${LFTP_DIR}/bookmarks"
  50. local password=$(cat $bookmark_file | grep "^$1" | awk 'BEGIN { FS = "[:@]" };{ print $3; }')
  51. if [[ $password ]]; then
  52. echo $password
  53. else
  54. die "Couldn't find host $1"
  55. fi
  56. fi
  57. }
  58.  
  59. connect_ssh() {
  60. local host=$1
  61. local tmp=$(mktemp)
  62. echo -n "$(get_pass $host)" > $tmp
  63. if [[ -f $tmp ]]; then
  64. (sleep 1; rm -f $tmp) &
  65. sshpass -f $tmp ssh "$host"
  66. else
  67. die "Something went wrong, can't read temporary pass file"
  68. fi
  69. }
  70. connect_lftp() {
  71. local host=$1
  72. local old_lftp=$LFTP_HOME
  73. LFTP_HOME=$LFTP_DIR
  74. (sleep 1; LFTP_HOME=$old_lftp) &
  75. lftp $host }
  76.  
  77. interactive() {
  78. local values=(alias user password protocol host dir)
  79. local desc=
  80. for val in ${values[@]}; do
  81. desc=$(usage | awk "BEGIN { ORS=\" \"; } /\|--$val/ { for(i=2;i<=NF;i++) print \$i; }")
  82. echo -n "$desc: "
  83. eval "read $val"
  84. done
  85. }
  86.  
  87. new_host() {
  88. if [[ $alias && $user && $password && $host ]]; then
  89. protocol=${protocol:-ftp}
  90. string="${alias}\t${protocol}://${user}:${password}@${host}/${dir}"
  91. bookmarks="${LFTP_DIR}/bookmarks"
  92. if [ -f $bookmarks ]; then
  93. echo "$string" >> ${LFTP_DIR}/bookmarks
  94. else
  95. die "Couldn't find bookmarks file."
  96. fi
  97. else
  98. die "Missing arguments. Check --help"
  99. fi
  100. }
  101.  
  102. add_host() {
  103. [[ ! $1 ]] && die "No alias specified."
  104. local bookmark_file="${LFTP_DIR}/bookmarks"
  105. local ssh_config="/home/${SUDO_USER}/.ssh/config"
  106. local line=$(cat $bookmark_file | grep "^$1" | perl -p -e "s/^($1)\s+(\w+):\/\/([^:]+):([^@]+)@([^\/]+)(.*)\w*$/\$1:\$2:\$3:\$4:\$5:\$6/")
  107. [[ ! $line ]] && die "Couldnt find $1"
  108. IFS=: read -r alias protocol user password host dir <<< "$line"
  109. if ( ! grep "^Host $alias$" $ssh_config >/dev/null 2>&1 ); then
  110. echo "Host $alias
  111. HostName $host
  112. User $user
  113. " >> $ssh_config
  114. echo "Added $alias to $ssh_config"
  115. else
  116. die "$alias already exists in your $ssh_config"
  117. fi
  118. }
  119.  
  120. if [ "$UID" -ne "$ROOT_UID" ]; then
  121. die "Must be root to run this script."
  122. fi
  123.  
  124. # Iterate over arguments and set stuff
  125. while [[ $1 = -?* ]]; do
  126. case $1 in
  127. -i|--interactive) new=1; interactive; break ;;
  128. -n|--new) new=1 ;;
  129. -A|--add) shift; add=1 alias=$1 ;;
  130. -l|--lftp) shift; connect_lftp "$1" ;;
  131. -s|--ssh) shift; connect_ssh "$1" ;;
  132. -H|--host) shift; host=$1 ;;
  133. -a|--alias) shift; alias=$1 ;;
  134. -u|--user) shift; user=$1 ;;
  135. -p|--password) shift; password=$1 ;;
  136. -P|--protocol) shift; protocol=$1 ;;
  137. -d|--dir) shift; dir=$1 ;;
  138. -h|--help) usage &>2; exit ;;
  139. *) die "Invalid option: $1" ;;
  140. esac
  141.  
  142. shift
  143. done
  144.  
  145. if [[ $new ]]; then
  146. new_host
  147. elif [[ $add ]]; then
  148. add_host "$alias"
  149. fi
Add Comment
Please, Sign In to add comment