这是我对@ArekBurdach答案的详尽阐述。它提供以下扩展:
- 所述主机可以是在任何地方的
ssh
命令行; 即它也支持ssh <args> <host> <commands>
语法
- 不会硬编码到的路径
ssh
- 更强大的解析
ssh_config
- 奖励:包装器
scp
,太
ssh-wrapper
#!/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
scp-wrapper
#!/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
安装
在您的中定义别名~/.bashrc
:
alias ssh=ssh-wrapper
alias scp=scp-wrapper
组态
使用该IgnoreUnknown
指令时,ssh
不会抱怨新引入的Password
指令,因此(与@ArekBurdach的答案相反),我们可以使该指令显示为“真实”配置。如果您不喜欢这样,将脚本改回注释掉的脚本很简单。
# Allow specifying passwords for Host entries, to be parsed by ssh-wrapper.
IgnoreUnknown Password
Host foohost
User baruser
Password foobarpassword