Category Archives: Shell

sh/bash/zsh/csh/tcsh/ash

Specific bash and tool features for working with quotes

Magic dollar

Try to make
user$ x='\n' && echo $x
\n
the same if you use double-quotes, how to output new line ?

user$ x=$'\n' && echo $x

and you get new line! — magic $''

Hex in commands

Try to replace «asd to 'dsa in file
user$ sed -i 's/\»asd/\'dsa/g' file
> ^C
but shell will try to open quotes and it get 3 qoutes, of course it ask you to close one of quotes:
use hex!
user$ sed -i 's/\x22asd/\x27dsa/g' file

Zabbix: pushbullet notifications

Put in zabbix's alertscripts folder following script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#!/bin/bash

WDIR='/tmp/zabbix/pushbullet'
LOG="/var/log/pushbullet.log"
mkdir -p $WDIR &>/dev/null

(
to="$1"
subject="$2"
body="$3"

if [[ -z "$to" ]]; then exit 0; fi

OID=$(echo "$body" | grep '^Original event ID: ' | awk '{print $4}')


if [[ -z $(echo "$subject" | grep '^PROBLEM') ]]; then
    # delete
    if [[ -f "$WDIR/$to-$OID" ]]; then
        ID=$(cat "$WDIR/$to-$OID")
        R=$(curl -u "$to": "https://api.pushbullet.com/v2/pushes/$ID" -X DELETE)
        echo -ne "$D Delete\nTo: $to\nSubject: $subject\nBody: $body\nOID: $OID\nResult: " >> $LOG
        echo "$R" >> $LOG
    fi
else
    # send
    D=$(date -R)
    echo -ne "$D Sending\nTo: $to\nSubject: $subject\nBody: $body\nOID: $OID\nResult: " >> $LOG
    R=$(curl -u "$to": https://api.pushbullet.com/v2/pushes -d type=note -d title="$subject" -d body="$body")
    echo "$R" >> $LOG
    ID=$(echo "$R" | grep -Po '"iden":"[^"]+"' | tr -d '"' | awk -F ':' '{print $2}')
    echo "$ID" > "$WDIR/$to-$OID"
fi

) &>/dev/null

Add it to administration->Media types
Then in Profile->Media of your user choose pushbullet and put Access token from https://www.pushbullet.com/account in «Send to» field.

That script will push new notification on trigger if the last one was activated and it removes notification from pushbullet if zabbix sent «OK».

PS: Zabbix's notification template (Configure->Actions->Triggers) should contains following string «Original event ID: {EVENT.ID}»

IT Security Brothers (http://itsb.pro)

Hi guys, I would like to present you our new project IT Security Brothers http://itsb.pro
We provide pentest, consultations and IT outsourcing services.
Feel free to hire us for IT jobs.

Script for downloading repositories from redmine

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
This script may be like a [b]template[/b] for you.
I've used it for downloading git projects from redmine's storage.
Before starting that script you have to authorize on the resource and save cookies in the file.


#!/bin/bash
URLBASE='https://domaint.tld/projects/1/repository/sql/revisions/master/raw/'
debug=0

# Function parses page and greps files names for next downloading
# This function get 1 parameter as a name of folder in which it'll download files
# Btw, this folder will be in the parent folder of that
function getfiles {
    for file in `egrep -o 'changes/.*\?rev=master\" ' index.html|sed -e 's/changes\///g' | sed -e 's/\?rev=master\"\ //g'`
    do
        echo $file
        echo "$URLBASE/$file"
        wget --no-check-certificate --load-cookies cookies-redmine.txt $URLBASE/$file -P ../$1
    done
}

# Function parses page and greps folders names for next downloading files in them.
function getfolders {
    for folder in `egrep -o 'show.*/?depth=1' index.html|sed -e 's/show\///g'|sed -e 's/\?depth=1//g'`
    do
        echo $folder
        mkdir -p ../$folder
        rm -rf index.html
        URL="$URLBASE"
        URL="$URL/$folder"
        wget --no-check-certificate --load-cookies cookies-redmine.txt $URL/
        if [ "$debug" == "1" ]; then
            exit
        fi
                #change this option to 1 for exit on the next turn, but before removing index.html
        debug=0
        getfiles $folder
        getfolders
    done
}

#Get listing of '/'
rm -rf index.html
mkdir -p ../$1
wget --no-check-certificate --load-cookies cookies-redmine.txt $URLBASE/$1/
getfiles $1
getfolders

Bash: IFS delimiter

echo > test << EOF
a b c d
aa bb cc dd
a_b_c_d
EOF

for i in `cat test`; do echo $i; done    
a
b
c
d
aa
bb
cc
dd
a_b_c_d

IFS=$'_'
for i in `cat test`; do echo $i; done
a b c d
aa bb cc dd
a
b
c
d

IFS=$'\n'                                                                                                                                                              
for i in `cat test`; do echo $i;done   
a b c d
aa bb cc dd
a_b_c_d

IFS=$' '
for i in `cat test`; do echo $i; done
a
b
c
d
aa
bb
cc
dd
a_b_c_d