Finding IP or hostname of origin machine (ssh)

I have never tried, but I can think of something that may work: You do not let SSH start your login shell, but take matters in your own hands. You can tell SSH to run arbitrary commands. Instead of

ssh remote_host

you would run something along the lines of

ssh remote_host -t "
    . /etc/profile; 
    . /etc/bash.bashrc; 
    DAT_ORIGIN_HOST=$(ifconfig eth0|grep -Po 't addr:\K[\d.]+') /bin/bash -l -i"

What this does is, it gives SSH something else to do instead of launching a login shell. That something is a string, which will be run as command, remotely. We use this to launch a shell in a very special manner: we give it an environmental variable DAT_ORIGIN_HOST, which contains our ip on eth0 (you may need to change that).

The trick we perform is that we put the command to execute remotely in double qoutes ". The double quotes (at least in Bash) mean, that BEFORE THE STRING IS PASSED TO SSH, our shell scans it and performs expansions/replacements, where appropriate. This means our shell will evaluate the `$(ifconfig ...) part, to our current ip address and pass ssh a string which contains the definition for an environmental variable with our local ip address.

Once logged in to the remote machine, echo $DAT_ORIGIN_HOST should print your IP address.

To develop that call to SSH I shamelessly took from here for extracting the IP address and here for the -t and how to launch something interactive

Disclaimer: I am unsure about the -l and -i option to /bin/bash. Maybe you need to omit them.


Id I understood correctly you are connection directly from origin machine to destination with proxycommand hops. And so here are three hacks for you (third added after comment on usage scenario). First) Use remote port forwarding to allow you to get back to you origin machine with -R remotemachineport:dstonlocalnet:dstportonlocalnet

ssh -R 2222:localhost:22 user@target
# on "target" you can now do this to get back to origin host
ssh user2@localhost -p 2222 

Second) Abuse AcceptEnv LC_* on target. It's not nice but it's quite common to allow LOCALE variables even whan AcceptUserEnviconment is not available. So now you can do:

export LC_SOURCEHOST=my.ip.addr.or:something
ssh user@target
# on tathet:
echo $LC_SOURCEHOST

Third) Use ssh remote forward to identify host or host type.

# Here is an example what you can use on target machine. 
# This will modify your PS1 variable (prompt) based on source host (port forwarding)
# Add this to .bash_profile

function checkHost {
  RET=""
  ## loop over test port numbers 17891-17895 in my example
  for x in $(seq 1 5); do 
    # if netstat is not available test port some other way like with nc or something
    ## && RET= will set RET = 1-5
    /bin/netstat -lnt|/bin/grep -q 127.0.0.1:1789$x && RET=$x;
  done
  # return RET
  # please note that if you have multiple open connections with different port numbers
  # this method cannot not distinguish between them 
  echo $RET
}

# get 1-5 number from function above
VAL=$(checkHost)
# do something like set PS1 var or map vim file or something
export PS1='\[\033k\033\\\]\u@\h: \w\$ '$VAL

Now connect with port forwarding:

### this will still enable ssh forwarding back. Change 22 to something else like 
### 24 to disable it (if nothing is listening on your source machines 24 port)
ssh -R 17891:localhost:22 user@target

You can type who -m on the remote end to get information on the currently logged in user (including the hostname). who gives you information on logged in users, and the -m switch shows you "only hostname and user associated with stdin".