3

I have a script that I use to automatically generate port forwards for SSH, since I am working in several terminal windows across different servers all day, and this checks whether or not the port has been mapped on my end, and if so bumps it with +1 until it finds a port that is available. Then it echos out the port forward info and just runs ssh -L<port1>:localhost:<port2> <server>. Simple.

When I have a lot of output and can't remember what the ports were mapped to originally, I have to scroll all the way up (hard work) to find this. This made me think that there might be some sort of wrapper of some sort that makes it possible to just add a single or multiple lines of text on top, and then the ssh session beneath it? I took the liberty to draw up a basic sketch:

Sketch

I have tried Googling this, but I am struggling to find anything. If this exists, the command might be something like this: wrapper --text="Port forwards: L5901:localhost:5900, L8080:localhost:80" --command="ssh -L<port1>:localhost:<port2> <server>"

Maybe using screen for this is a solution as well, but I would want to be able to run the "whole thing" (adding the line of text + starting the SSH session beneath it) with one command.

  • You can print the output of your command to tmux's status bar (have it set in your config and it will run when you open it)... – jasonwryan Apr 1 '15 at 19:45
  • @jasonwryan but is is possible to set the status bar text when initiating tmux from the command line? I can't seem to find a way to do that. – carestad Apr 1 '15 at 19:51
  • I don't follow: you set it in the config file, and then (re)start tmux. If your script is called ssh-info you add this to .tmux.conf: set -g status-right "#[fg=red] #(sshinfo)" – jasonwryan Apr 1 '15 at 19:53
  • @jasonwryan Right, I see, but that means it needs to run a pre-defined command every time I start tmux. It isn't necessarily the solution I am looking for, but if you have a good code example on how to make this work then I am happy to try :) – carestad Apr 1 '15 at 20:05
  • You could put the script in an if...then block so that it only prints to the status bar if it is active? – jasonwryan Apr 1 '15 at 20:23
0

I would just set the Gnome titlebar, that shows whatever you set at the top of the window.

To prevent bash from changing the title use:

PROMPT_COMMAND=''
PS1='$ '

(either PROMPT_COMMAND or PS1 might try and change the title bar), and then set the title with:

echo -ne "\033]0;new title with port number found\007"

Make sure your PS1 on the server is not setting the title either. For that you can test in your ~/.bashrc:

if [ ! -z "$SSH_CLIENT" ] ; then
   PS1='$ '
fi

(the above was necessary on a server running Ubuntu 12.04).

And run your ssh command:

ssh -t user@server "PROMPT_COMMAND='' bash -l"

Your PS1 and PROMPT_COMMAND can be more complex, as long as they don't try and set the title bar via some escape sequence (using \033]0; or \e]0;)

Any solutions involving tmux, screen and any curses based reservation of a line of the screen disable the appropriate scrolling with the Gnome terminal scrollbar.

  • Thanks for the reply! That sounds like a pretty good idea! But I am struggling to make it work. Just unsetting and echoing that line in the terminal doesn't work, but it does if I uncomment my existing PROMPT_COMMAND in my .bashrc file. Is it not possible to do "realtime"? – carestad Apr 2 '15 at 11:02
  • @carestad I updated the answer with something that hopefully works on your system. I have read somewhere it doesn't always do so. – Anthon Apr 2 '15 at 11:38

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.