Questo sito utilizza i cookie per migliorare l'esperienza di navigazione. Continuando la navigazione acconsenti all'utilizzo dei cookie.

BASH MAGIC

Talking about awk.

As you certainly know, there is  a function in awk that can return a portion of a string, this function is called substr. The  syntax is : substr(s,c,n)
where s is the original string, c iis the start posizion of the substring you want to get, and n is the length of the substring.

The problem I should face some days ago was to investigate in the logs of a Jboss server to get  the long executions time of a certain remote call, logged as below :  
2014-20-05 14:25:01,004 ...... Time:[245]

So I wanted the substr of "Time:[245]" to print the lines which the execution time was for example > 500 ms.

The solution is pretty easy: you want to get the substring of Time:[xxx] starting after the char "[" and finishing before "]" , than  you have to perform a numeric comparision between the substring obteined and the number 500. To get the position of the square brackets you can use another simple awk function  called "index".

but, as you can imangine,  substr return a string, so how do you cast this string in a number?
Below is my solution :

awk '{
b=substr ( $10, 7 , (index( $10,"]") - index( $10,"[") -1 )  )  + 0
if (  b > 500 )
        print  b  
}'



 I just added a zero to the result of substr. I know it is not very elegant but it works Fico

of course, if you have a better solution just post it!



full text Search  in a bash script

As you perfecly know, the find command can search files , and you have so many options to decide what files you want to find, and execute commands on every file.
This is done with the exec option. That said, you can easily perform some kind of full text search over a set of files.

For instance let's suppose we want to search all the strings that represent IP address on all the configuration files of a servlet engine like TOMCAT. This is easily done when you
have a regular expression that macth IP addresses : ([0-9]{1,3}\.){3}[0-9]{1,3}

 

Let's try to explain the details:
[0-9]{1,3} means a number from 0 to 999
([0-9]{1,3}\.) means the same but followed by a "." char
([0-9]{1,3}\.){3} means the same but repeated for 3 times


So what you have to do is to go in the ${CATALINA_HOME}/conf directory and for every file from there execute a egrep command to perform the full text search :
the command is the following


find . -type f -exec egrep "([0-9]{1,3}\.){3}[0-9]{1,3}" {} \;

Fine Isn't it ?

 

HTTP CALL with wget to resources protected by form authentication

SERVER HTTP : tomcat
HTTP client : wget

Let's suppose that you have to make a script that calls a web page behind an authentication form. You can do that using the wget command.
First of all we need to write on the file system the session cookie, because without the session the server redirect to the login page.
Launch this command once, to call the login page, eventually pass the right POST parameters ( for example : name=admin e pwd=admin ),
and save all cokies ( also the session cokie ) in the file /tmp/cookies.txt


wget -q --save-cookies /tmp/cookies.txt --keep-session-cookies --post-data "name=admin&pwd=admin" -O /dev/null "http://X.X.X.X:8080/jmsGridConsole/login"

At this point we can load all the cookies from /tmp/cookies.txt [ --keep-session-cookies --load-cookies /tmp/cookies.txt] and call the protected resource
passing the POST parameters [--post-data "name=admin&pwd=admin"]


wget -q --keep-session-cookies --load-cookies /tmp/cookies.txt --post-data "name=admin&pwd=admin" -O- "http://X.X.X.X:8080/jmsGrid......."

The -O- option write the http response to the standard output

SED can DO BACKUPS

from man page :
  -i[SUFFIX], --in-place[=SUFFIX]
    edit files in place (makes backup if extension supplied).
    
the next command can search and replace a string  in xml or properties configuration files and make a backup with extension .bck for each file edited    

  for i in $(find . -name "*xml" -o -name "*properties"  -exec grep -l  from {} \; ); do  echo -n $i":" ; sed  -i.bck 's/from/to/g' $i;  done

Share

Aggiungi commento


Codice di sicurezza
Aggiorna