Identify the server I'm working at

How to identify which server you're on when using ssh from terminal?

I have to ssh into 30 servers daily, and switch between them constantly.
Currently I'm editing the tab name and writing the IP iddress to jump from one to another.

Is there an automated way of doing this (assigning the server IP address as the tab name)? Any other alternatives that won't involve editing a file on each server?


Solution 1:

I usually use the hostname, set via $PS1. However, if you'd take the trouble, you can start using GNU screen and then you could define a function like so:

function ssh-title () {
    host="${@: -1}"  # You could customize it to make it identify the hostname better
    IP=$(host "$host" | awk '/has address/ { print $4 }')
    echo -ne '\033]0;'"$host"' - '"$IP"'\007'
    screen ssh "$@"
}

Explanation:

  1. Even if you set a title from your local shell, it may get overwritten by the remote shell settings. screen doesn't directly let the remote shell do this, and you have to customize it to allow it do so, making it easier for you to set a title locally and stick to it.
  2. Look up IP from hostname, while considering the last parameter as the hostname, and set it as the title. I had to do a bit of trial and error before I got a right echo string. You might have to do it too.
  3. Finally, use screen to start a session with the ssh command. The session ends when the command exits.

You can customize this in various ways. For example, you may save the hostnames and IPs in a file and read from it (kinda like the ssh_config) (and maybe save other things for the title, like usernames).

Now use ssh-title some.host to connect. I think this is the only way you can do this without editing anything server side.

Solution 2:

A few ways to identify a server:

  • SSH fingerprint - secure but changes whenever you redeploy.
  • Shell prompt (PS1) and terminal title - insecure (anybody can duplicate it) but simple and practical.
  • ip address - secure with an SSH fingerprint, and more easily readable.
  • hostname - insecure but very readable.

Solution 3:

Another option is to use liquidprompt --- works for both bash and zsh, and can be easily configured to have the terminal title set and even to color in different colors the hostname in the prompt:

enter image description here