Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- ROOT_UID=0
- LFTP_DIR=/usr/local/etc/lftp
- alias=
- user=
- password=
- protocol=
- dir=
- usage() {
- cat <<EOF
- $0 [OPTIONS]
- Connect to genero known host with ssh.
- Options:
- -n|--new Create a new host stored genero-wide
- -A|--add Add a host to your local configs
- -l|--lftp Connect to a host through lftp
- -s|--ssh Connect to a host through ssh
- -H|--host Host name (-n)
- -a|--alias Host alias (-n)
- -u|--user Host username (-n)
- -p|--password Host password (-n)
- -P|--protocol Host protocol (sftp/ftp) (-n)
- -d|--dir Host root directory (-n)
- -i|--interactive Interactive prompt (-n)
- -h|--help Display this help and exit
- EOF
- }
- die() {
- printf '%s\n' "$@" >&2
- exit 1
- }
- confirm() {
- echo
- read -p "$1 [y/n] " -n 1
- [[ $REPLY =~ ^[Yy]$ ]]
- }
- get_pass() {
- if [[ "$1" =~ ^[[:alpha:]]+$ ]]; then
- local bookmark_file="${LFTP_DIR}/bookmarks"
- local password=$(cat $bookmark_file | grep "^$1" | awk 'BEGIN { FS = "[:@]" };{ print $3; }')
- if [[ $password ]]; then
- echo $password
- else
- die "Couldn't find host $1"
- fi
- fi
- }
- connect_ssh() {
- local host=$1
- local tmp=$(mktemp)
- echo -n "$(get_pass $host)" > $tmp
- if [[ -f $tmp ]]; then
- (sleep 1; rm -f $tmp) &
- sshpass -f $tmp ssh "$host"
- else
- die "Something went wrong, can't read temporary pass file"
- fi
- }
- connect_lftp() {
- local host=$1
- local old_lftp=$LFTP_HOME
- LFTP_HOME=$LFTP_DIR
- (sleep 1; LFTP_HOME=$old_lftp) &
- lftp $host }
- interactive() {
- local values=(alias user password protocol host dir)
- local desc=
- for val in ${values[@]}; do
- desc=$(usage | awk "BEGIN { ORS=\" \"; } /\|--$val/ { for(i=2;i<=NF;i++) print \$i; }")
- echo -n "$desc: "
- eval "read $val"
- done
- }
- new_host() {
- if [[ $alias && $user && $password && $host ]]; then
- protocol=${protocol:-ftp}
- string="${alias}\t${protocol}://${user}:${password}@${host}/${dir}"
- bookmarks="${LFTP_DIR}/bookmarks"
- if [ -f $bookmarks ]; then
- echo "$string" >> ${LFTP_DIR}/bookmarks
- else
- die "Couldn't find bookmarks file."
- fi
- else
- die "Missing arguments. Check --help"
- fi
- }
- add_host() {
- [[ ! $1 ]] && die "No alias specified."
- local bookmark_file="${LFTP_DIR}/bookmarks"
- local ssh_config="/home/${SUDO_USER}/.ssh/config"
- local line=$(cat $bookmark_file | grep "^$1" | perl -p -e "s/^($1)\s+(\w+):\/\/([^:]+):([^@]+)@([^\/]+)(.*)\w*$/\$1:\$2:\$3:\$4:\$5:\$6/")
- [[ ! $line ]] && die "Couldnt find $1"
- IFS=: read -r alias protocol user password host dir <<< "$line"
- if ( ! grep "^Host $alias$" $ssh_config >/dev/null 2>&1 ); then
- echo "Host $alias
- HostName $host
- User $user
- " >> $ssh_config
- echo "Added $alias to $ssh_config"
- else
- die "$alias already exists in your $ssh_config"
- fi
- }
- if [ "$UID" -ne "$ROOT_UID" ]; then
- die "Must be root to run this script."
- fi
- # Iterate over arguments and set stuff
- while [[ $1 = -?* ]]; do
- case $1 in
- -i|--interactive) new=1; interactive; break ;;
- -n|--new) new=1 ;;
- -A|--add) shift; add=1 alias=$1 ;;
- -l|--lftp) shift; connect_lftp "$1" ;;
- -s|--ssh) shift; connect_ssh "$1" ;;
- -H|--host) shift; host=$1 ;;
- -a|--alias) shift; alias=$1 ;;
- -u|--user) shift; user=$1 ;;
- -p|--password) shift; password=$1 ;;
- -P|--protocol) shift; protocol=$1 ;;
- -d|--dir) shift; dir=$1 ;;
- -h|--help) usage &>2; exit ;;
- *) die "Invalid option: $1" ;;
- esac
- shift
- done
- if [[ $new ]]; then
- new_host
- elif [[ $add ]]; then
- add_host "$alias"
- fi
Add Comment
Please, Sign In to add comment