8 Replies Latest reply: Dec 14, 2012 9:58 PM by BobHarris
mtharpe Level 1 Level 1 (0 points)

I am trying to change the termnal tabs to show the remote hostame that I am connecting to with SSH. I have tried a few exports, and have not found the one that will provide the remote hostname in the tab.


MacBook Pro (15-inch Mid 2010), Mac OS X (10.7.4)
  • 1. Re: Terminal Tab Names (SSH host)
    Topher Kessler Level 6 Level 6 (9,340 points)

    The tab names are determined by the currently running process, and there is no way to automatically pass the hostname or any other information these processes are handling to the shell as a replacement for the process name. However, you can rename the tabs manually by right-clicking them and choosing "Inspect Tab" or by pressing Command-I. In the inspector window, click the "Info" tab and set the title in the appropriate text field (second from the top).

  • 2. Re: Terminal Tab Names (SSH host)
    mtharpe Level 1 Level 1 (0 points)

    I see that I can set the name, but this would be a static assignment. I am looking for a dynamic name assignment with the ssh username and host i.e. username@sshhost.

  • 3. Re: Terminal Tab Names (SSH host)
    Topher Kessler Level 6 Level 6 (9,340 points)

    Here's a solution that might work.

     

    Open a terminal window and edit your bash profile:

     

         pico ~/.bash_profile
    

     

    In the profile, add the following function:

     

         function tabname {
              printf "\e]1;$1\a"
         }
    

     

    Save and quit the editor (control-o, control-x), and then create a new bash session. Now when you type the following command you can change the name of the tab:

     

         tabname NAME
    

     

    (credit to The Lucid for this: http://thelucid.com/2012/01/04/naming-your-terminal-tabs-in-osx-lion/)

     

    With this setup, you can add this command before your ssh command in the following manner:

     

         tabname NAME; ssh username@host
    

     

    You can also script this behavior in the following manner. First create a script called "myssh.sh" or something similar, and then add the following to it:

     

         !#/bin/bash
         printf "\e]1;`echo $1 | sed -Ee 's/^.+\@//'`\a"
         ssh $1
         printf "\e]1;bash\a"
    

     

    When you run the script, use it as a replacement for the ssh command (if you need to add arguments, then encase them in quotes or escape the spaces between them). For example, see below:

     

         ./myssh.sh "-p 22 username@host.com"
    

     

         or...

     

         ./myssh.sh -p\ 22\ username@host.com
    

     

    If there are no arguments then you can simply use the following:

     

         ./myssh.sh username@host.com
    

     

    One issue with this script is that if you cancel the connection or otherwise kill the command without doing so cleanly, then the script will halt and leave the name of the tab as the hostname. The last line of the script should rename the tab back to "bash" when done if the ssh session exits cleanly and allows the script to continue.

  • 4. Re: Terminal Tab Names (SSH host)
    Topher Kessler Level 6 Level 6 (9,340 points)

    BTW, in the script you can use "$*" to handle all arguments as one variable instead of "$1", which will allow you to use the script without escaping any spaces or needing to use quotes in the arguments string.

  • 5. Re: Terminal Tab Names (SSH host)
    Zachary Jones Level 1 Level 1 (15 points)

    When I implement bash scenarios like these, I find that my SSH sessions, regardless, changes to the name user@host

     

    So, first it changes to my_custom_tab_name and then it almost-immediately changes to user@host

     

    I get the impression that SSH is sending the same control string when it runs, replacing the one I just sent.

     

    I don't think it is the remote server, since when I run this command on the remote, it does not change my local tab name.

     

    I looked in the SSH man pages and cannot find any documentation of it sending the escape code to change the terminal tab name.

     

    Does anyone know how to control this behaviour?

  • 6. Re: Terminal Tab Names (SSH host)
    BobHarris Level 6 Level 6 (13,100 points)

    So modify your remote system's shell initialization file to issue the title escape sequences.

     

    If using bash,

    .bash_profile

    .bash_login

    .profile

    .bashrc

     

    If using csh or tcsh

    .login

     

    If using sh or ksh

    .profile

     

    and you are on your own if you are using zsh, but I think .profile still works.

  • 7. Re: Terminal Tab Names (SSH host)
    VikingOSX Level 5 Level 5 (5,485 points)

    The following bash script does the following:

    1. Takes a hostname as a command line argument. Prompts for it if missing.
    2. Uses a trap for command errors. Reports them, and resets Terminal tab name back to bash
    3. Sets the terminal tab to the remote hostname
    4. Runs ssh
    5. On conclusion of normal connection, Terminal tab name is reset to bash

     

    Assumption: In Terminal > Preferences > Settings > Window > Title. All items are deselected.

     

    There is no need to do anything on the remote host to set terminal tab name.

     

    Screen Shot 2012-12-15 at 12.10.41 AM.png

  • 8. Re: Terminal Tab Names (SSH host)
    BobHarris Level 6 Level 6 (13,100 points)

    If ssh is not the changing the title, there is no need to have the remote node do anything.

     

    However if ssh is changing the title, or the remote host's /etc/profile, then either the remote account's shell initialization needs to take control, or play some local games with a background process started before the ssh sleeping just long enough to allow ssh or remote system to do its thing, then wake up and reset it to what you want.