0

I'm trying to get basic informations on my local machine from ssh connections. The purpose here is to get the current user, hostname, current working directory in my tmux status bar.

I've enabled ControlMaster in my ~/.ssh/config:

Host *
  ControlMaster auto
  ControlPath ~/.ssh/socket-%r@%h:%p

Now, I've managed to get the ControlPath from a homemade tmux plugin and I can send commands through the socket:

echo $(ssh -S ~/.ssh/socket-$user@$host:$port $host "$command")

This is working fine to get the remote hostname and the user. The issue is that when I change my remote user, whoami is still returning the user that was used to log in.

For example with whoami (~ is local, > is executed on the ssh session):

~ ssh soyuka@host
> whoami
  soyuka
~ ssh -S ~/.ssh/socket-soyuka@host:22 host whoami
  soyuka

> sudo -s
> whoami
  root

~ ssh -S ~/.ssh/socket-soyuka@host:22 host whoami
  soyuka #Here I want to get root because the remote user is now root

For example with pwd (~ is local, > is executed on the ssh session):

~ ssh soyuka@host
~ ssh -S ~/.ssh/socket-soyuka@host:22 host pwd
  /home/soyuka

> cd downloads
> pwd
  /home/soyuka/downloads

~ ssh -S ~/.ssh/socket-soyuka@host:22 host pwd
  /home/soyuka #Here I want to get /home/soyuka/downloads

Is this possible somehow to execute the command on the current ssh session by it's socket?

  • Can you clarify your question, please? (1) you say something about whoami but it's not clear why "returning the user that was used to log in" is not what you want and what it is that you expect instead; (2) I don't see the problem with your pwd-themed command transcript (which seems to have nothing to do with whoami) — are you expecting that changing the local pwd would also somehow change your remote default/home directory? I don't follow. – Celada May 23 '15 at 13:53
  • Improved, hope it'll be clearer – soyuka May 23 '15 at 14:01
3

What you're asking for does not seem to make sense.

Here I want to get root because the remote user is now root

No, the remote user is now root. The fact that you've done sudo -s on the local system doesn't change that you're ssh'ing into the soyuka account remotely.

The answer for "Here I want to get /home/soyuka/downloads" is similar.

Is this possible somehow to execute the command on the current ssh session by it's socket?

I believe that you may have a misconception that there is any persistent long-lived SSH session. In fact, every time you do:

ssh -l remoteuser host some-command

...a new SSH session is opened, some-command is executed in that session (without a pty, unlike SSH sessions opened without a remote command specified), and then that session is closed.

Even if there were such a persistent remote session, there is no reason to expect that doing things locally such as sudo or cd would affect that remote session in any way.

The fact that you are using SSH control sockets does not change this at all. The only thing that is different with a slave SSH session compared to a normal one is that the SSH session is piggybacked on an existing SSH connection instead of a brand new connection.

  • Thanks, I indeed misunderstood the point of ssh sockets. Is there any way to send a command to the opened session? Are ssh tunnels also using new sessions? – soyuka May 23 '15 at 14:17
  • There is not really a way to do that except by managing the remote session with something like expect which will let you write a script that spawns the remote session, sends commands to it, and expects responses. – Celada May 23 '15 at 14:22
  • Hmm ok but what I don't get is that printf "\033ktest\033\\" will change my window name test. I'm therefore able to communicate between my ssh session and my terminal. It'd require a modified alias for ssh. Can't I use something like this to get my current working directory back to tmux? – soyuka May 23 '15 at 14:36
  • That's a terminal control sequence that changes the title of your terminal, yes. It has nothing to do with the shell or anything else that is running locally in your terminal. "Can't I use something like this to get my current working directory back to tmux?" — no, terminal output is only sent to the terminal itself. – Celada May 23 '15 at 15:50

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.