Mostrando postagens com marcador Shell Script. Mostrar todas as postagens
Mostrando postagens com marcador Shell Script. Mostrar todas as postagens

Zimbra mailbox usage report

A way to get an emailable report of all mailboxes in a domain, their quota, usage and account status, so a domain admin can review their accounts without having to login to zimbra. (most of this is the same as viewing quota usage in zimbra admin, but this was easier, as it can then be emailed on a schedule)

I saved this file in /opt/zimbra/backup/scripts/ as usagereport.sh, owned by zimbra.zimbra chmod 755 accountusage.sh

#!/bin/bash

SMTP="10.0.0.2"
FROM="user@yourdomain.com"
TO="user@yourdomain.com"
SUB="Mailbox Usages"
MSG="Mailbox Usages"

output="/tmp/accountusage.csv"
domain="yourdomain.com"
SendTo="user@yourdomain.com"

rm -f $output
touch $output

server=`zmhostname`
/opt/zimbra/bin/zmprov gqu $server|grep $domain|awk {'print $1" "$3" "$2'}|sort|while read line
do
usage=`echo $line|cut -f2 -d " "`
quota=`echo $line|cut -f3 -d " "`
user=`echo $line|cut -f1 -d " "`
#status=`/opt/zimbra/bin/zmprov ga $user | grep  ^zimbraAccountStatus | cut -f2 -d " "`

#with status
#echo "$user; `expr $usage / 1024 / 1024`MB; `expr $quota / 1024 / 1024`MB; ($status account)" >> $output

# without status
echo "$user; `expr $usage / 1024 / 1024`MB; `expr $quota / 1024 / 1024`MB" >> $output
done

#cat $output | mail @SendTo -s"Mailbox Usages for $domain"

sendemail -q -o tls=no -f $FROM -t $TO -u $SUB -m $MSG -s $SMTP:25 -a $output

Remove files 30+ days old

#!/bin/bash
# log and delete files 30 days old
FILENAME=FILES_$(date +%Y%m%d%H%M%m).log
find . -mtime +30 -exec echo {} \; > $FILENAME
rm -rfv $(cat $FILENAME)

Shell script input trick

In the occasion where you are not sure how long the user input might be and you want to capture it all, you would use $@ instead. In the following example, I took in the entire string and print it out word by word after breaking them into a string array according to the spaces in between.

In script run_this.sh:

userInputs=($@)
for i in "${userInputs[@]}";; do
  echo "$i"
done

Running the command:

./run_this.sh who knows how long this can go
who
knows
how
long
this
can
go



source: https://medium.freecodecamp.org/functional-and-flexible-shell-scripting-tricks-a2d693be2dd4


Remove comments and whitespaces with sed

pico /usr/bin/scrub
# Remove all comments and empty lines
sed -e ‘s/#.*//’ -e ‘s/[ ^I]*$//’ -e ‘/^$/ d’ $1

chmod  755 /usr/bin/scrub

pico /usr/bin/clrspace
# Remove all leading and trailing whitespace from each line;
sed ‘s/^[ t]*//;s/[ t]*$//’ $1

chmod  755 /usr/bin/clrspace

now you can simply run
scrub filename
clrspace filename


Clean up boot partition - Ubuntu


1. Check the current kernel version

$ uname -r

It will shows the list like below:

3.19.0-64-generic


2. Remove the OLD kernels

2.a. List the old kernel

$ sudo dpkg --list 'linux-image*'|awk '{ if ($1=="ii") print $2}'|grep -v `uname -r`

You will get the list of images something like below:
linux-image-3.19.0-59-generic
linux-image-3.19.0-61-generic
linux-image-3.19.0-65-generic
linux-image-extra-3.19.0-58-generic
linux-image-extra-3.19.0-59-generic
linux-image-extra-3.19.0-61-generic

2.b. Now its time to remove old kernel one by one as

$ sudo apt-get purge linux-image-3.19.0-58-generic
$ sudo apt-get purge linux-image-3.19.0-59-generic
$ sudo apt-get purge linux-image-3.19.0-61-generic
$ sudo apt-get purge linux-image-3.19.0-65-generic


When you're done removing the older kernels, you can run this to remove ever packages you won't need anymore:

$ sudo apt-get autoremove




Send desktop notification from shell scripts

Install packages
sudo apt-get install libnotify-bin

send some notification
notify-send "rsnapshot done :)"

#another example
...
alert=18000
live=$(lynx --dump http://money.rediff.com/ | grep 'BSE LIVE' | awk '{ print $5}' | sed 's/,//g;s/\.[0-9]*//g')
[ $notify_counter -eq 0 ] && [ $live -ge $alert ] && { notify-send -t 5000 -u low -i   "BSE Sensex touched 18k";  notify_counter=1; }

...

-t 5000: Specifies the timeout in milliseconds ( 5000 milliseconds = 5 seconds)
-u low : Set the urgency level (i.e. low, normal, or critical).
-i gtk-dialog-info : Set an icon filename or stock icon to display (you can set path as -i /path/to/your-icon.png).


A Note About GUI Tools and Cronjob

You need to request local display/input service using export DISPLAY=[user’s machine]:0 command if you are using cronjob to call your scripts. For example, call /home/vivek/scripts/monitor.stock.sh as follows which uses zenity tool:

@hourly DISPLAY=:0.0 /home/vivek/scripts/monitor.stock.sh

convert single line output to multiple line

$ips="10.26.208.28 10.26.208.26 10.26.208.27"

echo $ips | tr -s " " "\n"
or
echo $ips|xargs -n1

result
10.26.208.28
10.26.208.26
10.26.208.27

rsync

rsync

Incremental backup
rsync -av --delete /home/donato/ /backup/

Remove blank lines with grep


Remove blank lines ( new line ) with grep
echo -e "a\n\nb" | grep -v '^ *$'

or

grep -v '^[\s\t]*$' file.txt  > new_file.txt

Include Shell Script

Include Shell Script

File in same directory
source $( dirname $0 )/include-functions.sh

Manipulate dates with linux


Current formatted date
date +%d-%m-%Y

Current formatted date with time
date '+%d-%m-%Y %H:%M:%S'

1 day before the current date
date -d '-1 day' +%d-%m-%Y

1 day after the current date
date -d '+1 day' +%d-%m-%Y

First day of current month
date -d '-0 month' +01-%m-%Y

Last day of current month
date -d "-$(date +%d) days +1 month" +%d-%m-%Y

Last day of current month + one day
date -d "-$(date +%d) days +1 month +1 day" +%d-%m-%Y

First day of the previous month
date -d '-1 month' +01-%m-%Y

Last day of the previous month
date -d "-$(date +%d) days -0 month" +%d-%m-%Y

First day of the next month
date -d '+1 month' +01-%m-%Y

Last day of the next month
date -d "-$(date +%d) days +2 month" +%d-%m-%Y

Date format style /var/log/messages ( Aug 25 19:15:20 )
date '+%b %d %R:%S'

Concatenate variable shell script

  
VAR=abc; echo ${VAR}def

for i in {1..5}; do echo x${i}x; done

Comment line with sed

comment line starting with abc
sed -i '/^abc.*/s/^/#/' file

comment all lines in the file
sed -i '/^/s/^/#/' file

comment line starting with abc and append a comment on end of line
sed '/^abc.*/s/^/# /; //s/$/ comment/' file

Loop for shell script

loop for shell script

loop 1 until 10
for i in {1..10}; do echo $i; done

list all files in directory
for i in /etc/*; do echo $i; done

Replace char/word in shell script


substitute whitespace by commas to generate csv output

echo a b c | tr -s ' ' ','

Bandwidth control script for some ips


#!/bin/bash

BANDWIDTH_KBIT=7000
BANDWIDTH_INTERFACE=eth0
FINAL_IPs_WITH_BANDWIDTH_CONTROL=(7 8 50 111)
LAN_IP=$( ip addr show $BANDWIDTH_INTERFACE | grep -Eo '([0-9]{1,3}\.){3}' | sed -n 1p )

# clear all tc rules
tc qdisc del dev $BANDWIDTH_INTERFACE root

# create tc rules
tc qdisc add dev $BANDWIDTH_INTERFACE root handle 1: htb

for i in ${FINAL_IPs_WITH_BANDWIDTH_CONTROL[*]}; do
  tc class add dev $BANDWIDTH_INTERFACE classid 1:${i} htb rate ${BANDWIDTH_KBIT}kbit
  tc filter add dev $BANDWIDTH_INTERFACE parent 1: protocol ip prio 1 u32 match ip dst $LAN_IP${i} flowid 1:${i}
done

to list only machines that have traffic
tc -s -d class show dev eth0 | awk '$1 == "Sent" && $2 { print "\n"x; print } { x=$0 }'

to list only machines that have traffic order by traffic usage
tc -s -d class show dev eth0 | awk 'BEGIN{ ORS=" " } $1 == "Sent" && $2 { print "\n"x; print } { x=$0 }' | cut -d' ' -f1,3,9,10,29,30 | sort -k6n

Reboot machine if it can’t ping

#!/bin/bash
# reboot if ping test fails for 3 times

unset IVAR

for i in {1..3}; do
  ping -c 1 8.8.8.8 > /dev/null || ((IVAR++))
done

if [ "$IVAR" == 3 ]; then
  echo '[' $( date '+%d/%m/%Y %R' ) '] reboot' | tee -a /var/log/$( basename $0 ).log
  /sbin/init 6
fi

Block IP's by countries



#!/bin/bash
# block countries with iptables based on list with range of ips

# download list with range of countries ips
rm -rf /tmp/all-zones*; wget -nc http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz -P /tmp

mkdir /tmp/all-zones; tar -xzvf /tmp/all-zones.tar.gz -C $_

COUNTRIES_ISO_CODE_LIST=(af cu mo)

for country_iso_code in ${COUNTRIES_ISO_CODE_LIST[*]}; do

  for country_ip in $( cat /tmp/all-zones/$country_iso_code.zone ); do
    echo creating rules to $country_iso_code $country_ip
    /sbin/iptables -A INPUT -s $country_ip -m comment --comment "rule to $( echo $country_iso_code | tr '[:lower:]' '[:upper:]' ) country" -j DROP
  done
done

Creating columns bash


create 1 column
seq 10

create 2 columns
printf %s\\n ID-{1..3}' '{10..25..5}

create 3 or more columns
seq 12 | pr -3t | column -t

Get last field using cut

get last field using cut

echo 'www.google.com' | rev | cut -d'.' -f1 | rev

Adicionar registro DNS windows linha de comando

 Usando o Prompt de Comando (dnscmd)Abra o Prompt de Comando como Administrador e utilize a seguinte estrutura para adicionar um registro do...

Mais vistos