3

I am trying to find a way to dynamically set bash environment settings depending on the environment I am logging in from.

For example:

  • Opening a Terminal session from the xfce GUI, it would load a "default" (lack of a better word) .bash_profile with my basic environment settings

  • Connecting via SSH from another FreeBSD (or Linux) machine would also use that "default" .bash_profile

  • Connecting via SSH from my Mac would add additional settings (i.e. iTerm2 shell integration, for example)

I thought of testing for the environment variable SSH_CLIENT but that would change depending on what DHCP address my machine was I ssh'ing from got that day.

Is there a way to determine what the remote machine's platform is and then process .bash_profile accordingly?

If I can't "self contain" it within .bash_profile, is there something I can "send" that will enabled me to do this?

1

Is there a way to determine what the remote machine's platform is and then process .bash_profile accordingly?

Not simply, no. You could, if network conditions allow, use nmap to attempt to identify the OS of the originating IP in $SSH_CLIENT, but that's not necessarily reliable.

If your source systems were on different network segments, you could use that as a rough indicator to distinguish the source systems, but that's not the case for you here.

Since you just want the Mac's connection to do something different, one option would be to wrap or alias the Mac's ssh connection to the target such that it passes a custom environment variable for bash to check:

alias ssh2it='ssh -t targethost "_origin=mac bash -il"'

Then set up your targethost's ~/.bashrc along these lines:

case $_origin in
(mac)
        echo mac-specific settings here
        ;;
(other-custom-system)
        echo other settings here
        ;;
(*)
        echo any default settings here
        ;;
esac

You'd have to extend that idea to each other type of system that you want to behave differently, and it means wrapping every interactive login in order to set the variable.

If you can further control the target system's sshd_config, then you could edit /etc/ssh/sshd_config to add:

AcceptEnv _origin

(and then recycle sshd).

Then on the originating system, edit ~/.ssh/config to send that variable:

Host targetsystem
  SendEnv _origin

and before you connect to the target, run:

export _origin=mac

Then, after you log in to the target system, $_origin will be set to that value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.