echo "export EDITOR=vim" >> ~/.bashrc && export EDITOR=vim
Wednesday, September 17, 2014
use vim as default editor
Tuesday, August 19, 2014
Modify all LDAP users in one shot
Once I need to reset login shell for all users. Here is easy solution, one simple loop
Of course, you can put instead of loginShell any other parameters. Don't forget, to change multiply parameters put '-' separator:
*****
echo "replace: userPassword" >> $LDIF_FILE
echo "userPassword: $PASSWD_SSHA" >> $LDIF_FILE
echo "-" >> $LDIF_FILE
echo "replace: sambaNTPassword" >> $LDIF_FILE
echo "sambaNTPassword: $sambaNTPassword" >> $LDIF_FILE
SAVEIFS=$IFS; IFS=$(echo -en "\n\b"); for i in $(ldapsearch -h localhost -p 389 -D "cn=Manager,dc=<yourdomain>,dc=com" -w "<Manager's passwd>" -s sub -b "ou=Users,dc=<yourdomain>,dc=com" | grep -v root | grep "dn:") do LDIF_FILE=/tmp/loginShell.ldif; echo "${i}" > $LDIF_FILE; echo "changetype: modify" >> $LDIF_FILE; echo "replace: loginShell" >> $LDIF_FILE; echo "loginShell: /usr/sbin/user_shell" >> $LDIF_FILE; /usr/bin/ldapmodify -h localhost -p 389 -D "cn=Manager,dc=<yourdomain>,dc=com" -w "<Manager's passwd>" -f $LDIF_FILE; done; IFS=$SAVEIFS;
Of course, you can put instead of loginShell any other parameters. Don't forget, to change multiply parameters put '-' separator:
*****
echo "replace: userPassword" >> $LDIF_FILE
echo "userPassword: $PASSWD_SSHA" >> $LDIF_FILE
echo "-" >> $LDIF_FILE
echo "replace: sambaNTPassword" >> $LDIF_FILE
echo "sambaNTPassword: $sambaNTPassword" >> $LDIF_FILE
Friday, August 15, 2014
Asterisk(Bash): watching agents on call/waiting in color
#!/bin/bash clear while true do buffer=$( clear /usr/sbin/asterisk -rx "show queue <My_Queue>" \ | grep Agent \ | grep -v \(Unavailable\) \ | sort -t"(" -k 2 \ | GREP_COLOR='01;31' egrep -i --color=always '^.*[0-9] \(Not in use.*$|$' \ | GREP_COLOR='01;32' egrep -i --color=always '^.*[0-9] \(Busy.*$|$' \ | GREP_COLOR='01;34' egrep -i --color=always '^.*\(paused.*$|$' ) echo "$buffer" sleep 2 done exit 0
Thursday, August 14, 2014
ZFS::chmod: changing permissions of Operation not permitted
Problem:
I can't change a folder permissions running on NFS client:chmod: changing permissions of <somedir>: Operation not permitted
But from ZFS/NAS server the command is working
Solutiom:
You need to add no_root_squash to your /etc/exports
But if you are running ZFS, you'll need to set
#sharemgr set -P nfs -p anon=0 zfs/data
Where zfs/data is name of your pool
Then you can check:
#sharemgr show -vp
Output:
default nfs=() zfs zfs/rpool nfs=() /rpool zfs/data nfs=(anon="0") /data
read more:
http://prefetch.net/blog/index.php/2009/05/05/using-sharemgr-to-manage-nfs-file-systems-on-opensolaris-hosts/
Monday, August 11, 2014
GREP: colored output
http://stackoverflow.com/questions/17236005/grep-output-with-multiple-colors
EXAMPLE: Colored output from asterisk CLI
EXAMPLE: Colored output from asterisk CLI
asterisk -rvvvvvvv | GREP_COLOR='01;36' egrep --color=always '^.*answered SIP.*$|$' | GREP_COLOR='01;31' egrep -i --color=always '^.*Got SIP response.*$|$' | grep -E '(answered SIP|Got SIP response)'
Thursday, August 7, 2014
BASH Shell: For Loop File Names With Spaces
http://www.cyberciti.biz/tips/handling-filenames-with-spaces-in-bash.html
BASH for loop works nicely under UNIX / Linux / Windows and OS X while working on set of files. However, if you try to process a for loop on file name with spaces in them you are going to have some problem. For loop uses $IFS variable to determine what the field separators are. By default $IFS is set to the space character. There are multiple solutions to this problem.
BASH for loop works nicely under UNIX / Linux / Windows and OS X while working on set of files. However, if you try to process a for loop on file name with spaces in them you are going to have some problem. For loop uses $IFS variable to determine what the field separators are. By default $IFS is set to the space character. There are multiple solutions to this problem.
Set $IFS variable
Try it as follows:#!/bin/bash SAVEIFS=$IFS IFS=$(echo -en "\n\b") for f in * do echo "$f" done IFS=$SAVEIFSOR
#!/bin/bash SAVEIFS=$IFS IFS=$(echo -en "\n\b") # set me FILES=/data/* for f in $FILES do echo "$f" done # restore $IFS IFS=$SAVEIFS
More examples using $IFS and while loop
Now you know that if the field delimiters are not whitespace, you can set IFS. For example, while loop can be used to get all fields from /etc/passwd file:.... while IFS=: read userName passWord userID groupID geCos homeDir userShell do echo "$userName -> $homeDir" done < /etc/passwd
Using old good find command to process file names
To process the output of find with a command, try as follows:find . -print0 | while read -d $'\0' file do echo -v "$file" doneTry to copy files to /tmp with spaces in a filename using find command and shell pipes:
find . -print0 | while read -d $'\0' file; do cp -v "$file" /tmp; done
Processing filenames using an array
Sometimes you need read a file into an array as one array element per line. Following script will read file names into an array and you can process each file using for loop. This is useful for complex tasks:#!/bin/bash DIR="$1" # failsafe - fall back to current directory [ "$DIR" == "" ] && DIR="." # save and change IFS OLDIFS=$IFS IFS=$'\n' # read all file name into an array fileArray=($(find $DIR -type f)) # restore it IFS=$OLDIFS # get length of an array tLen=${#fileArray[@]} # use for loop read all filenames for (( i=0; i<${tLen}; i++ )); do echo "${fileArray[$i]}" done
Playing mp3s with spaces in file names
Place following code in your ~/.bashrc file:mp3(){ local o=$IFS IFS=$(echo -en "\n\b") /usr/bin/beep-media-player "$(cat $@)" & IFS=$o }Keep list of all mp3s in a text file such as follows (~/eng.mp3.txt):
/nas/english/Adriano Celentano - Susanna.mp3 /nas/english/Nick Cave & Kylie Minogue - Where The Wild Roses Grow.mp3 /nas/english/Roberta Flack - Kiling Me Softly With This Song.mp3 /nas/english/The Beatles - Girl.mp3 /nas/english/John Lennon - Stand By Me.mp3 /nas/english/The Seatbelts, Cowboy Bebop - 01-Tank.mp3To play just type:
$ mp3 eng.mp3.txt
Saturday, August 2, 2014
bash:: tolowercase,to uppercase
http://stackoverflow.com/questions/2264428/converting-string-to-lower-case-in-bash-shell-scripting
To lowercase$ string="A FEW WORDS" $ echo ${string,} a FEW WORDS $ echo ${string,,} a few words $ echo ${string,,[AEIUO]} a FeW WoRDS $ string="A Few Words" $ declare -l string $ string=$string; echo $string a few words
To uppercase$ string="a few words" $ echo ${string^} A few words $ echo ${string^^} A FEW WORDS $ echo ${string^^[aeiou]} A fEw wOrds $ string="A Few Words" $ declare -u string $ string=$string; echo $string A FEW WORDS
Toggle (undocumented)$ string="A Few Words" $ echo ${string~~} a fEW wORDS $ string="A FEW WORDS" $ echo ${string~} a fEW wORDS $ string="a few words" $ echo ${string~} A Few Words
Capitalize (undocumented)$ string="a few words" $ declare -c string $ string=$string $ echo $string A few words
Title case:$ string="a few words" $ string=($string) $ string=${string[@]^} $ echo $string A Few Words $ declare -c string $ string=(a few words) $ echo ${string[@]} A Few Words
Saturday, July 26, 2014
BASH: running script with argument equal value ( ./script.sh VAR="value" VAR2="value")
This is an example:
Now you run your script as follow:
How this works? That's easy!
while [ $# -gt 0 ] - Do, till we are running out of arguments. If we invoke ./script.sh one two three, then our arguments will be:
$0 = name of script ('./script.sh')
$1 = 'one'
$2 = 'two'
$3 = 'three'
On the end of each iteration we cast shift, which removes the first element from arguments array, but not the element's number. Argument numbers will be shifted. So at first cycle $1 will be equal 'one', but at the second iteration $1 will be equal 'two'. Got it?
case ${1%%=*} - In case if the first part (before '=' ) of our argument $1 is equal to (below). If we invoke ./script.sh MESSAGE="sometext", then $1 will be equal 'MESSAGE="sometext"'. Then respectively:
$1 = 'MESSAGE="sometext"'
${1} = 'MESSAGE="sometext"'
${1%%=*} = 'MESSAGE'
${1#*=} = "sometext"
Example of usage function:
#Parse argument while [ $# -gt 0 ]; do case ${1%%=*} in MESSAGE|message) MESSAGE=${1#*=} ;; SPANS|spans) SPANS=${1#*=} ;; NUMBERS|numbers) NUMBERS=${1#*=} ;; ATTEMPT|attempt) ATTEMPT=${1#*=} ;; VERBOSE|verbose) VERBOSE=${1#*=} ;; REPEAT|repeat) REPEAT=${1#*=} ;; *) usage exit 1 ;; esac shift done
Now you run your script as follow:
./script.sh MESSAGE="message" SPANS="spans" NUMBERS="numbers" ....
How this works? That's easy!
while [ $# -gt 0 ] - Do, till we are running out of arguments. If we invoke ./script.sh one two three, then our arguments will be:
$0 = name of script ('./script.sh')
$1 = 'one'
$2 = 'two'
$3 = 'three'
On the end of each iteration we cast shift, which removes the first element from arguments array, but not the element's number. Argument numbers will be shifted. So at first cycle $1 will be equal 'one', but at the second iteration $1 will be equal 'two'. Got it?
case ${1%%=*} - In case if the first part (before '=' ) of our argument $1 is equal to (below). If we invoke ./script.sh MESSAGE="sometext", then $1 will be equal 'MESSAGE="sometext"'. Then respectively:
$1 = 'MESSAGE="sometext"'
${1} = 'MESSAGE="sometext"'
${1%%=*} = 'MESSAGE'
${1#*=} = "sometext"
P.S.
Example of usage function:
usage() { echo "Usage:" echo echo "$0 MESSAGE=\"message\" SPANS=\"spans\" NUMBERS=\"numbers\" [ATTEMPT=\"attempt\"] [VERBOSE=\"verbose\"] [REPEAT=\"repeat\"]" echo "Defaults: ATTEMPT=always VERBOSE=3 REPEAT=1" echo "Example: $APP_NAME MESSAGE=\"hello\" SPANS=auto NUMBERS=\"135xxxxxxxx,136xxxxxxxx\"" echo }Look as well at: http://tldp.org/LDP/abs/html/refcards.html#AEN22664
Friday, July 25, 2014
Postfix: controlling attachment size limits
One of the famous problems, if you're building up a corporate email server, might be an email attachment size, that is allowed for your users to send as emails. On newbie's questions on forums most of gurus are pointing to policyd. Obviously, this plugin is more than amazing, works more than perfect and pretty fast as well. But.. It wont control the attachment size. It's plugin "Quotas" is only to control the amount of sent messages and consumed bandwidth, but not to control a single message size.
NOTE: MessageCumulativeSize will never block your first message with attachment, even it's size is exceeding the limits of hundred times! The first message will always go through. Read this: https://www.mail-archive.com/users@lists.policyd.org/msg01962.html
So this is not what I expected. I'd like to have a simple plugin, that BLOCKS the message sending, if it's size is larger, than I would allow. And I came up with this simple path.
Go to /usr/lib64/policyd-2.0/cbp/modules/ or wherever your cbpolicyd files stored and modify Quotas.pm or make a patch like this (name it Quotas.pm.patch):
NOTE: MessageCumulativeSize will never block your first message with attachment, even it's size is exceeding the limits of hundred times! The first message will always go through. Read this: https://www.mail-archive.com/users@lists.policyd.org/msg01962.html
So this is not what I expected. I'd like to have a simple plugin, that BLOCKS the message sending, if it's size is larger, than I would allow. And I came up with this simple path.
Go to /usr/lib64/policyd-2.0/cbp/modules/ or wherever your cbpolicyd files stored and modify Quotas.pm or make a patch like this (name it Quotas.pm.patch):
[root@server modules]# cat Quotas.pm.patch
--- Quotas.pm.orig 2014-07-25 21:01:38.332494503 -0400 +++ Quotas.pm 2014-07-25 22:24:08.297785096 -0400 @@ -101,7 +101,6 @@ # stage # if ($sessionData->{'ProtocolState'} eq "RCPT") { - # Key tracking list, if quotaExceeded is not undef, it will contain the msg my %newCounters; # Indexed by QuotaLimitsID my @trackingList; @@ -414,7 +413,24 @@ if (lc($limit->{'Type'}) eq "messagecumulativesize") { # Bump up counter my $currentCounter = $qtrack->{'Counter'} + $sessionData->{'Size'}; - + +#-------------------------------# Added by SHIRKER + # $server->maillog("DEBUG Quota TESTTTTT START!! Quota=%s MessageSize=%s", + # $limit->{'CounterLimit'}, + # $sessionData->{'Size'}); + + if ($sessionData->{'Size'} > $limit->{'CounterLimit'}){ + # $server->maillog("DEBUG Quota TESTTTTT DONE!! Quota=%s MessageSize=%s" Verdict=%s", + # $limit->{'CounterLimit'}, + # $sessionData->{'Size'}, + # $quota->{'Verdict'}); + + # Set verdict + $verdict = $quota->{'Verdict'}; + } +#-------------------------------# END added by Shirker +# +# # Update database my $sth = DBDo(" UPDATEThen apply it:
patch Quotas.pm < Quotas.pm.patch
Saturday, May 31, 2014
OPENVPN working configuration
1) I have server inside a local network with eth0 192.168.0.14/24
2) A cheap modem with IP XX.XX.XX.166 and NAT 8460 port to 192.168.0.14
3) VPN network will be 172.26.0.0/24
We gonna use NAT since we cant add a static route to main gateway:
Windows openvpn client(172.26.0.6) <==>172.26.0.1 => NAT => 192.168.0.14
In general, my server configuration has been done according to this article: http://habrahabr.ru/post/194144/
2) A cheap modem with IP XX.XX.XX.166 and NAT 8460 port to 192.168.0.14
3) VPN network will be 172.26.0.0/24
We gonna use NAT since we cant add a static route to main gateway:
Windows openvpn client(172.26.0.6) <==>172.26.0.1 => NAT => 192.168.0.14
In general, my server configuration has been done according to this article: http://habrahabr.ru/post/194144/
/etc/openvpn/server.conf:
[root@gsm-gateway html]# cat /etc/openvpn/server.conf local 192.168.0.14 port 8460 proto tcp-server dev tun ca /etc/openvpn/easy-rsa/2.0/keys/ca.crt cert /etc/openvpn/easy-rsa/2.0/keys/server.crt key /etc/openvpn/easy-rsa/2.0/keys/server.key dh /etc/openvpn/easy-rsa/2.0/keys/dh1024.pem server 172.26.0.0 255.255.255.0 persist-key persist-tun ifconfig-pool-persist ipp.txt route 172.26.0.0 255.255.255.0 client-to-client push "route 192.168.0.0 255.255.255.0" push "redirect-gateway def" push "dhcp-option DNS 8.8.8.8" push "dhcp-option DNS 8.8.4.4" push "dhcp-option DNS 208.67.222.222" push "dhcp-option DNS 208.67.220.220" keepalive 10 120 comp-lzo max-clients 100 user openvpn group openvpn persist-key persist-tun status /var/log/openvpn/openvpn-status.log log-append /var/log/openvpn/openvpn.log verb 3 tun-mtu 1500 tun-mtu-extra 32
/etc/sysconfig/iptables:
[root@gsm-gateway html]# cat /etc/sysconfig/iptables # Generated by iptables-save v1.4.7 on Sun Jun 1 14:09:37 2014 *filter :INPUT ACCEPT [0:0] :FORWARD ACCEPT [0:0] :OUTPUT ACCEPT [2253:276759] -A INPUT -m state --state RELATED,ESTABLISHED -j ACCEPT -A INPUT -p icmp -j ACCEPT -A INPUT -i lo -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 22 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 8460 -j ACCEPT -A INPUT -p tcp -m state --state NEW -m tcp --dport 80 -j ACCEPT -A INPUT -i tun0 -j ACCEPT -A INPUT -j REJECT --reject-with icmp-host-prohibited -A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT -A FORWARD -s 172.26.0.0/24 -d 192.168.0.0/24 -i tun0 -o eth0 -m conntrack --ctstate NEW -j ACCEPT -A FORWARD -i tun0 -j ACCEPT -A FORWARD -j REJECT --reject-with icmp-host-prohibited COMMIT # Completed on Sun Jun 1 14:09:37 2014 # Generated by iptables-save v1.4.7 on Sun Jun 1 14:09:37 2014 *nat :PREROUTING ACCEPT [7656:717493] :POSTROUTING ACCEPT [2631:164557] :OUTPUT ACCEPT [2631:164557] -A POSTROUTING -s 172.26.0.0/24 -o eth0 -j MASQUERADE COMMIT # Completed on Sun Jun 1 14:09:37 2014
Client (Windows 7) server.ovpn:
client dev tun proto tcp-client remote XX.XX.XX.166 8460 resolv-retry infinite nobind tun-mtu 1500 tun-mtu-extra 32 mssfix 1450 persist-key persist-tun ca ca.crt cert client1.crt key client1.key auth-user-pass comp-lzo reneg-sec 0 verb 5
Run windows client as administrator!!!
Windows Username/Password annoying prompt
To disable Username/Password prompt read this thread https://forums.openvpn.net/topic11342.html
Enough said:
1. Create a txt file on a folder alongside your .ovpn files name it what ever you want ex. 'pass.txt'
2. Put your user/pass inside the file in two(2) lines, like:
3. Then save it.
4. open up your .ovpn file in notepad and add:
5. save and connect to your openvpn server.
2. Put your user/pass inside the file in two(2) lines, like:
Code:
username
password
password
3. Then save it.
4. open up your .ovpn file in notepad and add:
Code:
auth-user-pass pass.txt
5. save and connect to your openvpn server.
Sunday, May 18, 2014
VIM: Disable automatic comment insertion
Sometimes vim does weird things, like when you copy/paste stuff, that contained a line started with comment, then vim ALL next inserted line will put with comment in front...
this post is about how to get rid of it:
http://vim.wikia.com/wiki/Disable_automatic_comment_insertion
OR
create .vimrc file, it its not exists yet and add there following line:
this post is about how to get rid of it:
http://vim.wikia.com/wiki/Disable_automatic_comment_insertion
OR
create .vimrc file, it its not exists yet and add there following line:
autocmd FileType * setlocal formatoptions-=c formatoptions-=r formatoptions-=o
Monday, April 7, 2014
Bash: disk space alert
There are millions scripts and utilities for that.. Here is my input:
Replace /usr/local/sbin/php_mailer to your favorite Linux mailer
add to cron:
* */1 * * * /usr/local/sbin/diskspace_alert 80
where 80 - is the max percentage value
[root@gsm-gateway ~]# cat /usr/local/sbin/diskspace_alert #!/bin/bash ALERT=0 MAX_VALUE=$1 for i in `df -h | tr -s ' '| cut -d' ' -f5 | grep -oP "\d{1,3}?(?=%)"` do [ "$i" -ge "$MAX_VALUE" ] && ALERT=$((ALERT+1)) && DF_OUTPUT=$(df -h) done [ "$ALERT" -gt 0 ] && /usr/local/sbin/php_mailer "The disk space is nearly full" "$DF_OUTPUT"
Replace /usr/local/sbin/php_mailer to your favorite Linux mailer
add to cron:
* */1 * * * /usr/local/sbin/diskspace_alert 80
where 80 - is the max percentage value
Wednesday, April 2, 2014
Sending emails via gmail with php
This script developed to send notifications (as well html and attachment) to server admin through gmail account:
#!/usr/bin/php <?php /* Please make sure, you have installed following pear modules: pear install Mail pear install Net_SMTP pear install Mail_mime pear install Mail_mimeDecode IMPORTANT: Works only with utf-8 charset!!! */ $mailru_user = "YOUR_USER@gmail.com"; $mailru_passwd = "YOUR_PASS"; $to = '<TO_SERVER_ADMIN@gmail.com>'; require_once "Mail.php"; require_once('Mail/mime.php'); if(empty($argv[1]))exit("USAGE: $argv[0] 'Hello, how are you?' 'some text' '<h1>some html text</h1>' '/tmp/attachment.txt'"); function mail_via_gmail($subject,$text,$html,$file){ global $mailru_user; $from = '<'.$mailru_user.'>'; global $mailru_passwd; global $to; $crlf = "\r\n"; if( empty($to) ){ echo 'Please specify $to parameter'; exit(); } if( empty( $subject ) ) $subject = ''; if( empty( $text ) ) $text = ''; if( empty( $html ) ) $html = ''; if( empty( $file ) ) $file = ''; $headers = array( 'From' => $from, 'To' => $to, 'Subject' => $subject, 'Content-Type' => 'text/html; charset=UTF-8' ); $mime_params = array( 'text_encoding' => '7bit', 'text_charset' => 'UTF-8', 'html_charset' => 'UTF-8', 'head_charset' => 'UTF-8' ); $mime = new Mail_mime($crlf); $mime->setTXTBody($text); $mime->setHTMLBody($html); $mime->addAttachment($file,'application/octet-stream'); $body = $mime->get($mime_params); $headers = $mime->headers($headers); $smtp = Mail::factory('smtp', array( 'host' => 'ssl://smtp.gmail.com', 'port' => '465', 'auth' => true, 'username' => $mailru_user, 'password' => $mailru_passwd )); $mail = $smtp->send($to, $headers, $body); if (PEAR::isError($mail)) { echo('<p>' . $mail->getMessage() . '</p>'); } else { echo("Message successfully sent!\n"); } } @mail_via_gmail($argv[1],$argv[2],$argv[3],$argv[4]); ?>
Friday, January 24, 2014
My own fail to ban script for asterisk
This is script /usr/local/sbin/block_sip_ip.sh. It has some options, but i doubt if you really need all of them
you can just add it to your crontab:
*/3 * * * * /usr/local/sbin/block_sip_ip.sh -n 1000
where
-n 1000 - amount of las lines of asterisk log to parse. You need to adjust that part accordingly of intensity of your calls. by default its only 200 lines.
-q - how many attempts allowed before IP will be banned. 3 by default
you can just add it to your crontab:
*/3 * * * * /usr/local/sbin/block_sip_ip.sh -n 1000
where
-n 1000 - amount of las lines of asterisk log to parse. You need to adjust that part accordingly of intensity of your calls. by default its only 200 lines.
-q - how many attempts allowed before IP will be banned. 3 by default
#!/bin/bash # checking, is someone is getting "wrong password" in last 200 lines of asterisk log: DEBUG=0 # email to notify about banned IPs ADMIN_EMAIL="admin@domain.com" # our internal network. First part only LOCAL_NET="10.0.0" # our own external IP: OWNIP1="111.221.211.111" LINES=200 INFO="Use: $0 -l /PATH/TO/LOG -q ATTEMPTS -n LINES" while getopts ":dl:q:n:" optname do case "$optname" in "d") DEBUG=1 echo "debugging is on" ;; "n") LINES=$OPTARG ;; "l") LOG_FILE=$OPTARG ;; "q") ATTEMPTS=$OPTARG ;; "?") echo "Unknown option $OPTARG" echo $INFO exit 0 ;; ":") echo "No argument value for option $OPTARG" echo $INFO exit 0 ;; *) echo "Unknown error while processing options" exit 0 ;; esac done if [ -z "$LOG_FILE" ] then LOG_FILE="/var/log/sip_hackers_ips" fi [ "$DEBUG" -eq 1 ] && echo "logfile : $LOG_FILE" [ "$DEBUG" -eq 1 ] && echo "lines : $LINES" if [ -z "$ATTEMPTS" ] then ATTEMPTS=2 fi [ "$DEBUG" -eq 1 ] && echo "failed ext. allowed : $ATTEMPTS" for i in `tail -$LINES /var/log/asterisk/full | grep -v $LOCAL_NET | grep -E '(Wrong password| failed for )' | grep -Eo '(([0-9]{1,3}\.){3}[0-9]{1,3})' | sort | uniq | grep -v "$OWNIP1"` do # checking amount of attempted extensions EXT_CNT=$(tail -$LINES /var/log/asterisk/full | grep -oc "$i") # if its geater than 3, block it!! if [ "$EXT_CNT" -gt "$ATTEMPTS" ]; then IPTABLES_CNT=$(/sbin/iptables -nvL | grep -c $i); if [ "$IPTABLES_CNT" -eq 0 ]; then [ "$DEBUG" -eq 1 ] && echo "To BLOCK: $i"; echo -n "[`date`]">> $LOG_FILE echo $i >> $LOG_FILE /sbin/iptables -I INPUT -s $i -j DROP /sbin/iptables -A OUTPUT -d $i -j DROP [ "$DEBUG" -eq 1 ] && echo "IP $i has been blocked" echo "VPBX: $i has been blocked" | mail -s "VPBX: $i has been blocked" $ADMIN_EMAIL fi fi done
Saturday, January 18, 2014
freepbx moving all .conf.old files back to original
Hi, all
Once by mistake I recompiled all .conf files from asterisk source, and some freepbx files were moved into <FILE>.conf.old. So this is the script to change it back :
mv <FILE>.conf to <FILE>.conf_orig
mv <FILE>.conf.old mv <FILE>.conf
just go to /etc/asterisk
and run:
for i in `ls | grep "\.old$"`;do echo "${i%.old} will be saved as ${i%.old}_orig"; mv ${i%.old} ${i%.old}_orig; echo " moving $i to ${i%.old}" ; mv $i ${i%.old};done
Once by mistake I recompiled all .conf files from asterisk source, and some freepbx files were moved into <FILE>.conf.old. So this is the script to change it back :
mv <FILE>.conf to <FILE>.conf_orig
mv <FILE>.conf.old mv <FILE>.conf
just go to /etc/asterisk
and run:
for i in `ls | grep "\.old$"`;do echo "${i%.old} will be saved as ${i%.old}_orig"; mv ${i%.old} ${i%.old}_orig; echo " moving $i to ${i%.old}" ; mv $i ${i%.old};done
Thursday, January 16, 2014
How to merge wav files in linux for asterisk IVR
For example, for lazy admins, its possible to combine some asterisk system sounds into IVR. This is how to merge them and insert a pause. And on the end - sending to specified email for checking:
# Making first part. After it we need a pause: wavmerge good-morning.wav thank-you-for-calling.wav # Renaming from default mv -f merge.wav merge1.wav # Inserting pause in 1 second: sox merge1.wav "|sox our-business-hours.wav -p pad 1" merge2.wav # Adding more files wavmerge merge2.wav 9.wav hours.wav 12.wav hours.wav 13.wav hours.wav 18.wav hours.wav and.wav day-1.wav day-5.wav # Renaming from default mv -f merge.wav merge3.wav # Inserting pause in 1 second: sox merge3.wav "|sox this-call-may-be-monitored-or-recorded.wav -p pad 1" merge1_4.wav # Sending by email: echo "This is latest recording" | mutt -a "merge1_4.wav" -s "new recordimg out" -- youremail@gmail.com
Monday, January 13, 2014
cbpolicy email notifier
I come up with a script, that parses maillog and sends email notification to specified admin's email. take a look:
#!/bin/bash # Usage: # cbpolicy_notifier <LINES TO PARSE> <MAIL TO> LINES=$1 MAIL_TO=$2 if [ -z "$2" ] || [ ! -z "$3" ];then echo "" echo "ERROR: $0 requires number munber of lines to parse and valid email to notify you" echo "" echo "Usage: $0 <LINES TO PARSE> <MAIL TO>" echo "" exit fi EMAILMESSAGE="/tmp/cbpolicy_notifier_`date +%Y-%m-%d_%H-%M`.txt" HOST_NAME=$(hostname) MAIL_FROM=postmaster@$HOST_NAME for i in `tail -$LINES /var/log/maillog | grep cbpolicy | grep reject | grep -v "from=root@" | grep "track=Sender" | grep -oP '(?<=from=).*?(?=,)' | uniq`; do echo $i V_EMAIL=$i MSG_SUBJECT_ADMIN="$HOST_NAME::Outbound email quota was exceeded for $V_EMAIL" MSG_SUBJECT_USER="WARNING::Your outbound email hourly quota was exceeded" echo "We have encountered outbound email overlimiting for $V_EMAIL" > $EMAILMESSAGE echo "" >> $EMAILMESSAGE LOG_SAMPLE=$(tail -$LINES /var/log/maillog | grep cbpolicy | grep reject | grep -v "from=root@" | grep $V_EMAIL ) echo "This is the log sample" >> $EMAILMESSAGE echo "" >> $EMAILMESSAGE echo "$LOG_SAMPLE" >> $EMAILMESSAGE /bin/mailx -r "$MAIL_FROM" -s "$MSG_SUBJECT_ADMIN" "$MAIL_TO" < $EMAILMESSAGE echo "Dear customer." > $EMAILMESSAGE echo "" >> $EMAILMESSAGE echo "Your hourly outbound email quota was overlimited. Please reduce amount of emails you are sending out." >> $EMAILMESSAGE /bin/mailx -r "$MAIL_FROM" -s "$MSG_SUBJECT_USER" "$V_EMAIL" < $EMAILMESSAGE doneadd this to cron, just specify num of lines to parse and email. Like this:
*/15 * * * * /usr/local/sbin/cbpolicy_notifier 500 support@example.com
monitor HDD script via hpacucli
I have written some useful script to monitor HDD on HP server via hpacucli and send email notification
To find amount of disks, run
Then count them
[root@hp-proliant]# cat /usr/local/sbin/hpacucli_mon #!/bin/bash # Usage: # hpacucli_mon <NUM OF ARRAYS> <NUM OF PHISYCAL DISKS> <MAIL TO> HP_SLOT=`/usr/sbin/hpacucli ctrl all show status | grep -o -P 'Slot.{0,2}'| awk -F" " '{print $2}'` if [ "$3" = "" ] || [ "$4" != "" ];then echo "" echo "ERROR: hpacucli_mon requires number of arrays, disks and valid email" echo "" echo "Usage: hpacucli_mon <NUM OF ARRAYS> <NUM OF PHISYCAL DISKS> <MAIL TO>" echo "" echo "To find amount of arrays and disks you do have run:" echo "/usr/sbin/hpacucli ctrl slot=$HP_SLOT ld all show status" echo "and" echo "/usr/sbin/hpacucli ctrl slot=$HP_SLOT pd all show status" exit fi EMAILMESSAGE="/tmp/hpacucli_message.txt" LOCAL_IP=`/sbin/ifconfig eth0 | grep -Eo '(([0-9]{1,3}\.){3}[0-9]{1,3})' | grep -v ".255"` MSG_SUBJECT="Smart HP array failure at $LOCAL_IP" OK_ARRAY_CNT=`/usr/sbin/hpacucli ctrl slot=$HP_SLOT ld all show status | grep -o "OK" | wc -l` OK_DISKS_CNT=`/usr/sbin/hpacucli ctrl slot=$HP_SLOT pd all show status | grep -o "OK" | wc -l` if [ "$OK_ARRAY_CNT" -ne $1 ] || [ "$OK_DISKS_CNT" -ne $2 ]; then echo "We have encountered a problem at $LOCAL_IP" > $EMAILMESSAGE echo "Take look at this: " >> $EMAILMESSAGE /usr/sbin/hpacucli ctrl slot=$HP_SLOT ld all show status >> $EMAILMESSAGE /usr/sbin/hpacucli ctrl slot=$HP_SLOT pd all show status >> $EMAILMESSAGE echo "===============================================" >> $EMAILMESSAGE echo "INFORMATION PROVIDED BY SMARTCTL:" >> $EMAILMESSAGE echo "" >> $EMAILMESSAGE for (( i=0; i<$2; i++ )) do /usr/sbin/smartctl -a -d cciss,$i /dev/cciss/c0d0 | grep -E '(Serial|Health)' >> $EMAILMESSAGE echo "" >> $EMAILMESSAGE done mail -s "$MSG_SUBJECT" "$3" < $EMAILMESSAGE fiTo use this, just add to crontab:
00 */1 * * * /usr/local/sbin/hpacucli_mon <NUM OF LOGICAL HDD> <NUM OF PHYSICAL HDD> <YOUR@EMAIL>
To find amount of disks, run
export HP_SLOT=$(/usr/sbin/hpacucli ctrl all show status | grep -o -P 'Slot.{0,2}'| awk -F" " '{print $2}') && /usr/sbin/hpacucli ctrl slot=$HP_SLOT ld all show status && /usr/sbin/hpacucli ctrl slot=$HP_SLOT pd all show status
Then count them
howto debug SIP and RTP
Low level debug:
tcpdump -A host 188.232.87.xxx
High level debug:
asterisk -r
CLI> sip set debug ip 188.232.87.xxx
RTP debug:
tcpdump -n dst portrange 10000-20000
tcpdump -A host 188.232.87.xxx
High level debug:
asterisk -r
CLI> sip set debug ip 188.232.87.xxx
RTP debug:
tcpdump -n dst portrange 10000-20000
Subscribe to:
Posts (Atom)