Skip to content

Instantly share code, notes, and snippets.

View Dih5's full-sized avatar

Guillermo Hernández Dih5

  • Universidad de Salamanca
  • Salamanca, Spain
View GitHub Profile
@Dih5
Dih5 / whitelistFeed.sh
Created December 9, 2015 13:05
Script to whitelist hosts in hosts.allow using the feed of a gmail account
#!/bin/bash
# Script to whitelist hosts for remote access using gmail's https atom feed.
# Update the vars defined below and chmod 700 root:root, since the password is in clear text.
# Use cron to run it periodically.
#
# To add a host to the whitelist send an email to the GMAILUSER account with a subject like: "allow 13.13.13.13" (whithout quotes) using any of the accounts in ALLOWED
# To remove a host from whitelist read or delete the corresponding email. Note the feed shows only unread mail.
#
# The script works by updating hosts.allow. It checks the feed for mails like those described above.
# The whitelist stored in hosts.allow, between 2 markers (START FEEDSCRIPT, END FEEDSCRIPT), which should not be removed
@Dih5
Dih5 / battery_charge.sh
Last active December 10, 2015 09:12
Display colored battery charge. Useful for shell prompts.
#!/bin/bash
show_help() {
cat << EOF
Usage: ${0##*/} [-hz] [-b NUM]
Print level of battery charge (1-100) formatted with colors.
-h display this help and exit.
-b NUM print a NUM bars instead of a number, e.g. '▸▸▸▸▸▸▹▹▹▹'.
-z use zsh colors instead of bash. Remember to autoload -U colors && colors in .zshrc.
@Dih5
Dih5 / cpfluka.sh
Created March 14, 2016 17:26
Copies some FLUKA-relevant files from originDir into destinationDir, keeping the directory structure as seen from pwd.
#!/bin/bash
if [ "$#" -eq "2" ]; then
find $1 -type f \( -name '*.lis' -o -name '*.inp' -o -name '*.bnn' -o -name '*.bin' -o -name '*.flair' \) -exec install -D {} `readlink -f $2`/{} \;
else
echo -e "Usage: cpfluka originDir destinationDir\nCopies some FLUKA-relevant files from originDir into destinationDir, keeping the directory structure as seen from pwd.";
fi
@Dih5
Dih5 / cpuuse.sh
Last active March 18, 2016 08:16
Check the CPU use of the system, including number of threads in use
#!/bin/bash
CPUUSE=`top -b -n2 | grep "Cpu(s)" | tail -1 | sed -e 's/[^0-9]*\([0-9][0-9]*[\.\,][0-9]\).*/\1/'`
CORES=`cat /proc/cpuinfo | grep -i 'processor' | wc -l`
USEDCORES=`echo "scale=1;$CPUUSE * $CORES / 100" | bc`
echo CPU used: $CPUUSE% Threads: $USEDCORES/$CORES
@Dih5
Dih5 / ForEachServer.sh
Created March 17, 2016 10:57
Run a command in a list of servers. (Note there are better tools to do this like pssh pdsh, ...)
#!/bin/bash
#Server list:
declare -a arr=("server1" "server2" "server3")
#Command to execute:
cmd="./cpuuse.sh"
for i in "${arr[@]}"
do
echo "***Connecting to $i***"
ssh "$i" "$cmd"
@Dih5
Dih5 / commit-msg
Created January 11, 2017 12:39
commit-msg hook for git. Checks some format conventions.
#!/usr/bin/env python
# Put in .git/hooks/commit-msg and chmod +x it.
from __future__ import print_function
import sys
def fail():
print("Commit aborted.")
sys.exit(1)
@Dih5
Dih5 / humanorder.py
Last active January 12, 2017 08:51
Key function to sort in human order
import re
def human_order_key(text):
"""Key function to sort in human order."""
# Adapted from http://nedbatchelder.com/blog/200712/human_sorting.html
return [int(c) if c.isdigit() else c for c in re.split(r'(\d+)', text)]
@Dih5
Dih5 / mailer.py
Created February 9, 2017 10:33
Python mailing script
# Example of mailing script. Quick and (a little) dirty.
# Dih5 - 2017
# Spam is not cool. Thou shalt not spam!
import smtplib
import email
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
@Dih5
Dih5 / CStyle.xml
Created March 29, 2017 08:22
My C style for Eclipse
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<profiles version="1">
<profile kind="CodeFormatterProfile" name="Dih5" version="1">
<setting id="org.eclipse.cdt.core.formatter.insert_space_after_opening_paren_in_exception_specification" value="do not insert"/>
<setting id="org.eclipse.cdt.core.formatter.insert_space_after_opening_paren_in_parenthesized_expression" value="do not insert"/>
<setting id="org.eclipse.cdt.core.formatter.insert_space_before_closing_paren_in_for" value="do not insert"/>
<setting id="org.eclipse.cdt.core.formatter.insert_space_before_closing_brace_in_array_initializer" value="do not insert"/>
<setting id="org.eclipse.cdt.core.formatter.insert_space_before_opening_paren_in_for" value="insert"/>
<setting id="org.eclipse.cdt.core.formatter.indent_statements_compare_to_body" value="true"/>
<setting id="org.eclipse.cdt.core.formatter.insert_space_after_colon_in_base_clause" value="insert"/>
@Dih5
Dih5 / html_escape.py
Created July 4, 2017 09:56
Python method to escape html using entities
from html.entities import codepoint2name
def html_escape(text):
d = dict((chr(code), '&%s;' % name) for code, name in codepoint2name.items() if code!=38) # exclude "&"
text = text.replace("&", "&amp;")
for key, value in d.items():
text = text.replace(key, value)
return text