Category: CLI and tools
-
SSH in for loop (consuming stdin / using file descriptor 3)
Ever found yourself using a for/while loop using SSH in the loop, only to find out your loop only runs once? Your stdin is captured by ssh… This can be fixed.
-
Friendly (ionice/nice) rsync on remote server
Add –rsync-path=”ionice -c 3 nice rsync” to your rsync command (on sending side) Friendly for both sender and receiver: ionice -c3 nice rsync –rsync-path=”ionice -c 3 nice rsync”
-
Upgrade dumb shell to interactive shell (tty)
python -c ‘import pty; pty.spawn(“/bin/bash”)’ yay python
-
bash add IP in PWD to variable for easy reference
Add to .bashrc: export PROMPT_COMMAND=”${PROMPT_COMMAND:+$PROMPT_COMMAND ;} ip=\$( echo \$PWD | grep -oE ‘[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}’ ) ” Now if you browse to/in a directory which has an ipv4 ip, the IP will be stored in the $ip variable.
-
Easy AutoRecon summary (bash)
echo “===SUMMARY===”;grep . */report/notes.txt| while read line; do IP=$( echo -ne $line|cut -d’/’ -f1| tr -d ‘\n’ ); [[ x$IP != x$PREVIP ]] && echo; echo -n $IP ; echo -n $line | cut -d\] -f2- | grep –color=always ‘ .* found’ ; PREVIP=$IP; done; echo
-
Proxychains on fedora / centos7
Clone the repo: ./configure && make && sudo make install cp src/proxychains.conf /etc/proxychains.conf && vi /etc/proxychains.conf echo “alias p=’/usr/local/bin/proxychains4′” >> ~/.bashrc && . .bashrc p ssh xx@ip
-
Bash while loop input (stdin reading from while-input) fix
while read line <&3; do echo “==== $line ====”; yes y | sqlmap -u http://$line/ –crawl=1; read -p press\ enter; done 3< webservers.ip Using input 3 to read your list, leaving stdin untouched.
-
Bash array operations and examples
Split to array: while IFS=’;’ read -ra ADDR; do for i in “${ADDR[@]}”; do # process “$i” done done <<< “$IN” Define: distro=(“redhat” “debian” “gentoo”) Element: ${ArrayName[subscript]} Length: echo “${#distro[@]}” Example !/bin/bash define array name server names FQDN NAMESERVERS=(“ns1.nixcraft.net.” “ns2.nixcraft.net.” “ns3.nixcraft.net.”) get length of an array tLen=${#NAMESERVERS[@]} use for loop read all nameservers for ((…
-
Bash color variables
# Reset Color_Off=’\033[0m’ # Text Reset # Regular Colors Black=’\033[0;30m’ # Black Red=’\033[0;31m’ # Red Green=’\033[0;32m’ # Green Yellow=’\033[0;33m’ # Yellow Blue=’\033[0;34m’ # Blue Purple=’\033[0;35m’ # Purple Cyan=’\033[0;36m’ # Cyan White=’\033[0;37m’ # White # Bold BBlack=’\033[1;30m’ # Black BRed=’\033[1;31m’ # Red BGreen=’\033[1;32m’ # Green BYellow=’\033[1;33m’ # Yellow BBlue=’\033[1;34m’ # Blue BPurple=’\033[1;35m’ # Purple BCyan=’\033[1;36m’ #…
-
Tail: prepend file name to tailing output (bash)
cd /opt/arcsight/connectors/; tail -f *_con1_rsyslog_paloalto_2500*/current/logs/agent.out.wrapper.log | awk ‘/^==> / {a=substr($0, 5, length-8); next} {print a”:”$0}’
-
Personal (manual) template for internal hosts
This might help someone else, too. These are the things I do for an internal VM (on my hypervisor at home). This document will/might evolve over time. Default OS is centos7. yum -y install epel-release && yum -y update && yum -y install wget vim htop tcpdump yum-cron yum-utils ntp figlet lynis bind-utils bash-completion mlocate…
-
Bash suppress all output of script (echo off – style)
exec 1>/dev/null 2>/dev/null at the top of your script
-
Bash compound commands (()){{}}{}[[]]
Compound Commands A compound command is one of the following. In most cases a list in a command’s description may be separated from the rest of the command by one or more newlines, and may be followed by a newline in place of a semicolon. (list) list is executed in a subshell environment (see COMMAND…
-
Run programs without execute rights on file
/bin/bash script.sh /lib64/ld-linux-x86-64.so.2 binary.bin /usr/bin/php file.php
-
Bash keyboard shortcuts
Found here: http://ss64.com/bash/syntax-keyboard.html Bash Keyboard Shortcuts Moving the cursor: Ctrl + a Go to the beginning of the line (Home) Ctrl + e Go to the End of the line (End) Ctrl + p Previous command (Up arrow) Ctrl + n Next command (Down arrow) Alt + b Back (left) one word Alt + f…
-
Self extracting zipped encrypted linux package
#!/bin/bash T=`mktemp -d /tmp/$$XXXX`;trap “rm -rf $T” EXIT SIGINT; tail -n+3 $0|openssl enc -d -aes-256-cbc|tar zx -C $T;C=`pwd`; cd $T;./_;cd $C;exit 0 build: #!/bin/bash find . -name “*~” -delete TMP=`mktemp -d /tmp/$$.XXXXX` trap “rm -rf $TMP” EXIT # Create payload tar -czf $TMP/payload –exclude=*~ ./* cd .. #?? DST=./packages/`date +%Y%m%d`/sysprep.run mkdir `dirname $DST` ( tail…
-
Bash percentage calculate snippet
MEMLINE=$( tac $MGRBASE/$REL_LOGLOC/server.status.log | egrep -m1 ‘\*\*.*Memory Status:’ ) USED=$( echo “$MEMLINE” | egrep -o ‘[0-9\.,]+ MB Used’ | sed ‘s/\..*//;s/,//’ ) MAX=$( echo “$MEMLINE” | egrep -o ‘[0-9\.,]+ MB Max’ | sed ‘s/\..*//;s/,//’ ) PERC=$( printf “%.0f” $(echo “scale=2;($USED/$MAX)*100″ | bc) ) if [[ $PERC -gt $MEM_CRIT_PERC ]]; then ERRORS=”$ERRORS memory ${PERC}%” elif [[…
-
Bash file age check snippet
if [[ -f $BACKUP_LOCKFILE ]]; then #Check age if [[ $(( $( date +%s ) – $(stat -c %Y $BACKUP_LOCKFILE ) )) -gt $BACKUP_MAXLENGTH_SECS ]]; then echo “Backup takes too long” else echo “Doing backup” fi fi