Skip to content

Instantly share code, notes, and snippets.

View purpleidea's full-sized avatar

James purpleidea

View GitHub Profile
@purpleidea
purpleidea / .bashrc_vagrant.sh
Last active August 1, 2017 10:33
# Vagrant vsftp and other tricks# from: https://ttboj.wordpress.com/2013/12/21/vagrant-vsftp-and-other-tricks/# and# Vagrant clustered SSH and 'screen'# from: https://ttboj.wordpress.com/2014/01/02/vagrant-clustered-ssh-and-screen/# for use inside a ~/.bashrc or similar
# vagrant vsftp and other tricks
# from: https://ttboj.wordpress.com/2013/12/21/vagrant-vsftp-and-other-tricks/
# and
# Vagrant clustered SSH and 'screen'
# from: https://ttboj.wordpress.com/2014/01/02/vagrant-clustered-ssh-and-screen/
# for use inside a ~/.bashrc or similar
# copyright James Shubin, 2013, agplv.3+
### VAGRANT ###################################################################
# avoid needing to always add --provider=kvm
@purpleidea
purpleidea / gist:8202237
Created December 31, 2013 21:05
for your .bashrc
# run nethogs on the interface that is being used for the default route
function nethogs {
dev=`ip r | grep '^default' | awk '{print $5}'`
#echo $dev
if [ `id -u` -eq 0 ]; then
nethogs $@ $dev
else
sudo nethogs $@ $dev
fi
#!/bin/bash
if [ "$1" = '' ]; then
echo "Usage: ./`basename "$0"` <hostname>"
exit 1
fi
# NOTE: lets say you try to provision a previously provisioned host...
# $ vp foo
# [foo] Configuring cache buckets...
@purpleidea
purpleidea / quick-devops-hacks
Created February 4, 2014 05:04
Code and slides from "Quick DevOps Hacks" lightning talk.
# slides
Available at:
https://dl.dropboxusercontent.com/u/48553683/quick-devops-hacks-devopsmtl-2014.pdf
# show the exit status in your $PS1
Article and code at:
https://ttboj.wordpress.com/2014/01/29/show-the-exit-status-in-your-ps1/

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

#!/bin/bash
# Restart the GNOME shell by commandline via SSH. Similar to nohup.
# Copyright (C) 2013-2014 James Shubin
# Written by James Shubin <james@shubin.ca>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
$ cat fix-dropbox.sh
#!/bin/bash
# XXX: use at your own risk - do not run without understanding this first!
exit 1
# safety directory
BACKUP='/tmp/fix-dropbox/'
# TODO: detect or pick manually...
$brickdir = '/storage1';
$glusterdir = '/storage';
// if you only want to copy certain directories, specify them here
$directories = array('apt-mirror', 'apt-repo', 'downloads', 'torrents', 'storage');
$max_copies = 100; // concurrency
$file_size = 0; // total copied file size
{
"createdBy": "Redirector v3.0.4",
"createdAt": "2015-11-14T09:52:11.766Z",
"redirects": [
{
"description": "ghttps",
"exampleUrl": "https://example.com/whatever",
"exampleResult": "ghttps://example.com/whatever",
"error": null,
"includePattern": "https://example.com/*",
@purpleidea
purpleidea / git-tpush.sh
Last active September 9, 2018 00:05
Safely pust to git master once CI passes: https://ttboj.wordpress.com/2016/02/16/introducing-git-tpush/
#!/bin/bash
# git-tpush: Safely push to git master once CI passes
# Copyright (C) 2016+ James Shubin, AGPLv3+
# Written by James Shubin <james@shubin.ca>
# Put in your $PATH such as ~/bin/ as "git-tpush" and make it executable
# README: https://ttboj.wordpress.com/2016/02/16/introducing-git-tpush/
ORIGIN='origin' # remote remote, most call this 'origin', some say 'upstream'
BRANCH='master' # remote branch, most everyone calls this master
POLL='5s' # how often do we sleep between polling the ci?