Commands tips & tricks

Infos Reseau

1
2
IP=$(ip -4 -o a | sed -e '/^2:/!d;s/^.*inet //;s/\/.*$//g')
HOSTNAME=$(hostname -s)

Modification des hosts et dns

1
[[ $(grep -E "$IP(.*)$(hostname -s)(.*)" /etc/hosts) ]] || echo -e "$IP    $(hostname -s)" >> /etc/hosts

Verification execution en root

1
[[ $(whoami) != root ]] && { echo "ERREUR : Veuillez exécuter ce script en tant que root avec la commande sudo !"; exit 1; }

Proxy

APT:

1
echo -e "Acquire::http::Proxy \"${PROXY_URL}/\";" > /etc/apt/apt.conf.d/proxy.conf

System:

1
2
3
4
5
PROXY_URL="http://USER:PASS@HOST:PORT"
echo -e "export http_proxy='${PROXY_URL}'\n
export https_proxy='${PROXY_URL}'\n
export no_proxy=EXCLUDED_DOMAIN.fr,localhost,127.0.0.1,${HOSTNAME}" > /etc/profile.d/proxy.sh
source /etc/profile.d/proxy.sh

Maven:

1
2
mkdir -p ~/.m2
echo ${http_proxy} | tr "/:@" " " | awk '{print "<settings>\n<proxies>\n<proxy>\n<id>proxy</id>\n<active>true</active>\n<protocol>"$1"</protocol>\n<host>"$4"</host>\n<port>"$5"</port>\n<username>"$2"</username>\n<password>"$3"</password>\n</proxy>\n</proxies>\n</settings>"}' > ~/.m2/settings.xml

Curl:

1
echo "proxy = ${http_proxy}" > ~/.curlrc

Decompresser à la volée

1
curl -s $PACKAGE_REGISTRY/stedolan/jq/1.6/jq-1.6.tar.gz | tar xz -C /usr/bin/ && chmod +x /usr/bin/jq

Add Iptables

1
iptables -A INPUT -p tcp -m tcp --dport 443 -j ACCEPT && iptables -A INPUT -p tcp -m tcp --dport 80 -j ACCEPT && iptables-save 2> /dev/null > /etc/sysconfig/iptables

Optimisation memoire Java (java heap size)

1
-Xmx2048m -Xms512m -XX:MaxPermSize=2048m -XX:+UseConcMarkSweepGC -XX:ReservedCodeCacheSize=128m -XX:+HeapDumpOnOutOfMemoryError

Changement de cible binaire

1
2
3
4
5
update-alternatives --set php /usr/bin/php5.6

Alias alternative from vars:
echo "[[ \${php} ]] && alias php='update-alternatives --set php /usr/bin/php\${php} > /dev/null; php' \
\n[[ \${node} ]] && alias node='nvm use \${node} >/dev/null; node'" >> ~/.bashrc

Manipulation de variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Variable avec nom dynamique (shell): 
varname="toto"
varcontent="content"
$varname=$varcontent

name=${varname}
data=${!varname}

Variable avec nom dynamique (Groovy):
def OC_CONFIG_PATH="OC_CONFIG_AA"
evaluate("env.${OC_CONFIG_PATH} = \"--config='/tmp/$OC_CONFIG_PATH/config'\" ")
env.OC_CONFIG_PATH="$OC_CONFIG_PATH"
OC_CONFIG_PATH=\${!OC_CONFIG_PATH}
env.KUBECONFIG="/tmp/AA/oc_config_${JOB_BASE_NAME}"

Sed tricks

1
2
3
4
5
change maven version
sed -i -r -e 's#<source>(.*)</source>#<source>1.8</source>#g' -e 's#<target>(.*)</target>#<target>1.8</target>#g' pom.xml

# Export 'exist*' groovy vars outside this shell
echo "\$(env | grep -E '_exist|_version' |sed 's/^/env./')" | tee ${WORKSPACE}/vars.groovy

Find tricks

1
2
find ${builder.buildDir}/ -name "*.sh" -exec chmod +x {} \\;
find ${builder.buildDir}/ -name "*.sh" -exec chmod 755 {} \\;

Import propre sql

1
mysql -u root -e "source /db.sql;"

Windows Startup applications

1
%AppData%\Microsoft\Windows\Start Menu\Programs\Startup

Systemd Authorize execution from alternative PATH

1
2
semanage fcontext -a -t bin_t "/data/pgsql/bin(/.*)?"
restorecon -RF /data/pgsql/bin

Tester la presence d’un executable:

1
command -v <BIN>

Pousser une commande en Background et la récupérer:

1
2
3
4
tail -f /dev/null 	#exemple
ctrl + z #stop le process
bg # le pousse en Background
fg # pour récupere le Process

Menu en Shell

Create Usage function

1
2
3
4
5
6
7
8
9
10
USAGE(){
echo "-e <env>, --env=<value> Précise l'environnement cible (--env=dev, --env=rec, --env=prod, etc)"
echo "-u, --uninstall Désinstalle le Chatbot"
echo "-d, --delete-volumes Supprime les données persistantes du Chatbot (peut être cumulé avec l'option --uninstall)"
echo "-p, --pull Télécharge seulement les images nécessaires au Chatbot"
echo "-h, --help Affiche cette page d'aide"
echo
echo "Si aucune option n'est précisée, le script lance un déploiement du Chatbot en mode interactif"
echo
}

Using Menu

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
OPTS=$(getopt --options "e:,h,u,d,p" --long "env:,help,uninstall,delete-volumes,pull" --name "$0" -- "$@")
[[ $? != 0 ]] && { echo "Echec à la lecture des options !" >&2 ; exit 1; }
eval set -- "$OPTS"

while true ; do
case "$1" in
-e | --env ) ENV="$2"; shift 2 ;;
-p | --pull ) DOWNLOAD_IMAGES; EXIT_STATUS=true; shift ;;
-u | --uninstall ) CLEAN_ENV; EXIT_STATUS=true; shift ;;
-d | --delete-volumes ) CLEAN_PERSISTENT_DATA; shift ;;
-h | --help ) USAGE; exit 1;;
-- ) shift; break ;;
* ) USAGE; exit 1;;
esac
done
[[ $EXIT_STATUS ]] && exit 0;

Importation certificat SSL (ubuntu/centos)

Importation certificat SSL (ubuntu/centos)

1
2
3
4
5
6
7
8
9
10
11
12
13
if [ "$(. /etc/os-release && echo "$ID")" = "ubuntu" ]; then
CERT_DIR="/usr/local/share/ca-certificates"
cp $APACHE_PATH/ssl/certs/cacert.pem $CERT_DIR/ca.crt
---- OR ----
openssl s_client -connect HOST:PORT -showcerts </dev/null 2>/dev/null | sed -e '/-----BEGIN/,/-----END/!d' | sudo tee -a $CERT_DIR/HOST.crt
update-ca-certificates --fresh
else
CERT_DIR="/etc/pki/ca-trust/source/anchors"
cp $APACHE_PATH/ssl/certs/cacert.pem $CERT_DIR/ca.crt
---- OR ----
openssl s_client -connect HOST:PORT -showcerts </dev/null 2>/dev/null | sed -e '/-----BEGIN/,/-----END/!d' | sudo tee -a $CERT_DIR/HOST.crt
update-ca-trust
fi