-
Notifications
You must be signed in to change notification settings - Fork 0
/
autosshpass
executable file
·48 lines (44 loc) · 1.22 KB
/
autosshpass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env bash
# e.g. SSHPASS environment variable
# export SSHPASS=xxx
#
# e.g. ~/.ssh/config
# Host xxx
# HostName xxx.com
# User xxx
# SendEnv SSHPASS xxx
function main() {
local cmd_name=$(basename $0)
# NOTE: guess real cmd name
local cmd=${AUTOSSHPASS_CMD:-$(printf '%s' "$cmd_name" | grep -o 'auto.*pass' | sed -E 's/^auto(.*)pass$/\1/g')}
if [[ -z $cmd_name ]]; then
[[ $cmd_name =~ ssh ]] && cmd='ssh'
[[ $cmd_name =~ scp ]] && cmd='scp'
[[ $cmd_name =~ rsync ]] && cmd='rsync'
fi
if ! type >/dev/null 2>&1 sshpass; then
$cmd "$@"
return
fi
[[ -z $cmd ]] && echo 1>&2 "Unknown corresponding command to '$cmd_name'!" && return 1
local host
for arg in "$@"; do
[[ $arg =~ ^-.* ]] && continue
if [[ $cmd =~ ssh ]]; then
local host=$arg
break
fi
# NOTE: for scp or rsync
[[ $arg =~ .*:.* ]] && local host=${arg%:*}
done
# NOTE: uncomment for localhost to localhost
# [[ -z $host ]] && echo 1>&2 "Unknown host name" && return 1
local login_password=$(ssh -G "$host" 2>/dev/null | grep -A1 '^sendenv SSHPASS$' | sed -n 2,2p | cut -d' ' -f2-)
[[ -n $login_password ]] && export SSHPASS=$login_password
if [[ -n $SSHPASS ]]; then
sshpass -e "$cmd" "$@"
else
$cmd "$@"
fi
}
main "$@"