— scripting101, automation, devops, feature — 1 min read
During my office-hours, what I do as a Devops Engineer is mostly repetitive tasks to implement and test parts of system or software across multiple versions and environments. For example, occasionally I need to verify installation of a random software across all servers that I am in charge of. In which I will need to conduct a ssh through bastion host(s) to connect to multiple servers. If you are not familiar with the term bastion host, check the below diagram.
Such simple tasks would not be too complicated to spent a huge amount of time to develop an ansible-playbook or pipeline and used for a single time, however, it is still very time-comsuming and error-prone if these tasks were not done properly. Hence, finding the right tool for the right job is my purpose of this series, and scripting comes in very handy for such simple tasks.
In this post, I will introduce is a simple example of combining peco with ssh command to simplify workflow. The purpose of this script is to quickly ssh to remote server with or without using a bastion host or jump host. List of hosts will be configure in a collection file in ini format as an example listed below
Here is the script:
1#!/bin/bash2
3if [ $# -ge 1 ]4then5 CONFIG=$16else7 CONFIG=~/.config/ssh_peco/remote_hosts8fi9
10function read_ini_file() {11 local obj=$112 local key=$213 local file=$314 awk '/^\[.*\]$/{obj=$0}/=/{print obj $0}' $file \15 | grep '^\['$obj'\]'$key'=' \16 | perl -pe 's/.*=//'17}18
19function ssh_start() {20 REMOTE=$(grep -iE "\[*\]" $CONFIG | tr -d "[]" | peco)21 echo $REMOTE22 IP=$(read_ini_file $REMOTE ip $CONFIG)23 BASTION=$(read_ini_file $REMOTE bastion $CONFIG)24 if [ -z ${BASTION} ]; then25 ssh $IP26 else27 BASTION_IP=$(read_ini_file $BASTION ip $CONFIG)28 ssh -J $BASTION_IP $IP29 fi30}31
32ssh_start
For a working example code using this script, take a look at my repository
Simplistic interactive filtering tool
peco is a great filter tool that can be pipelined with multiple output types: logs, directory, grep, ... It is such a amazing tool. Read more about peco at this awesome repo https://github.com/peco/peco
In order to use this script, peco must be install
1#macOS2brew install peco3#ubuntu4apt install peco
Default file path is ~/.config/ssh_peco/remote_hosts
Otherwise, you can input during execution
1[bastion_hostname]2ip=<public_ipv4>3
4[hostname]5ip=<private_ipv4>6bastion=<bastion_hostname>
If you are not providing custom config file, default remote_hosts file should
be available at ~/.config/ssh_peco/remote_hosts
For example
1# in remote_hosts2[jump-host]3ip=12.34.56.784
5[private-server]6ip=10.0.0.107bastion=jump-host8
9# executing10# ssh_peco.sh <remote_hosts_collection>(optional)11ssh_peco.sh ~/.config/ssh_peco/remote_hosts
peco showing available hosts
query from text
However, this might be prone to security concerns.
Do it as your own risk
Create symlink for script and remote_hosts config file
1mkdir -p ~/.config/ssh_peco2ln -ns $(pwd)/ssh_peco.sh /usr/local/bin3ln -ns $(pwd)/remote_hosts ~/.config/ssh_peco