Alpha
Share and Organize Code
login register
HotNewTop
F.A.Q.
1. To begin adding code first register.
2. Create your own tags for code that you add.
3. To see your own code click Me, to see all code click Everyone, or filter by people rob.
4. To follow a user, click on their username, rob and hit follow.
5. Add other code to your library by clicking the
button near the code titles (note that you can not edit these codes).
6. To create a copy of a code that you can edit use fork.
7. If you can't find your code, double check your filter settings as you can filter by Category, User and Tag simultaneously.
8. Add comments to a code by clicking on it's title or the comments link underneath it.

Monitor Who's on Server (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#!/bin/bash
# --- Script: /usr/local/bin/monitor-who
# --- Author: Paul Greene
# --- Objective: Monitor who's on local server (NODE1) and a remote server (NODE2), from local
# --- server and log it on NODE1
# --- PRE-REQUISISTE: SSH key pair access NEEDS to be set up between NODE1 and NODE2
WHONODE1=`/usr/local/bin/security-who`
WHONODE2=`ssh -p 22 monitor@192.168.1.11 exec /bin/bash -s "$LE_PATH" < /usr/local/bin/security-who`
echo "NODE1: $WHONODE1" | /usr/local/bin/timestamp | tee -ai /var/log/monitor-security-who.log
echo "NODE2: $WHONODE2" | /usr/local/bin/timestamp | tee -ai /var/log/monitor-security-who.log

#!/bin/bash
# --- Script: /usr/local/bin/timestamp
# --- Author: Paul Greene
# --- Objective: Act as a timestamp for log output
while read line ; do
    echo "$(date): ${line}"
done

#!/bin/bash
# --- Script: /usr/local/bin/security-who
# --- Author: Paul Greene
# --- Objective: Capture who's on the server
who -u --lookup

# Enjoy!
[+] (22) more...
comments (1) flag fork

Network Test (plain)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#!/bin/bash
# --- Script: /usr/local/sbin/commtest
# --- Permissions: Ensure you execute 'chmod 0700 commtest'
# --- Author: Paul Greene
# --- Objective: Verify ports are open to or on your server for applications to run
# --- Notes: This will simply output PASS or FAILED. Ensure nmap is installed.

# --- Verify Network communication is open to your MySQL Database
echo ""
echo "TEST 1 - Verify Network communication is open to DB"
portchk=`nmap -v -p 3306 localhost | awk '{print $2}' | awk 'NR==6'`
if [ "$portchk" = "open" ]; then
        echo "MySQL port 3306 is open to web server ...PASSED"
else
        echo "MySQL port 3306 is closed to web server ...FAILED"
fi
[+] (12) more...
comments (1) flag fork

Creating custom validation class in CI (plain)

1
2
3
4
5
6
7
8
9
10
class MY_Form_validation extends CI_Form_validation {

    function oneof($str, $values)
    {
        return in_array($str, explode(',', $values)) ? TRUE : FALSE;
    }
}

//you will need to make this entry in language files as well
$lang['oneof'] = "The %s field has an invalid option.";
[+] (6) more...
comments (1) flag fork

Get all counts of counts (plain)

1
2
3
SELECT c.count, count(*) FROM
(SELECT count(*) as count FROM category GROUP BY id) as c
GROUP BY c.count
comments (0) flag fork

Get all records for this year (plain)

1
SELECT * FROM table WHERE  YEAR(recDate) =  YEAR(CURDATE()) AND MONTH(recDate) = MONTH(CURDATE());
comments (0) flag fork

Get all records for this month (plain)

1
SELECT * FROM table WHERE DATE_FORMAT(recDate, '%Y%m') = DATE_FORMAT(NOW(), '%Y%m');
comments (0) flag fork

SELECT, GROUP BY CONCAT, then UPDATE (plain)

1
UPDATE foodservice_recipes SET ingredients=(SELECT GROUP_CONCAT(CONCAT(CONCAT(CONCAT(CONCAT(amount,' '),item),' '),quantity)) AS ingredients FROM foodservice_ingredients WHERE recipe_id=1) WHERE id=1;
comments (0) flag fork

Add and revoke privileges (plain)

1
2
3
4
5
GRANT ALL PRIVILEGES ON *.* TO '<user>'@* IDENTIFIED BY '<password>' WITH GRANT OPTION;

GRANT SELECT,INSERT,UPDATE,DELETE,CREATE,RELOAD,PROCESS,FILE,REFERENCES,INDEX,ALTER ON *.* TO '<user>'@'<ip>' IDENTIFIED BY '<password>';

REVOKE SELECT,INSERT,UPDATE,DELETE,CREATE,RELOAD,PROCESS,FILE,REFERENCES,INDEX,ALTER ON *.* FROM '<user>'@'<ip>';
[+] (1) more...
comments (0) flag fork

Import using socket (plain)

1
mysql --socket=/var/lib/mysqld/mysqld1.sock -u root < /home/dump.sql
comments (0) flag fork

Init new database on socket (plain)

1
mysql_install_db --user=mysql --ldata=/var/lib/mysql1
comments (0) flag fork

Connecting on socket (plain)

1
mysql --socket=/var/lib/mysqld/mysqld1.sock -u root -p<password>
comments (0) flag fork

Kill running query (plain)

1
mysql -h localhost -u username -p -e "KILL Id#"
comments (0) flag fork

Show running queries (plain)

1
mysql -h localhost -u username -p -e "SHOW PROCESSLIST"
comments (1) flag fork

Count Unique Visits (plain)

1
2
# Change the access.log name accordingly to match your server
awk '{ print $1 }' access.log | sort | uniq | wc -l
comments (1) flag fork

Undo last push (plain)

1
git push -f origin HEAD^:master
comments (0) flag fork

Undo last commit (plain)

1
2
3
4
5
$ git commit ...
$ git reset --soft HEAD^
$ edit                        
$ git add ....              
$ git commit -c ORIG_HEAD
[+] (1) more...
comments (0) flag fork

Remove/add origin (plain)

1
2
git remote rm origin
git remote add origin git@github.com:SWEngineer/sample_app.git
comments (0) flag fork

Delete tag (plain)

1
git tag -d <tag name>
comments (0) flag fork

Create tag (plain)

1
git tag -a v1.1.4 <hash code> -m “message”
comments (0) flag fork

Delete branch (plain)

1
git branch -D <branch name>
comments (0) flag fork

Name branch from sha1 (plain)

1
git branch <branch name> <sha1>
comments (0) flag fork

Rename a branch (plain)

1
git branch -m <branch 1> <branch 2>
comments (0) flag fork

Copying remote images using CURL (plain)

1
2
3
4
5
6
7
8
$ch = curl_init('http://www.domain.com/path/to/image');
$fp = fopen('/local/path//upload/tn/'.$image_id.'.jpg', 'w');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_exec($ch);
curl_close($ch);
fclose($fp);
[+] (4) more...
comments (0) flag fork

List Only Directories (plain)

1
2
3
4
# This examples list all the directories in /var/log/
tree -d /var/log
# OR type this to remove any indentations
tree -di /var/log
comments (0) flag fork

List Directories & Files (plain)

1
2
3
4
5
6
7
8
ls    # list dir / files
ls -lah | more    # list all files in list / human readable format
ls -d    # list directories only
ls -lR    # list files / dir in list format recursively
ls -lahRG    # list all files / dirs, list format, recursively, with colour format (if colours not already set in environment variables)
ls -lah | sort -bn +4    # list files by size smallest to largest
ls -lah | sort -bnr +4    # list files by size largest to smallest
ls -lahG *.txt    # list only .txt files (with other options mentioned above)
[+] (4) more...
comments (1) flag fork

Show Used HDD Space (plain)

1
df -h
comments (0) flag fork

Kill Process (plain)

1
2
3
4
5
6
7
8
9
# You can see the top processes by typing: top -c
# Then kill a process based on process ID:
kill 15647
# OR multiple processes
kill 1567 3245 57654

# If the process refuses to die you can force it by...
# This will stop whatever it's doing, not recommended unless necessary
kill -9 15647
[+] (5) more...
comments (1) flag fork

Recursively Search for a String in Multiple Directories & Files (plain)

1
2
3
# Don't run this from the / directory... it would be really taxing on the server
cd /home
find . | xargs grep -i some_pattern
comments (1) flag fork

GZip Compress File (plain)

1
gzip file.txt
comments (1) flag fork

Analyze SELECT Statement (plain)

1
2
/* Just add EXPLAIN in front of SELECT... */
EXPLAIN SELECT * FROM db.table;
comments (0) flag fork