lundi 26 octobre 2015

Scripting : set variables depending how the where launched (terminal mode, cron/auto mode, ...)

You all know the deal : when you launch a command it (obviously) comes with all your environment variables. But when you want to cron it, none of them is present. We hence want to set some variables, but only in some cases. The "tty" command will help us do so.


You can use the tty tool to check if a script is called from the standard output :


if ! tty -s
then
    exec >/dev/null 2>&1
else
    MAIL_DEST="email@server.ext"
fi



  • The command tty returns :

User Commands                                              tty(1)

NAME
     tty - return user's terminal name

SYNOPSIS
     tty [-l] [-s]

DESCRIPTION
     The tty utility writes to the standard output  the  name  of
     the  terminal  that is open as standard input. The name that
     is used is equivalent to the string that would  be  returned
     by the ttyname(3C) function.

OPTIONS
     The following options are supported:

     -l       Prints the synchronous line  number  to  which  the
              user's terminal is connected, if it is on an active
              synchronous line.

     -s       Inhibits printing of the terminal path name, allow-
              ing one to test just the exit status.

EXIT STATUS
     The following exit values are returned:

     0        Standard input is a terminal.

     1        Standard input is not a terminal.

     >1       An error occurred.

 

lundi 19 octobre 2015

MS Word shortcuts

Sometimes you're bound to use it for professional purposes...



3 shortcuts on MS-Word :

  • Shift + F3 : change the current word type case between ALLUPPER <-> alllower <-> Firstcharup.
  • Shift + F4 / F5 : navigate 1 screen forward (f4) / backward (f5.

  • Control + Y : repeat the last action performed.

mardi 29 septembre 2015

Gnuplot by line (instead of columns)

In case you have some data not sorted by column (the easiet way to deal with gnuplot), and you want to plot it


Gnuplot definition: (file myplotrow.gpl)
set terminal x11 1 noraise
set autoscale
set grid
set xdata time
set timefmt "%Y-%m-%d_%H:%M:%S"
set format x "%m/%d\n%H:%M"
plot "data.dat" \
        using 1:(stringcolumn(2) eq "valAAA"? $3:1/0) title "A" lc rgb "blue",\
     "" using 1:(stringcolumn(2) eq "valBBB"? $3:1/0) title "B" lc rgb "red",\
     "" using 1:(stringcolumn(2) eq "valCCC"? $3:1/0) title "C" lc rgb "green",\
     "" using 1:(stringcolumn(2) eq "valDDD"? $3:1/0) title "D" lc rgb "orange",\
     "" using 1:(stringcolumn(2) eq "valEEE"? $3:1/0) title "E" lc rgb "purple",\
     "" using 1:(stringcolumn(2) eq "valFFF"? $3:1/0) title "F" lc rgb "violet"

pause 100
reread

That you can launch with :
gnuplot < myplotrow.gpl

Or, if you change the output in the first line :
set terminal postscript color


with :
gnuplot < myplotrow.gpl > myoutput.ps


Exemple values :
2015-09-24_10:00:00 valAAA 37.000
2015-09-24_10:00:00 valBBB 37.000
2015-09-24_10:00:00 valCCC 37.000
2015-09-24_10:00:00 valDDD 37.000
2015-09-24_10:00:00 valEEE 37.000
2015-09-24_10:00:00 valFFF 37.000
2015-09-24_13:30:00 valAAA 40.000
2015-09-24_13:30:00 valBBB 40.000
2015-09-24_13:30:00 valCCC 41.000
2015-09-24_13:30:00 valDDD 42.000
2015-09-24_13:30:00 valEEE 42.000
2015-09-24_13:30:00 valFFF 42.000
2015-09-25_02:00:00 valAAA 5.000
2015-09-25_02:00:00 valBBB 5.000
2015-09-25_02:00:00 valCCC 5.000
2015-09-25_02:00:00 valDDD 5.000
2015-09-25_02:00:00 valEEE 5.000
2015-09-25_02:00:00 valFFF 5.000
2015-09-25_03:30:00 valAAA 15.000
2015-09-25_03:30:00 valBBB 15.000
2015-09-25_03:30:00 valCCC 16.000
2015-09-25_03:30:00 valDDD 16.000
2015-09-25_03:30:00 valEEE 15.000
2015-09-25_03:30:00 valFFF 15.000
2015-09-25_05:30:00 valAAA 19.000
2015-09-25_05:30:00 valBBB 19.000
2015-09-25_05:30:00 valCCC 19.000
2015-09-25_05:30:00 valDDD 20.000
2015-09-25_05:30:00 valEEE 19.000
2015-09-25_05:30:00 valFFF 19.000
2015-09-25_06:00:00 valAAA 19.000
2015-09-25_06:00:00 valBBB 19.000
2015-09-25_06:00:00 valCCC 18.000
2015-09-25_06:00:00 valDDD 19.000
2015-09-25_06:00:00 valEEE 19.000
2015-09-25_06:00:00 valFFF 20.000
References :
  • Gnuplot terciary condition:
http://stackoverflow.com/questions/6564561/gnuplot-conditional-plotting-plot-col-acol-b-if-col-c-x
  • Colors
 http://gnuplot.sourceforge.net/demo/rainbow.html
 

lundi 21 septembre 2015

Flatten and XML file (perl)

This perl one-liner is useful in case you need to "flatten" and XML file.



PERL_CONVERT="use XML::Simple; print XMLout(XMLin('-', KeepRoot => 1), AttrIndent => 1, KeepRoot => 1, XMLDecl => '');"

To be used like this :

/usr/bin/perl -e "$PERL_CONVERT" > $TMPFILE


Whatever input you have (in XML), the goal is to have a "comparable" ouptut that will respect the XML structure :
FROM:


...

...

 TO:


attr1="value1"
attr2="value2"
...
attr3="value3"
>

...




...


Specifically, this is really useful if you need to merge 2 different versions of  your XML file, like for example in SVN.

vendredi 19 juin 2015

Ansible vs. Chef vs. Puppet vs. Salt

There are currently various tools to maintain automatically an infrastructure. The four listed below seem to be the main ones.


Ansible is an IT automation tool. It can configure systems, deploy software, and orchestrate more advanced IT tasks such as continuous deployments or zero downtime rolling updates.


·         Chef https://www.chef.io/
“Chef turns infrastructure into code. With Chef, you can automate how you build, deploy, and manage your infrastructure. Your infrastructure becomes as versionable, testable, and repeatable as application code."


·         Puppet https://puppetlabs.com
“Puppet is a configuration management solution that allows you to define the state of your IT infrastructure, and then automatically enforces the desired state. Puppet automates every step of the software delivery process, from provisioning of physical and virtual machines to orchestration and reporting; from early-stage code development through testing, production release and updates.”


·         Salt : http://saltstack.com
“SaltStack takes a new approach to infrastructure management by developing software that is easy enough to get running in seconds, scalable enough to manage tens of thousands of servers, and fast enough to control and communicate with them in milliseconds. SaltStack delivers a dynamic infrastructure communication bus used for orchestration, remote execution, configuration management and much more. The Salt project was launched in 2011 and today is the fastest-growing, most-active infrastructure orchestration and configuration management open source project in the world. The SaltStack community is committed to keeping the Salt project focused, friendly, healthy and open.”




And some comparisions : 




jeudi 18 juin 2015

CVSS : Common Vulnerability Scoring System (v3) calculator

As explained in wikipedia:CVSS "Common Vulnerability Scoring System (CVSS) is a free and open industry standard for assessing the severity of computer system security vulnerabilities. It is under the custodianship of the Forum of Incident Response and Security Teams (FIRST). It attempts to establish a measure of how much concern a vulnerability warrants, compared to other vulnerabilities, so efforts can be prioritized. The scores are based on a series of measurements (called metrics) based on expert assessment. The scores range from 0 to 10. Vulnerabilities with a base score in the range 7.0-10.0 are High, those in the range 4.0-6.9 as Medium, and 0-3.9 as Low.[1]"




mardi 2 juin 2015

Replace all occurences of a string in a bunch of files by another string (in place)


  • Find all files on containing STRING_AAAA and replace it by STRING_BBBB in place (directly in the file).
find . -type f -exec grep -l STRING_AAAA {} \; -exec perl -pi -e 's!STRING_AAAA!STRING_BBBB!g' {} \;
Pre-tests :
  • Find all files on containing STRING_AAAA.
find . -type f -exec grep -l STRING_AAAA {} \;

mercredi 29 avril 2015

Security links




vendredi 24 avril 2015

get return value ($?) of a tee'd command in ksh

How to get the return value of "command1" if passed to a " | command2" , for example "| tee OUTFILE" :
 
 
    command1 | tee OUTFILE


 With recent shells, you can use the array storing this : bash is ${PIPESTATUS[x]} , zsh is $pipestatus[x] to get this value :
 
      command1 | tee OUTFILE
      echo ${PIPESTATUS[0]}

With older shells, this feature is not available, but you still can use posix features and play around with the file descriptors :
 
    exec 4>&1; RETVAL=$({ { command1 ; echo $? >&3 ; } | { tee $OUTFILE >&4; } } 3>&1); exec 4>&-

 
Explainations 
  1. Create a file descriptor fd4 and map it to STDOUT
  2. Fill the RETVAL value with the fork.
    1. fd4 is transmitted, but STDOUT and STDERR are recreated for the forked process so fd4 still exist and is available.
    2. create the fd3 and redirect it to STDOUT
    3. map 'tee' STDOUT to the previously created fd4 so that it gets printed to the terminal; 
    4. tee then forks the piped 'command1' which output value $? is sent to fd3 with 'echo $? >&3', and hence outputed in the RETVAL variable; 
  3. Eventually, close the fd4


References :

mercredi 25 mars 2015

Firefox Detect if a page is loaded in the "sidebar" (Javascript)

Firefox has this wonderful option when you create a bookmark : you can load a specific page directly in the sidebar. But it's not very well documented on how you can detect as a web developer that your page is actually shown in such a sidebar.
This is the javascript trick I used (validated with Firefox version=20.0.1):
{ if (window.content == window) /* not sidebar */ else /* sidebar */ }
Source :