Reputation: 100050
These two ssh options:
ssh -o StrictHostKeyChecking=no -o NumberOfPasswordPrompts=0
I am interested in setting those using env variables, is there a way to do that?
export StrictHostKeyChecking=no
export NumberOfPasswordPrompts=0
but that's of course not quite right
Upvotes: 4
Views: 4552
Reputation: 16138
The "proper" way to do this is within your ~/.ssh/config
file, where you can make those global for all connections or you can restrict it by host (or a few more advanced things). I'm going to assume you can't do that but that you do still have the ability to alter your ~/.bashrc
or whatever.
You can solve this by putting this in your ~/.bashrc
or ~/.zshrc
(or by running these lines before ssh
):
SSH_ARGS=( -o StrictHostKeyChecking=no -o NumberOfPasswordPrompts=0 )
alias ssh='ssh "${SSH_ARGS[@]}"'
If you want explicit environment variables like what you're proposing in your question (e.g. $StrictHostKeyChecking
) then you'll have to make a really ugly alias or function to convert them all to the final ssh
call. Here's a solution that should work in bash but won't work in POSIX shell:
ssh() {
local SSH_VAR SSH_VAR_VALUE
for SSH_VAR in StrictHostKeyChecking NumberOfPasswordPrompts; do # expand this
SSH_VAR_VALUE="${!SSH_VAR}" # bash indirect expansion
if [ -n "$SSH_VAR_VALUE" ]; then
set -- -o "$SSH_VAR=$SSH_VAR_VALUE" "$@"
fi
done
command ssh "$@"
}
The above example only supports the two SSH variables named in the question. That line will likely have to get really long as it must explicitly name every option before the semicolon on the line with the # expand this
comment.
This loops over every supported SSH variable and uses bash's indirect expansion to check whether it is in the environment (and not an empty value). If it is, a -o
and the option name and its value are prepended to the start of the argument list ($@
).
If you use zsh, you'll need zsh's parameter expansion flag P
, replacing bash's ${!SSH_VAR}
with zsh's ${(P)SSH_VAR}
. Other shells may need to replace that whole "bash indirect expansion" line with eval "SSH_VAR_VALUE=\"\${$SSH_VAR}\""
(watch your quoting there, or that line can be dangerous).
Invoking command ssh
prevents the recursion we'd get from calling ssh
from within a function of the same name. command ssh
ignores the function and actually runs the true ssh
command.
Upvotes: 3