Bash Commands

From GoBlueMich Wiki
Jump to navigation Jump to search

For Loop

  • For with sed -i to output to beginning of file
 for LIST in `cat robots.list`; do sed -i '1i Disallow:' $LIST && sed -i '1i User-agent: Mediapartners-Google' $LIST; done
  • For loop for all cPanel users
 for USER in `ls -lhs  /var/cpanel/users | awk '{print $10}'| tr -d '.|/'| sed '/^$/d'`; do CMD $USER ; done
  • Check if SA is enabled for each cPanel account
 for USER in `ls -lhs  /var/cpanel/users | awk '{print $10}'| tr -d '.|/'| sed '/^$/d'`; do if [ -f /home/$USER/.spamassassinenable ]; then echo "SA ENABLED $USER"; else echo "SA DISABLED $USER";fi;done| sort
  • Dig all domains on server
for DOMAIN in `cat /etc/userdatadomains | awk -F":" '{print $1}'`; do dig "$DOMAIN" | grep -E "^$DOMAIN.*\b(A|CNAME).*\..*\..*" ; echo ""; done


cPanel - SuPHP


  • Find all robots.txt with user-agent
 find /hom*/* -type f -name robots.txt |xargs grep -i "User-agent: Mediapartners-Google"

  • Find and log all php.ini memory limits


Set variable:

 PHPLOG='/root/phpini.list.'$(date +%Y%m%d)'.log'Find and log:
 echo "Writing to "$PHPLOG && echo 'Global\n' > $PHPLOG && echo '/usr/local/lib/php.ini'>> $PHPLOG && grep -i  memory_limit /usr/local/lib/php.ini >> $PHPLOG && echo '\nLocal\n' >> $PHPLOG && for LINI in `find /hom*/* -type f -name php.ini`; do echo '\n'$LINI >> $PHPLOG && grep -i  memory_limit $LINI >> $PHPLOG && echo '\n' >> $PHPLOG; done && less $PHPLOG

egrep for string

 find /hom*/*/public_html -type f -exec egrep -Howi "db_name" "{}" + ;


  • Toggle cPHulk via CLI

Hostname Page

  • Output to hostname web page
 cmd > /usr/local/apache/htdocs/log.txt


  • Strace by pid
 strace -Ff -s512 -Tt -o out -p 24687

  • Strace called script
 strace -Ff -s512 -Tt -o out php

Encrypt File


  • A simple way to encrypt a single file is with openssl:
  openssl des-ede3-ofb < youfile.txt > yourfile.txt.des-ede3-ofb

NOTE: This will prompt you for a passphrase, which you will need to enter later when decrypting the file.

  • To decrypt, run the following and enter the passphrase.
  openssl des3 -d < yourfile.txt.des-ede3-ofb > yourfile.txt.decrypted



Check Bandwidth of Email User

 grep /var/log/maillog{,.1,.2,.3} | grep -E -o 'size\=[^,]*' | awk -F\= '{ SUM += $2} END {print SUM/1024/1024"MB"}'

List cPanel Users

  • List all cPanel Users
 ls -lhs  /var/cpanel/users | awk '{print $10}'| tr -d '.|/'| sed '/^$/d'

Removes "./"

 tr -d '.|/'

Removes blank lines

 sed '/^$/d'

Create Load

  • Run below to create load - please note it can take down servers
 dd if=/dev/zero of=/dev/null &
  • Kill PID to stop process
 kill $PID


If Statement

 <If "%{REMOTE_HOST} == ''">
   Redirect 302 /

Regex Examples

  • Remove all subdomains from list of domains
egrep -v "^[[:alnum:]]+\.+[[:alnum:]]+\.[[:alnum:]]+"

Add Double Quotes to Date/Time

Take date in format below and adds double quotes at start and end

jfkla,4404,mario-test_latest,2016-9-30 12:8:46

changes to

jfkla,4404,mario-test_latest,"2016-9-30 12:8:46"


  • Using sed
sed -r -i'.lwbkup.ndilernia' 's/[0-9]{4}\-((0?[1-9]|1[0-2])){1}-(0?[1-9]|[1-2][0-9]) [0-9]*:[0-9]*:[0-9]*$/"&"/' -i example.file
  • Adding "-i'.lwbkup.ndilernia'" to sed creates a backup file before making any changes. With the above command, example.file would now have the double quotes around the date/time but example.file.lwbkup.ndilernia will be a copy of the original.


  • Using awk
for line in $(cat example.file); do echo -n "$line"|awk -F "," '{if ($4~/([0-9]|0[0-9]|1[0-9]|2[0-3]):[0-5][0-9]*$/) print $1","$2","$3",""\""$4"\""; else print $0 }'; done