Skip to content

Instantly share code, notes, and snippets.

@nguyensinhtu
Created February 21, 2021 07:59
Show Gist options
  • Save nguyensinhtu/1d134a06a8a90ec7fbeedf1ddcc8d1ab to your computer and use it in GitHub Desktop.
Save nguyensinhtu/1d134a06a8a90ec7fbeedf1ddcc8d1ab to your computer and use it in GitHub Desktop.

Revisions

  1. nguyensinhtu created this gist Feb 21, 2021.
    31 changes: 31 additions & 0 deletions my_fk_bash.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    # find ssh host by name
    # your ssh's config will be
    # Host your_host
    # Hostname your_ip
    # User your_username

    _find_host() {
    local your_host=$1

    host="$( awk -v your_host=$your_host '
    $1 == "Host" {
    host = $2
    next;
    }
    $1 == "Hostname" {
    host_name = $2;
    next;
    }
    $1 == "User" {
    $1 = "";
    sub( /^[[:space:]]*/, "" );
    if (host == your_host)
    printf "%s@%s \n", $0, host_name;
    }
    ' ~/.ssh/config )"

    echo $host
    }

    _find_host your_host