#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    32
    Rep Power
    4

    Define from which server ssh connection is established


    Hello everyone!

    The situation is as follows:
    I've got three servers
    - mainserver.domain.com
    - testserver.domain.com
    - buildserver.domain.com

    mainserver and testserver sometimes SSH to buildserver as shareduser.

    I need to determine being on the buildserver from which server I am currently SSH'd. I am limited to do it from shell script running from shareduser home directory.

    All I am concerned about is what command I should use in this script to determine the server from which SSH session is established.

    Thanks,
    Paul
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    32
    Rep Power
    4
    OK, looks like I can check $SSH_CONNECTION var to determine the IP address from which I am connected.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    32
    Rep Power
    4
    Googled this one:
    $TTY stores current tty number then we use it to parse 'FROM' field of "w" output like this (not a script, just to give a general idea)
    Code:
    echo $TTY
    /dev/pts/77
    
    w | grep 'pts/77'
    USER       TTY      FROM                  LOGIN@ IDLE  JCPU  PCPU  WHAT
    shareduser pts/77   testserver.domain.com 09:26  2.00s 0.22s 0.10s w
    ---
    UPDATE:
    The disadvantage is that you can get the following:
    Code:
    w | grep 'pts/77'
    USER       TTY      FROM           LOGIN@ IDLE  JCPU  PCPU  WHAT
    shareduser pts/77   testserver.dom 09:26  2.00s 0.22s 0.10s w
    so that FQDN is trimmed to fit the 'FROM' column width
    Last edited by >>>RAM>>>; July 24th, 2013 at 09:05 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2010
    Posts
    32
    Rep Power
    4
    I ended up with this:
    Code:
    echo $SSH_CONNECTION | cut -f 4 -d : | cut -f 1 -d ' '
    where
    echo $SSH_CONNECTION gives me something like
    ::ffff:125.12.40.160 52592 ::ffff:125.12.110.16 22
    which means I am connected from 125.12.40.160 port 52592 to 125.12.110.16 port 22
    Then I cut 4th field formed by delimiter ":" and get
    125.12.40.160 52592
    The last cut helps to separate the ip address from port (setting delimiter to space)

    Both IP and FQDN are acceptable for me, so I stop my searches on this.

IMN logo majestic logo threadwatch logo seochat tools logo