9

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?

7
  • 1
    what do you need to know toidentify your server? I simply type in "hostname" to see the servers name. That's enough for me to know because we used to use a good naming convention
    – Private
    Sep 22, 2014 at 13:22
  • currently i have user@hostname on the first host i connect. i would like to see the ip address. also, if i jump from server1 to server2, the tab doesn't change names.
    – RASG
    Sep 22, 2014 at 13:26
  • yes that's your name, but I mean servername. there must a hostname for that machine. isn't that enough info for you?
    – Private
    Sep 22, 2014 at 13:29
  • 1
    How about setting the background color of your terminals? Depending on how you connect and what terminal you're using you could be able to automate it. For example, I may set a dark red background for production servers so I know I have to be extra careful, especially when connecting simultaneously to staging servers which look and feel exactly the same. Sep 23, 2014 at 3:14
  • 1
    If you are switching between 30 servers constantly then you need to look at scripting and tools like puppet for management. Also if an IP is more recognisable than the hostname, these don't sound like good hostnames!
    – JamesRyan
    Sep 23, 2014 at 10:19

4 Answers 4

11

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.

5

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.
1
  • thanks. and how would you automatically add that as your terminal tab title?
    – RASG
    Sep 22, 2014 at 13:46
2

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

2

This solution is a little hackish, but I think it will do what you're looking for. You will need xdotool and wmctrl installed for this to work.

First you have to edit your ~/.bashrc file because by default the terminal resets its title after every line, rendering command line title sets useless.

Right after: xterm*|rxvt*)

Replace: PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]$PS1"

With: PS1="${debian_chroot:+($debian_chroot)}\u@\h \w\a$ "

Next we're going to create a custom ssh script called whatever you want, but for this answer I'll be calling mine tab-ssh. Open a text editor and save the following as tab-ssh:

#!/usr/bin/env bash

WID=$(xprop -root | grep "_NET_ACTIVE_WINDOW(WINDOW)"| awk '{print $5}')
xdotool windowfocus $WID
xdotool key ctrl+shift+t
wmctrl -i -a $WID

sleep 1
title=$(echo "$*" | sed s/.*@//g)
xdotool type --delay 1 --clearmodifiers 'echo -en "\033]0;"'"${title}"'"\a"'
xdotool key Return

sleep 1
xdotool type --delay 1 --clearmodifiers "ssh $*"
xdotool key Return

Then make it executable:

chmod +x tab-ssh

Now, assuming you're using bash and gnome-terminal, running the script should open a new tab, rename it, and start the ssh session. For exmaple:

tab-ssh [email protected]

will open a new tab, rename it to 0.0.0.0 (i.e. everything following the "@" sign), then execute [email protected].

Created using the following:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .