#!/bin/sh PASS_PREFIX="tc/ssh/" host="$1" # Open an "anonymous" fifo in fd 3 pipe="$(mktemp -u)" # get random path mkfifo "$pipe" # create a fifo there exec 3<> "$pipe" # open fifo rw at fd 3 rm "$pipe" # remove file echo -n "Trying to get password from 'pass'... " if pass show "${PASS_PREFIX}$host" 2> /dev/null >&3; then echo "FOUND ('${PASS_PREFIX}$host'). Connecting..." exec sshpass -d3 ssh -o StrictHostKeyChecking=accept-new $@ else echo "not found. Trying pubkeys..." ssh -o NumberOfPasswordPrompts=0 -o StrictHostKeyChecking=accept-new $@ ok=$? if [ "$ok" -ne 0 ]; then read -rp "Connection via pubkey failed, password? " passphrase echo "$passphrase" >&3 echo "Connecting with passphrase..." sshpass -d3 ssh -o StrictHostKeyChecking=accept-new $@ ok=$? if [ "$ok" -eq 0 ]; then echo "It worked! Storing password..." echo "$passphrase" | pass insert -e "${PASS_PREFIX}$host" else echo "Unable to connect with password either; giving up." fi fi fi