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?

yodog
  • 296

4 Answers4

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.

muru
  • 207,228
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.
l0b0
  • 9,271
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

Rmano
  • 32,167
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 username@0.0.0.0

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

Created using the following:

TheSchwa
  • 3,860