pretty

Monday, January 13, 2014

Check if process is running in bash script

Check if process is running in bash script

#!/bin/bash

if [ -z "$(pgrep mysql)" ]
 then
     echo "Mysql is not running"     
 else
     echo "Mysql is running"
fi

Friday, January 10, 2014

Backup and restore mysql database

Make backup

mysqldump -u user -p password  | gzip > outputfile.sql.gz

Restore

gunzip < outputfile.sql.gz | mysql -u user -p password databasename
adapted from http://www.ducea.com/2006/10/28/compressing-mysqldump-output/

Thursday, December 26, 2013

Compare directory trees

bash version

diff <(cd dir1 && find | sort) <(cd dir2 && find | sort)

Python version soon . . .

Monday, November 18, 2013

Disable line wrap in bash

Disables
printf '\033[?7l'
Enables
printf '\033[?7h'

Brief pcap view with tshark

tshark -T fields -eip.src -e tcp.srcport -eip.dst -e tcp.dstport -e text -r http.cap | sed -e 's/Source GeoIP: Unknown,Destination GeoIP: //g'

Thursday, August 29, 2013

Traverse a directory in python

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import os

for root, dirs, files in os.walk(rootpath):
	for filename in files:
		path = os.path.join(root, filename)
		print path


With file filtering

#!/usr/bin/env python
# -*- coding: utf-8 -*- 
import os
import fnmatch

pattern = "*.conf"
rootpath = "/etc"
for root, dirs, files in os.walk(rootpath):
	for filename in fnmatch.filter(files, pattern):
		path = os.path.join(root, filename)
		print path
Python 2.7 documentation on os

Thursday, May 30, 2013

Concatenate files in python


cat folder/* > outfile

import fileinput
with open(outfilename, 'w') as fout:
for line in fileinput.input(filenames):
fout.write(line)