…with an open minded approach

Latest

setup iptable rules for transmission (torrents)

Without further addo, here it is, a working example how to configure iptables to allow transmission torrents to internet (udp 51413) + transmission remote RPC (tcp 9091, localnetwork only) + ssh (tcp 22, localnetwork only).

 


# To keep it DRY (and fix some errors, put some improvements) I've updated the firewall info in this other gist:
# https://gist.github.com/zipizap/6935850
# Check it out 🙂

 

Drop a comment if its usefull 🙂

text manipulation: lines to columns and other goodies

Have a look at the power of the “paste” comand – it just solves a bunch of situations with ease, and its litle known… transpose pair a lines into 2 columns, etc…

 

Have a look to these examples to get an idea of “paste” power…

 

http://www.theunixschool.com/2012/07/10-examples-of-paste-command-usage-in.html

Compress your mobile video files to mp4 (x264 + mp3)

My mobile phone records videos with an astonishing 100MBytes/minute… this is because the video is saved with almost no compression at all… So I made this little script to compress a video file with x264 (video compression) and mp3 (audio compression) and store it in a MP4 container.

It uses the “avconv” executable which comes with the ubuntu package “libav-tools” – if you dont have it, you can install with “sudo apt-get install libav-tools”

 


#!/bin/bash
[[ -r $1 ]] || { echo "Usage: $0 <videofile-to-be-resized> [<videofile2> …]"; exit 1; }
for INPUT_FILE in "$@"; do
OUTPUT_MP4_FILE=$(echo "$INPUT_FILE" | sed 's/\.[^.]*$/_resized.mp4/g')
#See https://trac.ffmpeg.org/wiki/x264EncodingGuide
avconv -y -i "$INPUT_FILE" -threads auto -c:v libx264 -preset medium -b:v 988k -pass 1 -an -f mp4 /dev/null && \
avconv -i "$INPUT_FILE" -threads auto -c:v libx264 -preset medium -b:v 988k -pass 2 -c:a libmp3lame -b:a 192k "$OUTPUT_MP4_FILE"
rm -f av2pass* &>/dev/null
done

 

Drop a comment if its usefull to you 🙂

bash scripts multiple arguments

Example how to parse multiple arguments, one-by-one, in bash – be aware of the importance of surrounding “$@” and “$ARG_I” within double-quotes.

 


#!/bin/bash
[[ $# -gt 1 ]] || { echo "Usage: $0 <arg1> <arg2> … <argN>"; exit 255; }
echo 'Dont forget to surround the "$@" with the double-quotes '' so that it does not divide arguments that contain spaces.'
echo 'Samewise, you also need to surround the "$ARG_I" var'
for ARG_I in "$@"; do
echo '$@ = '"$@"
echo '$ARG_I = '"$ARG_I"
done
# paulo@lnxp:/tmp$ bash a.sh "Arg1" "I'm argument 2 with spaces" "And Im arg3"
# $@ = Arg1 I'm argument 2 with spaces And Im arg3
# $ARG_I = Arg1
# $@ = Arg1 I'm argument 2 with spaces And Im arg3
# $ARG_I = I'm argument 2 with spaces
# $@ = Arg1 I'm argument 2 with spaces And Im arg3
# $ARG_I = And Im arg3
#
# NOTE:
# Dont forget to surround the "$@" with the double-quotes so that it does not divide arguments that contain spaces.
# Samewise, you also need to surround the "$ARG_I" var

 

 

unix directory hacks with cd

I’ll just link to the http://www.thegeekstuff.com/2008/10/6-awesome-linux-cd-command-hacks-productivity-tip3-for-geeks/

Read them carefully and your console efficiency may just get a big boost 🙂

 

sudo run as another user – scripting it


function run_as_user {
local NEW_USER=$1
local CMDS=$2
sudo -u $NEW_USER -i bash -l -c "$CMDS"
}
#Use it like
run_as_user luke "echo $PWD; id"

view raw

run_as_user.sh

hosted with ❤ by GitHub

To ease your admin scripts….

Bash local variables

All variables in bash default to being *global*.

To define a variable as *local* to a function, we can do:

function my_func {

local MY_LOCAL_VAR=”is local to the scope of its function”

}


#!/usr/bin/env bash
# In bash all variables are defined by default as GLOBAL, even if defined inside functions
# To define a variable as LOCAL to a function, we have to prepend "local" in the definition
# This makes the variable only defined in the current function scope and so not global.
VAR_GLOBAL="im global – you can read and change me from anywhere, even inside functions – which may not always be a good thing"
echo "Seen from outside: $VAR_GLOBAL"
function my_func {
echo "Seen from inside my_func: $VAR_GLOBAL"
VAR_GLOBAL="the global var was changed from inside my_func – one must be conscient when doing this or some drangons may come out inespectedly later…"
echo "Seen from inside my_func: $VAR_GLOBAL"
}
my_func
echo "Seen from outside: $VAR_GLOBAL"
echo "————————"
function my_func2 {
local VAR_LOCAL_INSIDE="var local inside"
echo "Seen from inside my_func2: $VAR_LOCAL_INSIDE"
}
my_func2
echo "Seen from outside: $VAR_LOCAL_INSIDE"

debugging bash scripts

There is a very handy bash option, that will “run the entire script in debug mode. Traces of each command plus its arguments are printed to standard output after the commands have been expanded but before they are executed” (see [1])

This seems most usefull for inhouse-scripts, to show progress of the script

 


red@ownc:/tmp$ cat a.sh
#!/bin/bash
# Activate debug
set -x
echo "hi"
ls
pwd
# De-activate debug
set +x
date
red@ownc:/tmp$ ./a.sh
+ echo hi
hi
+ ls
9591.jsvc_up a.sh hsperfdata_cassandra hsperfdata_root tmux-1001
+ pwd
/tmp
+ set +x
Tue Dec 10 17:05:17 CET 2013

view raw

gistfile1.txt

hosted with ❤ by GitHub

 

[1]: http://tldp.org/LDP/Bash-Beginners-Guide/html/sect_02_03.html

 

 

Detect ssh login from ~/.bashrc

When you login with ssh, your environment gets 2 variables defined: SSH_CONNECTION and SSH_TTY

A simple way to detect if you are inside a bash session, established from ssh-login, is to check these vars

You can put this check inside ~/.bashrc, so that it executes in all new bash-logins, so that when a ssh-login is detected you can make something happen 🙂

This is a simple hack to detect normal ssh-logins, but dont use this for anything too important 🙂

 


# If you put this check inside ~/.bashrc you can detect bash session opened by ssh-logins 🙂
[ "$SSH_CONNECTION" -a "$SSH_TTY" == $(tty) ] && echo "This bash session was opened from an ssh login :)"

view raw

gistfile1.sh

hosted with ❤ by GitHub

 

incron

There is a great linux kernel functionality called “inotify” – which can emmit events on file changes. Its just great, have a look at its home page, I think its one of those linux pearls not much widely known…

Well, the thing is that using inotify, a tool called incron has been made, which is like a cron for file events (instead of time events), and that is really usefull when we want to detect file changes and react accordingly 🙂 very usefull for server scripts 🙂

And so, without further delay, here it goes, the best resume I found so far detailing how to use incron: http://www.howtoforge.com/triggering-commands-on-file-or-directory-changes-with-incron

NOTES: there be dragons…

Inside you incrontabs, you must leave *only* 1 space between the <path> <mask> <cmd>. If you leave 2 or more spaces, then the 2nd (and more) spaces will be considered part of the <mask> or <cmd> and it will fail… I was leaving 2 spaces between <mask> and <cmd>, and incron did not work and in /var/log/syslog there were these messages

incrond[27693]: cannot exec process: No such file or directory

This was because of having 2 spaces… when I corrected to only 1 space, it began working correctly. Keep it in mind, so you dont suffer as much as I did to find it out 🙂

 

After editing with “incrontab -e”, check that “incrontab -l” shows the rules. If it does not show some rule, then that is because that rule has some error and was not recognized.

 

To debug what is happening behind the curtains, its usefull to have a “tail -f /var/log/syslog” on another terminal…

 

Cheers