Skip to content

Instantly share code, notes, and snippets.

@sqqqrly
sqqqrly / trapping.sh
Last active Apr 1, 2020
Trapping signals / awk, without leaning toothpicks
View trapping.sh
#!/bin/bash
function shutdown {
echo "Shutting down $PROGRAM"
local -r QUIT="SIGQUIT"
# TODO - What is the process is not found?
local -r PID="$(ps aux | awk '( $0 ~ "/usr/bin/clickhouse-server" ) {print $2}')"
echo "pid: $PID"
#kill -s 3 "$PID"
echo "Done killing"
@sqqqrly
sqqqrly / bounce_services.out
Last active Feb 28, 2020
A utility to restart select services and/or truncate log files.
View bounce_services.out
$ ./bounce_services.sh
=== What would you like to do with openstack services?
services --> logs:
openstack-nova-compute-8202E4C_10B54ER.service --> /var/log/nova/nova-compute-8202E4C_10B54ER.log
openstack-cinder-volume-generic0.service --> /var/log/cinder/volume-generic0.log
none --> /tmp/infinidat_logit.log
1) truncate logs only 3) truncate and restart
2) restart services only 4) exit
@sqqqrly
sqqqrly / output
Last active Aug 29, 2019
Filter away sensitive strings in py3 from stdout and other stream-like objects. Here we are filtering stdout, stderr and file stream.
View output
[I] ➜ ./redactor.py
data out: ********
data err: ********
data out: ********
data err: ********
[I] ➜ cat test.txt
data: ********
data: ********
@sqqqrly
sqqqrly / create_veth.sh
Last active Jun 20, 2019
Create a veth using dummy module without setting an IP
View create_veth.sh
#!/usr/bin/env bash
# Create a dummy veth.
# To add an addr:
# ip addr add $addr/24 brd + dev $veth label $veth:0
# To remove $veth:
# if addr set:
# ip addr del $addr/24 brd + dev eth10 label veth5:0
# ip link delete $veth type dummy
# rmmod dummy
View gist:9985420

Let's say you have a Bash shell script, and you need to run a series of operations on another system (such as via ssh). There are a couple of ways to do this.

First, you can stage a child script on the remote system, then call it, passing along appropriate parameters. The problem with this is you will need to manually keep the remote script updated whenever you change it -- could be a bit of a challenge when you have something to execute on a number of remote servers (i.e., you have a backup script running on a central host, and it needs to put remote databases in hot backup mode before backing them up).

Another option is to embed the commands you want to run remotely within the ssh command line. But then you run into issues with escaping special characters, quoting, etc. This is ok if you only have a couple commands to run, but if it is a complex piece of Bash code, it can get a bit unwieldy.

So, to solve this, you can use a technique called rpcsh -- rpc in shell script, as follows:

First, place th