Skip to content

Instantly share code, notes, and snippets.

sshx - Run a Local Script over SSH with Interactivity

Sometimes you want to run a bit more than just one command over ssh, and you also want to maintain interactivity to the processes that you launch. Usually you could just do ssh -t <host> "foo; bar; fish; paste" however that can quickly become unwieldly, especialy if it is more than just a sequence of commands. You also can't use cat my_script | ssh <host> /bin/bash because then you loose your stdin and thus your interactivity.

However, there is a way to achieve our goal using the first form above. Consider that the maximum command line length is usually more than 2MB (see getconf ARG_MAX). Thus, we convert our entire script into a base64 encoded string with cat my_script | base64 and then call ssh -t <host> /bin/bash "<(echo <base64 string> | base64 --decode)". In this way the entire script is sent to the host in the command buffer, and so long as our total command length is less than 2MB, it will run.

@sourcesimian
sourcesimian / python-trezor.md
Created January 16, 2018 20:26
Installing Python trezor API on Ubuntu Linux (17.10)

Installing Python Trezor API on Ubuntu Linux (17.10)

This proved to be quite tricky. I did so for Python 2 and Python 3. Here are my notes:

Setup for Chrome:

$ cat /etc/udev/rules.d/51-trezor.rules

SUBSYSTEM=="usb", ATTR{idVendor}=="534c", ATTR{idProduct}=="0001", MODE="0666", GROUP="dialout", SYMLINK+="trezor%n"
KERNEL=="hidraw*", ATTRS{idVendor}=="534c", ATTRS{idProduct}=="0001",  MODE="0666", GROUP="dialout"

(ref: https://doc.satoshilabs.com/trezor-user/settingupchromeonlinux.html#manual-configuration-of-udev-rules)

@sourcesimian
sourcesimian / update.sh
Created May 17, 2017 13:14
Dropin folder to generate ~/.ssh/config
#!/bin/bash
# Put this script in a folder named ssh-config.d
# Add as many contributer files as you line to this folder
# They will be concatenated into ~/.ssh/config in lexical order
echo "### Auto generated by: $0 ###" >~/.ssh/config
for script in $(ls -1 $(dirname $0)/* | grep -v -e "$0" -e "~\$" ); do
echo "" >>~/.ssh/config
echo "# --- Origin: $script --- #" >>~/.ssh/config
@sourcesimian
sourcesimian / Bitcoin-Armory-Migration.md
Created April 16, 2017 11:22
Bitcoin: Migrate away from Armory Wallet

Bitcoin: Migrate away from Armory Wallet

Previously I setup an Armory online/offline/cold/paper bitcoin storage system. Recently I decided to migrate my bitcoin from Armory wallet cold storage to the BIP39 and related standards. I hadn't switched on the offline PC in a long time, and neither had I updated the blockchain on the online PC. Thus, I started considering my recovery options should something have broken or gone missing. Below are some of the ways I found to recover and move bitcoin in such failure scenarios.

Armory Paper Backup -> Root Key

Version 1.35c

The single-sheet Armory paper backup directly displays the Root Key, e.g.:

Root Key:     nidh jtfj ejue whkj dngt dftt hsss idgh ssss
              wwad dojw aeja idfg went ukkk sajj uuwj kwjt
@sourcesimian
sourcesimian / read-only-bind-mount.txt
Last active June 11, 2016 21:26
Read only bind mount
## Setup
$ sudo mkdir /mnt/pool-RO
$ sudo mount --bind /mnt/pool /mnt/pool-RO
$ sudo mount -o remount,ro /mnt/pool-RO
## Test
$ touch /mnt/pool/t1
$ touch /mnt/pool-RO/t2
touch: cannot touch `/mnt/pool-RO/t2': Read-only file system
$ rm /mnt/pool-RO/t1
@sourcesimian
sourcesimian / md5sum-cmp.sh
Created June 11, 2016 19:50
Filesystem tree content comparison using md5sum
#!/bin/bash
# Filesystem tree content comparison using md5sum
#
# Usage:
# $ md5tree-cmd hash <folder 1> > my_hash_file_1
# $ md5tree-cmp hash <folder 2> > my_hash_file_2
#
# $ md5tree-cmp only <hash_file_1> <hash_file_2>
set -e
cmd=$1
@sourcesimian
sourcesimian / tssh
Created June 10, 2016 23:00
Light weight MoSH using Tmux and BASH
#! /bin/bash
# usage: tssh <host> <tmux target> [<ssh args>]
# requires ssh passwordless login to be setup and tmux to be installed on the remote machine
host=$1
target=$2
args=${*:3}
ssh="ssh -o ConnectTimeout=4 -o ServerAliveInterval=4 -o ServerAliveCountMax=2 -o TCPKeepAlive=yes"
if [ ! -z $target ]; then
@sourcesimian
sourcesimian / texttable.py
Created June 9, 2016 20:16
Parser to interpret tabulated text
import re
class TextTable(object):
"""
Parser to interpret tabulated text, e.g:
>>> text = '''-------------------------------------------------------------------
... PRES HGHT TEMP DWPT RELH MIXR DRCT SKNT THTA THTE THTV
... hPa m C C % g/kg deg knot K K K
@sourcesimian
sourcesimian / argparsetree.py
Created June 9, 2016 18:15
A convenience class for building hierarchical CLI argument parser with sub commands using ArgumentParser.
#!/usr/bin/env python
import sys
from argparse import ArgumentParser
class ArgParseTree(object):
"""
A convenience class for building hierarchical CLI argument parser with sub commands using ArgumentParser.
Example: