One tmux to Rule Them All

| by Aviv Laufer

tmux_and_HipChat_-_Dev

 

At Rounds, we have quite a few servers that we deploy across multiple cloud providers. We always aim everything will be done automatically. We use Ansible for minimizing cases where we have to SSH into a machine. However, in real life we do need from time to time to perform some manual tasks on the servers. Since we are great fans of tmux, we use tmux-cssh for SSHing into multiple machines and execute the same commands in all of them simultaneously, like in the James Bond movies. Since our machine inventory is dynamic, we needed to find a way to log into servers that we don’t have a DNS entry and we don’t know their IP address.

We wrote two scripts, one for AWS and one for DigitalOcean, that use the machines’ tag names in order to populate a list of online machines to log into.

DigitalOcean

# Usage:
$ do-ssh.sh <server name starts with>
 
# For example, the following SSH’s into all machines whose name starts with “MyServer-”
$ do-ssh.sh MyServer-

Here’s the source:

#!/bin/bash
OLD_TMUX=$TMUX
unset TMUX
CMD="tmux-cssh -ns -c ~/.ssh/YOURKEY"
while read line
do
CMD="${CMD} -ts  ${line}"
done < <(tugboat droplets | grep $1 | awk {'print $1 " -sc root@"  $3'} | sed "s/,//")
$CMD
TMUX=$OLD_TMUX

Amazon

# Usage:
$ ec2-ssh.sh <server tag name regex>
 
# For example, the following SSH’s into all machines whose tag name starts with My-Server
ec2-ssh.sh "My-Server*"
#!/bin/bash
OLD_TMUX=$TMUX
unset TMUX
CMD="tmux-cssh -ns -c ~/.ssh/YOURKEY"
while read line
do
CMD="${CMD} -sc ubuntu@${line}"
done < <(aws ec2 describe-instances --filter '{"Name":"tag:Name","Values":["'"$1"'"]}'  |grep PublicIpAddress |  awk {'print $2'} |sed "s/,//"|sed "s/"//g")
$CMD
TMUX=$OLD_TMUX

Category:

Have something to say?