Quantcast
Viewing all articles
Browse latest Browse all 16

Answer by Ingo Karkat for Can you set passwords in .ssh/config to allow automatic login?

Here's my elaborate variation on @ArekBurdach's answer. It offers the following extensions:

  • the host can be anywhere in the ssh command-line; i.e. it also supports the ssh <args> <host> <commands> syntax
  • does not hard-code the path to ssh
  • more robust parsing of ssh_config
  • Bonus: wrapper for scp, too

ssh-wrapper

#!/bin/bashpassword=$(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" ssh "$@"else    exec ssh "$@"fi

scp-wrapper

#!/bin/bashpassword=$(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" scp "$@"else    exec scp "$@"fi

Installation

Define aliases in your ~/.bashrc:

alias ssh=ssh-wrapperalias scp=scp-wrapper

Configuration

With the IgnoreUnknown directive, ssh does not complain about a newly introduced Password directive, so (in contrast to @ArekBurdach's answer), we can make this appear as a "real" configuration. If you don't like this, it's trivial to change the script back to the commented-out one.

# Allow specifying passwords for Host entries, to be parsed by ssh-wrapper.IgnoreUnknown PasswordHost foohost    User baruser    Password foobarpassword

Viewing all articles
Browse latest Browse all 16

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>