Skip to content

Instantly share code, notes, and snippets.

@kozmonaut
kozmonaut / mongo-generate-data.php
Last active December 21, 2015 05:19
Some sample data for mongoDB.
<?php
// Define connection and name of database/collection for storing data
try{
$mongo = new Mongo();
$db = $mongo->myfirstdb;
$collection = $db->samples;
}catch(MongoConnectionException $check){
die('Database connection failed!'. $check->getMessage());
@kozmonaut
kozmonaut / mongoDB-logging.php
Last active June 16, 2018 16:27
Example of how to use mongoDB for storing apache access logs
<?php
// Initialize connection
try{
$mongo = New Mongo();
$db = $mongo->test;
$collection = $db->logs;
;
} catch (MongoConnectionException $check){
die ('Error msg: '. $check->getMessage());
@kozmonaut
kozmonaut / pyhash.py
Created April 8, 2014 09:27
Text hashing (sha1,md5,sha256)
#!/usr/bin/env python2.7
import hashlib
algorithms = dict(
md5 = lambda s: hashlib.md5(s).hexdigest(),
sha1 = lambda s: hashlib.sha1(s).hexdigest(),
sha256 = lambda s: hashlib.sha256(s).hexdigest(),
)
@kozmonaut
kozmonaut / makefile
Created April 8, 2014 13:40
Socket: Makefile example
all: client.o server.o
client.o: client-beta.c poruke.h
gcc client-beta.c -o client
server.o: server-beta.c
gcc server-beta.c -o server
clean:
rm server-beta.c server.exe client-beta.c client.exe poruke.h
@kozmonaut
kozmonaut / rsa-keys.py
Created April 14, 2014 10:47
Extraction of RSA keys
#Extract RSA keys in separate files
"""
Example of usage:
1. Generate your RSA keys
# openssl genrsa 2048 > mykeys.key
2. Read keys in human way and save it to a new .txt file
# openssl rsa -in mykeys.key -noout -text
3. Exclude keys in separate files with rsa-keys.py script
"""
@kozmonaut
kozmonaut / hash-sha265.py
Created April 14, 2014 11:29
Hashing with SHA256
#!/usr/bin/env python3.0
import hashlib
import sys
algorithm = dict(
sha256 = lambda x: hashlib.sha256(x).hexdigest()
)
def usage():
@kozmonaut
kozmonaut / linux-packages
Created April 14, 2014 11:44
Backup and restore installed packages inside Linux Debian
# Backup your packages list
# Get a packages list
dpkg --get-selections > ~/Package.list
# Copy list of repositories
sudo cp /etc/apt/sources.list ~/sources.list
# Export repo keys
sudo apt-key exportall > ~/Repo.keys
@kozmonaut
kozmonaut / linux-system-info
Last active January 15, 2021 16:28
Extract Linux system data using command line
Time: /bin/date
Login: users | awk '{print $1}'
Architecture: uname -m
Kernel version: uname -v -r
Processor: cat /proc/cpuinfo | grep \"name\" | cut -d : -f2 | uniq
Operating system: cat /etc/*-release | grep \"PRETTY_NAME\" | cut -d \"=\" -f2 | uniq
Interfaces: ip link show | grep 'eth\|wlan' | awk '{print $1,$2}'
IP addresses: ip a | grep 'eth\|wlan' | awk '{print $1,$2}'
Transmitted: /sbin/ifconfig eth0 | grep 'TX bytes' | awk '{print $7,$8}'
Recieved: /sbin/ifconfig eth0 | grep 'RX bytes' | awk '{print $3,$4}'
@kozmonaut
kozmonaut / euclidean-algorithm.py
Last active January 15, 2021 16:22
Euclidean algorithm - Find a greatest common divisor
def gcd(a,b): # Define function with two parameters
while b != 0: # Run loop till 'b' value is 0
if a > b: # 'a' value must be larger than 'b'
r = a % b # Remainder value
a = b # Assign 'b' to 'a'
b = r # Asign remainder value to 'b'
return a # Return result
@kozmonaut
kozmonaut / weather-api.py
Created January 30, 2015 08:03
Use OpenWeatherMap API with Python
import urllib
import json
# Define url you wanna fetch
url = "http://api.openweathermap.org/data/2.5/find?q=London&units=metric"
# Fetch url
response = urllib.urlopen(url)
# Load url response to json