Skip to content
/var/log/tienvv.blog
About meLinkedin

scripting101: "peco" a great discovery

scripting101, automation, devops, feature1 min read

Introduction

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.

bastion


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/bash
2
3if [ $# -ge 1 ]
4then
5 CONFIG=$1
6else
7 CONFIG=~/.config/ssh_peco/remote_hosts
8fi
9
10function read_ini_file() {
11 local obj=$1
12 local key=$2
13 local file=$3
14 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 $REMOTE
22 IP=$(read_ini_file $REMOTE ip $CONFIG)
23 BASTION=$(read_ini_file $REMOTE bastion $CONFIG)
24 if [ -z ${BASTION} ]; then
25 ssh $IP
26 else
27 BASTION_IP=$(read_ini_file $BASTION ip $CONFIG)
28 ssh -J $BASTION_IP $IP
29 fi
30}
31
32ssh_start

For a working example code using this script, take a look at my repository

About peco

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


Installation

In order to use this script, peco must be install

1#macOS
2brew install peco
3#ubuntu
4apt install peco

Update host collections

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>

Usage & Example

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_hosts
2[jump-host]
3ip=12.34.56.78
4
5[private-server]
6ip=10.0.0.10
7bastion=jump-host
8
9# executing
10# ssh_peco.sh <remote_hosts_collection>(optional)
11ssh_peco.sh ~/.config/ssh_peco/remote_hosts

Results:

  • peco showing available hosts demo

  • query from text query1 query2

Symlink make it quicker for integration

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_peco
2ln -ns $(pwd)/ssh_peco.sh /usr/local/bin
3ln -ns $(pwd)/remote_hosts ~/.config/ssh_peco
© 2024 by /var/log/tienvv.blog. All rights reserved.
Theme inspired by LekoArts