2

I connect from machine A to machine B. In machine B, I'd like to print A's hostname in bash's command prompt.

I tried to get this using who -m and doing some cuting, put output differs between different unix'es.

I could not find any direct way to get this hostname.

I see two possibilites:

  1. Somehow passing it when connecting using ssh (I don't think I will ever use something else to connect remotely)
  2. Processing who's output in multiple ways and hoping there aren't too much versions around

Possibility (1) is preferred, because I can send the hostname I've assigned to a host and not my carrier's one, but I couldn't find any way to archive this in man ssh_config.

Jens Erat
  • 18,485
  • 14
  • 68
  • 80

1 Answers1

1

Accessing the IP address of the client is pretty easy. In my .profile, I have the following snippet:

PS1="\T [\h:\w]\n(from ${SSH_CLIENT%% *}) $ "

which produces a two line prompt that looks like this:

08:25:14 [fluorine:~]
(from 72.69.85.67) $ 

If getting the client hostname is really important to you, then you could use that IP address and do a reverse DNS lookup on the extracted IP address (assuming dig or something similar is installed on the server).

Or, as you suggest, you can send environment variables to the server assuming you have the ssh server configured to do so. You can refer to this answer for help in doing that.

Ryan
  • 221
  • 1
  • 3
  • 8