Skip to content

Instantly share code, notes, and snippets.

View ltpitt's full-sized avatar
🥋
A black belt is a white belt that never quit.

Davide Nastri ltpitt

🥋
A black belt is a white belt that never quit.
View GitHub Profile
@ltpitt
ltpitt / calculateDatetimeDeltaHours.js
Last active February 27, 2017 13:11
Javascript function that calculates the difference between two datetime in hours
/**
* Calculates the difference between two datetime in hours
* @param {String} Current datetime (any format compatible with Date)
* @param {String} Start datetime (any format compatible with Date)
* @return {Integer} The difference between Current datetime and Start datetime in hours
*/
function calculateDatetimeDeltaHours(datetimeCurrent, datetimeStart) {
var datetimeCurrent = new Date(datetimeCurrent);
var datetimeStart = new Date(datetimeStart);
var deltaHours = Math.abs(datetimeCurrent - datetimeStart) / 36e5
@ltpitt
ltpitt / countOccurrenciesInString.js
Last active February 27, 2017 13:09
Javascript function that counts occurrencies of a substring in a string
/** Function that counts occurrencies of a substring in a string;
* @param {String} string The string
* @param {String} subString The sub string to search for
* @param {Boolean} [allowOverlapping] Optional. (Default:false)
* @return {Integer} Total count of occurrencies
*/
function occurrencesCount(string, subString, allowOverlapping) {
string += "";
subString += "";
if (subString.length <= 0) return (string.length + 1);
@ltpitt
ltpitt / download-latest-code-from-git.sh
Last active February 27, 2017 12:59
Git commands to download latest version of your code from repository
# Reset current HEAD to the specified HEAD state
git reset --hard HEAD
# Fetch from the remote repository and integrate with local branch
git pull
@ltpitt
ltpitt / git-commit.sh
Last active March 11, 2020 04:13
Git commands commit your code to your repository
# Check current status
git status
# Add changed files (or all files using *)
git add yourfilename
# Check if everything was added correctly
git status
# Perform the commit
git commit -m "Insert your commit comment here"
# Push to repo
git push
@ltpitt
ltpitt / miniGrep.js
Last active August 14, 2024 04:35
Javascript function that mimics basic GNU/Linux grep command
/** Function that mimics basic GNU/Linux grep command;
* @param {String} multiLineString The multiline string
* @param {String} patternToSearch The RegEx pattern to search for
* @return {Array} An Array containing all the matching row(s) for patternToSearch in multiLineString
*/
function miniGrep(string, patternToSearch) {
var regexPatternToSearch = new RegExp("^.*(" + patternToSearch + ").*$", "mg");
match = string.match(regexPatternToSearch);
return match;
}
// Password change timestamp
var int_pass_rotation = "2017-03-06";
var lastPasswordUpdateDate = new Date(int_pass_rotation);
console.log(lastPasswordUpdateDate);
// GET CURRENT DATE
var now = new Date();
console.log(now);
@ltpitt
ltpitt / create-python-cli-tool.sh
Created April 24, 2017 12:50
Easily create python command line tools using cookie cutter templates
# You need pipsi or pip to proceed. Once that is sorted...
pip install cookiecutter.
# Go in preferred folder
cd ~/Desktop
# Start cookiecutter using python cli tool template
cookiecutter https://github.com/ltpitt/cookiecutter-python-cli
@ltpitt
ltpitt / check-os.sh
Created April 24, 2017 12:55
Function to check Distribution and Version of *nix Operating Systems
# OS check function
os_check ()
{
OS=`uname -rs`
if [ "`uname -rs | grep 'Linux 2.6.9'`" ]
then
OS=RHEL_4
elif [ "`uname -rs | grep 'Linux 2.6.18'`" ]
then
@ltpitt
ltpitt / pip-reinstall.sh
Created April 26, 2017 13:49
Force pip reinstall of a specified tool
# Reinstall Python package present in the current folder
sudo pip install . --upgrade --force-reinstall
@ltpitt
ltpitt / reverse_tunnel_ssh.sh
Last active June 27, 2017 07:33
Reverse tunnel ssh on Linux
#!/bin/sh
# Forward only to loopback interface of the server
ssh -fN -R 10022:CLIENTIPTOFORWARD:22 SERVER
# Forward to all interfaces
# In this case it is mandatory to add
# GatewayPorts yes
# to /etc/ssh/sshd_config
ssh -R \*:3389:WINDOWSCLIENT:3389 -N [email protected]