Skip to content

Instantly share code, notes, and snippets.

@a-sync
Last active November 5, 2019 07:44
Show Gist options
  • Save a-sync/8b242622c827cc39e8671e782f55b3d4 to your computer and use it in GitHub Desktop.
Save a-sync/8b242622c827cc39e8671e782f55b3d4 to your computer and use it in GitHub Desktop.

Revisions

  1. a-sync revised this gist Jan 26, 2018. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion .ssh\config
    Original file line number Diff line number Diff line change
    @@ -6,4 +6,3 @@ Host my-server
    Port 22
    User myuser
    Password mypassword

  2. a-sync created this gist Jan 25, 2018.
    2 changes: 2 additions & 0 deletions .bash_aliases
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,2 @@
    alias ssh=ssh-wrapper
    alias scp=scp-wrapper
    9 changes: 9 additions & 0 deletions .ssh\config
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,9 @@
    # 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

    32 changes: 32 additions & 0 deletions scp-wrapper
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    #!/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
    29 changes: 29 additions & 0 deletions ssh-wrapper
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    #!/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