Skip to content

Instantly share code, notes, and snippets.

@rafamdr
rafamdr / user_data_debugging.txt
Created May 16, 2021 10:56
Get logs of AWS EC2 "user_data" script execution inside instance
# grep cloud-init /var/log/messages
# OR
# tail -f /var/log/messages | grep cloud-init
# Source: https://stackoverflow.com/questions/50255413/var-log-cloud-init-output-log-is-not-present-on-rhel-7-5
@rafamdr
rafamdr / whole_system_proxy.sh
Created May 16, 2021 10:49
Setting proxy for entire system (Centos7/Redhat7)
# set proxy config via /etc/profile.d - should apply for all users
# You can edit /etc/profile or add a script in /etc/profile.d directory which will be executed /etc/profile script
# After that, you can test executing "source /etc/profile"
PROXY_URL="http://10.102.45.9:8080/"
export http_proxy="$PROXY_URL"
export https_proxy="$PROXY_URL"
export ftp_proxy="$PROXY_URL"
export no_proxy="127.0.0.1,localhost"
@rafamdr
rafamdr / cmd_remote_server_sshpass.sh
Created April 6, 2021 13:49
Execute commands in many servers through ssh using sshpass (with password)
#!/bin/bash
trap "trap - SIGTERM && kill -- -$$" SIGINT SIGTERM EXIT
if [ ! "$BASH_VERSION" ] ; then
exec /bin/bash "$0" "$@"
fi
SERVERS=$(cat <<EOF
10.0.0.1
10.0.0.2
@rafamdr
rafamdr / install-pip-packages.sh
Created March 16, 2021 15:30
Install pip packages from tarballs or whl files
#!/bin/bash
set -e
DIRECTORY=$1
while read -r package; do
venv/bin/pip install "${package}" -f "$DIRECTORY"/ --no-index
done < requirements.txt
@rafamdr
rafamdr / download-pip-packages.sh
Created March 16, 2021 15:28
Download pip packages to create a bundle
#!/bin/bash
set -e
rm -rf pip_packages
mkdir pip_packages
while read -r package; do
venv/bin/pip download "${package}" -d pip_packages
done < requirements.txt
@rafamdr
rafamdr / ubuntu-apt-install-ssh-fix.sh
Created February 8, 2021 18:13
Solving "invoke-rc.d: initscript ssh, action "restart" failed."
# Setting up openssh-server (1:6.6p1-2ubuntu2.10) ...
# update-rc.d: warning: default stop runlevel arguments (0 1 6) do not match ssh Default-Stop values (none)
# invoke-rc.d: initscript ssh, action "restart" failed.
# dpkg: error processing package openssh-server (--configure):
# subprocess installed post-installation script returned error exit status 1
# Errors were encountered while processing:
# openssh-server
# E: Sub-process /usr/bin/dpkg returned an error code (1)
@rafamdr
rafamdr / ftp_recurs_get.sh
Created November 16, 2020 11:47
How to recursively download a folder via FTP on Linux
wget -r -nH --cut-dirs=5 -nc ftp://user:pass@server//absolute/path/to/directory
# -nH avoids the creation of a directory named after the server name
# -nc avoids creating a new file if it already exists on the destination (it is just skipped)
# --cut-dirs=5 allows to take the content of /absolute/path/to/directory and to put it in the directory where you launch wget. The number 5 is used to filter out the 5 components of the path. The double slash means an extra component.
# Source: https://stackoverflow.com/questions/113886/how-to-recursively-download-a-folder-via-ftp-on-linux#answer-5567776:~:text=Just%20to%20complement%20the%20answer%20given%20by%20Thibaut%20Barr%C3%A8re.
@rafamdr
rafamdr / sudo_mult_cmd_user.sh
Created November 5, 2020 18:51
Execute multiple commands under another linux user
sudo -u devil -- sh -c "whoami; cd ~/ && pwd"
# Expected result
# devil
# /some/hell/path/devil
@rafamdr
rafamdr / ssh_embed_creds.sh
Created November 5, 2020 18:40
Bash script to execute a command through SSH passing username/password as parameters
#!/bin/bash
#!/usr/bin/expect
if [ ! "$BASH_VERSION" ] ; then
exec /bin/bash "$0" "$@"
fi
echo -n "SSH User":
read -r sshusertemp
@rafamdr
rafamdr / gist:f52a1cd74c6bd03864ca0da3828da30f
Last active April 12, 2021 11:12
Command to extract content from hadoop gziped files
cat hadoop_file.bin | gzip -d