Replacing a piece of XML with awk

This script searches for an initial tag and and closing one and replaces the content.

# !/bin/bash
awk ‘
BEGIN {pr = 1;one = 0}
/<Name>OPENING<\/Name>/ {pr = 0;}
{ if (pr) print }
{ if (!pr && !one) {print (“\t\t <Name>OPENING</Name> \n \t\t\t <Value>false</Value> \n \t\t<Type>STOPHERE</Type> \n ” ); one =1 ;}}
/<Type>STOPHERE<\/Type>/ {pr = 1;}
‘< $1

Not the most elegant solution but lends itself to multiple replacements.

 

find . | grep file.xml | xargs -I {J} -n 1 bash -c ‘ ./aw.sh {J} > {J}.new ; mv -f {J}.new {J}  ‘

nJoy 😉

 

 

 

Track ftp password from tcpdump Linux

Very nice and simple:

tcpdump port ftp -l -A | egrep -i ‘pass=|pwd=|log=|login=|user=|username=|pw=|passw=|passwd=|password=|pass:|user:|username:|password:|login:|pass |user ‘ –color=auto –line-buffered -B20

nJoy  😉

Adding all new files and subdirs to svn

Sometimes you find yourself catching up with some devs who did not care to add all the stuff they should to SVN or you want to make sure all files are in SVN. Well here’s a hand script :

svn status | awk ‘{if ($1 == “?”) print $2 }’ | xargs svn add

nJoy 😉

Running node.js in production.

Node.js is my new love !!

In one of my latest projects I needed to set up a RESTFUL web service which in a nutshell would be a simple interface / API to a bunch of functions based tightly on the HTML standard commands GET, PUT, DELETE, etc..

I decided to go with a language I know JAVA ..  I never worked before with J2EE a week and bulky installations etc later , multiple mixed information , different frameworks etc.. it’sa nightmare. If you join a team who already works in a given way etc.. it’s not too bad but the divergence required to maintain and test the services, the slowness of having to run upload , connect to glassfish / tomcat , the overheads…  Well not a good experience .. got it working but sustaining it for growth and even just changing a DB config is a messy thing unless you have all the best practices in mind when you start it’s not a nice place ..

I tried other stuff including ruby and then I tried node.js … BINGO !!!!

What took me 12 hours to get going from scratch on my system, took only 5 minutes or so with node, npm and express..

Love at first sight !! Thought I’d share some of the love here !!!

Advantages :

  1. Great performance (also see 2)
  2. Designed with non-blocking architecture from the ground up
  3. Built on the Google v8 JavaScript run time engine.
  4. Ultimate transparency of code
  5. Cross platform compatible ( and one language fits all Server -> )
  6. Super libraries (many of which quite well hardened.
  7. Massive industry backing and support :
    1. Google
    2. Ebay
    3. Amazon
    4. Yahoo
    5. Microsoft (Azure)
    6. Miscellaneous but all true : https://github.com/joyent/node/wiki/Projects,-Applications,-and-Companies-Using-Node
    7. Massive industry support : http://nodejs.org/industry/
  8. Scales almost transparently.
  9. Support for almost all know DBs especially friendly with the new NoSQL systems (they kind of grew together)
  10. It also handles the persistence across reboots.
  11. Very concise and powerful structure.

Disadvantages :

  1. No good open source IDE with intelligent smartfill etc.. (I still use VIM and Notepad ++ )  I want a Visualstudio.js
  2. Single threaded by default though thanks to pm2 (see later) it can be an advantage.
  3. Code can become messy fast.
  4. Usual JavaScript nags –In particular I hate type variant languages.

The first question I had was how to have it work on a server

There is a number of ways to keep a node.js application running if it crashes with automatic restart. 5 of which are :

  • node-dev
  • nodemon
  • forever
  • supervisor
  • pm2

The first four are mostly a great way to run code in dev mode so when code changes they automatically reload an app and restart everything that needs to be restarted.

For production though, pm2 rules them all.

On a prod server you do not want the app to reload on a file change as soon as the file changed so that’s the first point.

The added features pm2 comes with are amazing though:

  1. Simple to run an application on n threads e.g.:
        • pm2 start app.js -i 4
  2. Logs are, by default all piped to common logs by application instance name and can be monitored from command line
        • pm2 logs app
  3. Pm2 provides a great tool to monitor the run time engines
        • pm2 monit

 

PM2 Notes can be found here: https://github.com/Unitech/pm2/blob/master/README.md

Simply awesome !!

I need to dedicate a whole section of the blog to Node !!!

nJoy 😉

 

Disabling IPv6, breaks SSH X11 Forwarding in Linux CENTOS and UBUNTU all versions

This was a weird one. It took me ages to discover why SSH X11 died at some point after tuning a box.

It so happens that disabling IPv6 ( not used on my networks) is assumed in SSH at some point and if you disable it (which many people say is a way to avoid systems using IPv6 to other boxes and ignore IPv4 provisions like firewall settings, routes etc..)

So re-enable the IPv6 in /etc/sysctl.conf or /etc/sysconfig/network or other networking script locations, if you need this functionality.

If i find any other counter indication I’ll drop a note and link.

nJoy 😉

Using Puttygen to create a ssh key and passwordless login with putty to Centos.

The ssh password-less login works in a  simple way. Though sometime people find it confusing.

The basics:

>  Client side has a private key pointed to by

>  Server side has the public key of the client.

On your client box create a public and private key set using (for putty) puttygen. (In linux all you need to use is ssh-keygen.

Please look at the page here : https://www.puttygen.com

On the server :

If not already there create a folder /<user_home>/.ssh  e.g. /root/.ssh and in that folder create a file named authorized_keys. This file will store all the public keys that a given user with matching private key, can use. Easy way echo “<paste here>” >  ~/.ssh/authorized_keys

mkdir ~/.ssh

echo “ssh-rsa AAAAB3NzaC1yc2EAAAABJQAAAIBz7Pml97wSzFMSr1W84rA0Mm8MY8I1jKdAmMcF4sw5GilormHJQRYI8siT1XPaLJFAO20ziZg9YrJFp+roKC34gpe1myFWUz944iucrLIQznZwPDJbMKxQXwzj1LUPmt7eXPzwM1ztvcG8HOoZlTt2B6hOAVWAHxlZNzPq/9y1Fw== rsa-key-20131124” > ~/.ssh/authorized_keys

chmod 600 ~/.ssh/authorized_keys

chmod 700 /~/.ssh

Once this is done you can add the private key to the client under ssh -> Auth -> private key.

Select back the session and save it as usual in Putty.

If you get the error “Server refused our key” it probably means you still have SElinux enabled.

To fix this:

restorecon -R -v /root/.ssh

 

That should work.

nJoy 😉

Controlling windows system from a Linux box.

Set up winexe.

wget "ftp://ftp.pbone.net/mirror/ftp5.gwdg.de/pub/opensuse/repositories/home:/GRNET:/synnefo/CentOS_CentOS-6/x86_64/winexe-1.00-9.1.x86_64.rpm"
yum install winexe-1.00-9.1.x86_64.rpm

 

Test the connection

<pre>telnet 10.0.0.123 139</pre>

Now we can try the system out

winexe -U "User 1" --password=secretpassword //10.0.0.123 'cmd /C "whoami"'

If you get :

ERROR: Failed to open connection - ERRDOS:ERRnomem

then it’s probably a Windows 7 box

run

 reg add "HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Memory Management" /v "LargeSystemCache" /t REG_DWORD /d 1 /f
 reg add "HKLM\SYSTEM\CurrentControlSet\Services\LanmanServer\Parameters" /v "Size" /t REG_DWORD /d 3 /f

on the windows box and restart.

Now try again

winexe -U "User 1" --password=secretpassword //10.0.0.123 'cmd /C "whoami"'

you should get something like :

Win7Sstem\User 1

njoy 😉

 

 

ref: http://alan.lamielle.net/2009/09/03/windows-7-nonpaged-pool-srv-error-2017

ref: http://www.decuslib.com/decus/vmslt99a/nt/tips.txt