Elegant way to pass arguments to a bash script

When I started to write bash scripts, I found that it was really convenient to use the basic way to pass arguments with $1 $2 and so on.

It’s quick and easy to write, but not very flexible because the argument order maters and you need to write a check condition.

When I discovered the getopts option, it made my scripts way better. In the following example, I added a function named “commands” that executes the command if the dry-run flag is not set and just display the command if not

Another interesting arg is the “set -x” command, that prints all the command executed by the script.

Continue reading “Elegant way to pass arguments to a bash script”

Reverse shell on firewalled server

Sometimes, it may be useful to keep a connection to a server (let’s call it SERVER_1) that is not reachable from the internet because of iptables ingress rules or simply because it doesn’t have a public IP.¬† You can reach it from another server (let’s name it SERVER_2) that is accessible.

1 – From the server you want to reach (SERVER_1) :

you@SERVER_1:/~ ssh -R 22222:localhost:22 USERNAME@SERVER_2

Continue reading “Reverse shell on firewalled server”

How to resume a stalled scp transfer

SCP is the perfect command line tool to transfer huge files from a server to another one but if the connection drops, the transfer become “stalled” and seems impossible to resume. Don’t panic, rsync allow us to resume a failed scp transfer :

 

rsync --partial --progress --rsh=ssh user@server:path/my_file /home/my_file

 

Simple python random

Recently, I had to chose randomly 4 people for a thankless task. It was the perfect occasion to code a simple script using the Python random module:
#!/usr/bin/python
# -*- coding: utf-8 -*-
#export PYTHONIOENCODING=utf8
import random
people_initials = ['ML','WD','ME','BH','FS','AQ','SB','FR','KB','LL','FP','MG','AH','FJ']
rand_people = random.sample(people_initials, 4)
print rand_people

 

Don’t name your script random.py or it won’t work.

IP extraction

Regex are useful to cope with basic unix / linux administration tasks.
To extract IP from file, this simple command do the trick :
grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}' /var/log/auth.log 15-08-27 - 16:17:00
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
188.216.199.96
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142
11.213.54.142

Continue reading “IP extraction”