Is there a way to get the user's SSH client IP address in a bash script?
From the manual of OpenSSH SSH client (man 1 ssh
):
ENVIRONMENT
ssh
will normally set the following environment variables:[…]
SSH_CONNECTION
Identifies the client and server ends of the connection. The variable contains four space-separated values: client IP address, client port number, server IP address, and server port number.[…]
In .bashrc
you should do like:
case "${SSH_CONNECTION%% *}" in
1.2.3.4|1.2.3.5 )
PS1=…
;;
2.2.7.* )
PS1=…
;;
# add support for more users if needed
esac
or like
if [ "${SSH_CONNECTION%% *}" = "1.2.3.4" ]; then
PS1=…
else
PS1=…
fi