Skip to content

Instantly share code, notes, and snippets.

@normanr
Last active April 30, 2023 20:36
Show Gist options
  • Save normanr/fdb8b73b536a0b4857016ee3408bbf29 to your computer and use it in GitHub Desktop.
Save normanr/fdb8b73b536a0b4857016ee3408bbf29 to your computer and use it in GitHub Desktop.
read sftp password from .netrc
#!/bin/bash
# Usage: SSH_ASKPASS_REQUIRE=force SSH_ASKPASS=netrc-askpass sftp ...
if [[ $1 =~ ([[:graph:]]+)@([[:graph:]]+)\'s\ password: ]]; then
USER="${BASH_REMATCH[1]}"
HOST="${BASH_REMATCH[2]}"
state=token
while read -a WORDS; do
for WORD in ${WORDS[@]}; do
case $state in
token)
case "${WORD}" in
machine)
state=machine
;;
login)
state=login
;;
password)
state=password
;;
esac
;;
machine)
machine="${WORD}"
state=token
;;
login)
login="${WORD}"
state=token
;;
password)
password="${WORD}"
state=token
if [[ "$machine" == "$HOST" ]] && [[ "$login" == "$USER" ]]; then
echo "$password"
exit 0
fi
;;
esac
done
done < ~/.netrc
fi
exit 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment