Skip to content

Instantly share code, notes, and snippets.

@jeremyxu2010
Forked from a-sync/.bash_aliases
Created July 24, 2019 10:13
Show Gist options
  • Save jeremyxu2010/08e645a8ef21becc7298a8ed2e46c624 to your computer and use it in GitHub Desktop.
Save jeremyxu2010/08e645a8ef21becc7298a8ed2e46c624 to your computer and use it in GitHub Desktop.
SSH / SCP wrapper with password configuration support
alias ssh=ssh-wrapper
alias scp=scp-wrapper
# Allow specifying passwords for Host entries, to be parsed by ssh-wrapper.
IgnoreUnknown Password
Host my-server
HostName 10.20.30.40
Port 22
User myuser
Password mypassword
#!/bin/bash
password=$(awk '
BEGIN {
# Collect the SCP arguments as keys of a dictionary, so that we can easily
# check for inclusion.
for (i = 2; i < ARGC; i++) {
colonIdx = index(ARGV[i], ":")
if (colonIdx > 0) {
scpArgs[substr(ARGV[i], 1, colonIdx - 1)] = 1
}
}
# Only process the first argument; all others are the command-line arguments
# given to scp.
ARGC = 2
}
$1 == "Password" && inhost { print $2 }
/^\s*Host\s/ {
if ($2 in scpArgs)
inhost=1
else
inhost=0
}
' ~/.ssh/config "$@")
if [ "$password" ]; then
sshpass -p "$password" "$(which scp)" "$@"
else
"$(which scp)" "$@"
fi
#!/bin/bash
password=$(awk '
BEGIN {
# Collect the SSH arguments as keys of a dictionary, so that we can easily
# check for inclusion.
for (i = 2; i < ARGC; i++) {
sshArgs[ARGV[i]] = 1
}
# Only process the first argument; all others are the command-line arguments
# given to ssh.
ARGC = 2
}
$1 == "Password" && inhost { print $2 }
/^\s*Host\s/ {
if ($2 in sshArgs)
inhost=1
else
inhost=0
}
' ~/.ssh/config "$@")
if [ "$password" ]; then
sshpass -p "$password" "$(which ssh)" "$@"
else
"$(which ssh)" "$@"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment