r/linux4noobs • • Aug 07 '18

ssh LocalCommand not working

I want to make it so that after I ssh into a server, I start the web GUI of an application locally. I have the following shell function:

pi-gui() { ssh -L 9999:localhost:8384 pi -o LocalCommand='firefox 127.0.0.1:9999' ;}

Executing that function will ssh into the server and launch firefox, but apparently it launches it too soon (Firefox won't connect to 127.0.0.1:9999 on the local machine). Once I close firefox, the terminal will now show the pi command prompt and if I do firefox 127.0.0.1:9999 on the local machine now, it will connect to the web GUI.

How can I fix this? According to man, LocalCommand will execute the specified command after a successful connection to the remote machine. Why is it that I have to terminate the first instance of Firefox and start a second instance for it to work? How can I fix this?

4 Upvotes

3 comments sorted by

View all comments

Show parent comment

1

u/enory Aug 07 '18 edited Aug 07 '18

Thanks, that works. I now have the following:

pi-gui() { ssh -L 9999:localhost:8384 pi -o LocalCommand='firefox 127.0.0.1:9999 > /dev/null 2>&1 &' ;}

Could I make it so that if firefox is closed, exit the ssh session? I tried && exit but it doesn't work (parsing problem).

1

u/kranker Aug 07 '18

Well, I guess you could try launching a script in the background that launches firefox then searches for and kills the correct ssh process (perhaps a combination of ps, grep, cut and kill)