Skip to content

Instantly share code, notes, and snippets.

View matteomattei's full-sized avatar

Matteo Mattei matteomattei

View GitHub Profile
@matteomattei
matteomattei / bash_countdown.sh
Last active August 29, 2015 14:03
Simple countdown in Bash
#!/bin/bash
# A simple countdown in Bash
echo -n "countdown..."
for i in 1 2 3 4 5 6 7 8 9 10
do
echo -n -e "\rcountdown... ${i}"
sleep 1
done
echo -e "\nDONE!"
@matteomattei
matteomattei / backup_ftp_plesk_server.sh
Last active August 29, 2015 14:03
Backup last 7 days of httpdocs and MySQL data using FTP. This script is intended to be used in a server with Plesk.
#!/bin/bash
TMP_FOLDER="/root/daily_backup"
MYSQL_FOLDER="${TMP_FOLDER}/MYSQL"
WWW_FOLDER="${TMP_FOLDER}/WWW"
LOCAL_WWW_FOLDER="/var/www/vhosts"
FTP_USER="ftp_username"
FTP_PASS="ftp_password"
FTP_HOST="ftp_host"
MAIL_ALERT="notification@email.com"
@matteomattei
matteomattei / svn_stats.sh
Created July 8, 2014 07:47
How to calculate the number of inserted, deleted and modified lines in Subversion
#!/bin/bash
FIRST_REV=${1}
LAST_REV=${2}
REPO_ROOT=${3}
if [ -z "${FIRST_REV}" ] || [ -z "${LAST_REV}" ] || [ -z "${REPO_ROOT}" ]
then
echo "usage: ${0} first_revision last_revision repository_root"
exit 1
@matteomattei
matteomattei / phpsendmail.php
Last active August 29, 2015 14:03
How to log email sent from PHP through mail() function
#!/usr/bin/php
<?php
$sendmail = '/usr/sbin/sendmail';
$logfile = '/var/log/mail_php.log';
/* Get email content */
$logline = '';
$mail = '';
$fp = fopen('php://stdin', 'r');
@matteomattei
matteomattei / pyside_template_with_external_ui.py
Created July 8, 2014 08:00
PySide and QtDesigner template for applications
#!/usr/bin/python
# -*- coding: utf-8 -*-
from PySide.QtGui import *
from PySide.QtCore import *
from myapplication_ui import *
class MyApplication(QtGui.QMainWindow, Ui_MainWindow):
def __init__(self, parent=None):
@matteomattei
matteomattei / mysql_csv_export.php
Created July 8, 2014 08:09
Export MySQL table in CSV format using PHP
<?php
$link = mysql_connect($mysql_host,$mysql_user,$mysql_pass) or die('Could not connect: '.mysql_error());
mysql_select_db($mysql_db,$link) or die('Could not select database: '.$mysql_db);
$query = "SELECT * FROM $tablename ORDER BY id";
$result = mysql_query($query) or die("Error executing query: ".mysql_error());
$row = mysql_fetch_assoc($result);
$line = "";
$comma = "";
foreach($row as $name => $value)
@matteomattei
matteomattei / mysu.c
Created July 8, 2014 08:12
How to execute commands with specific user privilege in C under Linux
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
int main(int argc, char* argv[])
{
if(argc != 3)
{
printf("Usage: %s [USERNAME] [COMMAND]\n",argv[0]);
@matteomattei
matteomattei / mysu.py
Created July 8, 2014 08:14
How to execute commands with specific user privilege in Python under Linux
#!/usr/bin/env python
import sys,pwd,os
pw = pwd.getpwnam(sys.argv[1])
os.initgroups(sys.argv[1],pw.pw_gid)
env={"TERM":"xterm","USER":pw.pw_name,"HOME":pw.pw_dir,"SHELL":pw.pw_shell,"LOGNAME":pw.pw_name,"PATH":"/usr/bin:/bin:/opt/bin"};
os.setgid(pw.pw_gid);
os.setuid(pw.pw_uid);
os.execve(sys.argv[2],[],env);
@matteomattei
matteomattei / mac_x919.py
Created July 8, 2014 08:15
How to implement MAC_X919 algorithm in Python
#!/usr/bin/env python2
import Crypto.Cipher.DES as des
import Crypto.Cipher.DES3 as des3
def mac_x919(key,data):
while len(data) % 8 != 0:
data += '\x00'
des_key1 = des.new(key[0:8],des.MODE_CBC)
des_key2 = des.new(key[0:8],des.MODE_ECB)
@matteomattei
matteomattei / clone_mysql_schema.php
Last active August 29, 2015 14:03
How to clone MySQL database schema in PHP
<?php
/********************* START CONFIGURATION *********************/
$DB_SRC_HOST='localhost';
$DB_SRC_USER='root';
$DB_SRC_PASS='password';
$DB_SRC_NAME='database1';
$DB_DST_HOST='localhost';
$DB_DST_USER='root';