How to parse xml tags in bash or retrieve xml tag values in linux


Consider the following example file test.xml





<header>
        <tag1>1</tag1>
        <system>test123</system>
        <cdc>230</cdc>
       
</header>

   
 


 grep -E -m 1 -o "(.*)" test.xml

This will return "test123"


if you want only the value we need to pipe it further

grep -E -m 1 -o "(.*)" test.xml | sed -e 's,.*\([^<]*\).*,\1,g'

This will return "test123"

fix failed: NT_STATUS_BAD_NETWORK_NAME"



smbclient -L 127.0.0.1 -U testuser  ( will list directories )

smbclient //127.0.0.1/Pubshare -U testuser ( will connect )

Simple gpg encryption setup

Generation of encryption keys
-----------------------------------

gpg --gen-key


gpg (GnuPG) 1.4.5; Copyright (C) 2006 Free Software Foundation, Inc.
This program comes with ABSOLUTELY NO WARRANTY.
This is free software, and you are welcome to redistribute it
under certain conditions. See the file COPYING for details.

Please select what kind of key you want:
   (1) DSA and Elgamal (default)
   (2) DSA (sign only)
   (5) RSA (sign only)
Your selection? 1
DSA keypair will have 1024 bits.
ELG-E keys may be between 1024 and 4096 bits long.
What keysize do you want? (2048)
Requested keysize is 2048 bits
Please specify how long the key should be valid.
         0 = key does not expire
       = key expires in n days
      w = key expires in n weeks
      m = key expires in n months
      y = key expires in n years
Key is valid for? (0) 0
Key does not expire at all
Is this correct? (y/N) y

You need a user ID to identify your key; the software constructs the user ID
from the Real Name, Comment and Email Address in this form:
    "Heinrich Heine (Der Dichter) "

Real name: learnadmin
Email address: admin@learnadmin.com
Comment: this is comment section
You selected this USER-ID:
    "learnadmin (this is comment section) "

Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? o
You need a Passphrase to protect your secret key.
----------------
Here give password which is used for decryption of files. ( my testpassphrase is test@123 )
----------------
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.++++++++++++++++++++++++++++++.+++++++++++++++.+++++++++++++++...++++++++++++++++++++++++++++++++++++++++..+++++..+++++..++++++++++++++++++++.+++++..........+++++

Not enough random bytes available.  Please do some other work to give
the OS a chance to collect more entropy! (Need 162 more bytes)
We need to generate a lot of random bytes. It is a good idea to perform
some other action (type on the keyboard, move the mouse, utilize the
disks) during the prime generation; this gives the random number
generator a better chance to gain enough entropy.
.+++++.+++++++++++++++..+++++.+++++.+++++++++++++++.+++++.++++++++++++++++++++.+++++++++++++++.++++++++++...+++++++++++++++....+++++++++++++++..++++++++++++++++++++>++++++++++>+++++.>.+++++........<+++++.......................................................................>+++++....................<+++++......>+++++................................................................................................................+++++^^^^
gpg: /home/username/.gnupg/trustdb.gpg: trustdb created
gpg: key 4F13D8CF marked as ultimately trusted
public and secret key created and signed.

gpg: checking the trustdb
gpg: 3 marginal(s) needed, 1 complete(s) needed, PGP trust model
gpg: depth: 0  valid:   1  signed:   0  trust: 0-, 0q, 0n, 0m, 0f, 1u
pub   1024D/4F13D8CF 2014-03-10
      Key fingerprint = BB11 7A85 CC87 55D2 6F35  D34C CD32 5F04 4F13 D8CF
uid                 learnadmin (this is comment section)  
sub   2048g/5A897101 2014-03-10

------------------------
Tip for creating random key for cert generation :
Depending on your disk type
dd if=/dev/sda of=/dev/zero
(or)
dd if=/dev/hda2  of=/dev/zero
2911235+0 records in
2911234+0 records out
1490551808 bytes (1.5 GB) copied, 8.24918 seconds, 181 MB/s

The above command with take input form your disk and writes it randomly to /dev/zero

---------------------------


gpg --import Yourkey.gpg ( not required . This key need to be imported if you are using this encryption on other server or other client )

Create a file test.txt and try to encrypt and check if it is working ...!!


gpg --always-trust --encrypt --recipient "learnadmin (this is comment section) " test.txt


gpg --batch --passphrase test@123 --decrypt test.txt.gpg>test.txt


Graphite & statsd installation and Configuration

How to install Graphite and statsd
-----------------------------------

yum install   python-devel.noarch
yum install   pycairo.x86_64 Django.noarch django-tagging.noarch  python-twisted.noarch python-zope-interface.x86_64 python-zope-interface httpd memcached  python-memcached
yum install   fontconfig.x86_64 fontconfig-devel.x86_64
yum install   mod_wsgi.x86_64
yum install   python-pip.noarch

pip-python install whisper
pip-python install carbon
pip-python install graphite-web



cp /opt/graphite/conf/carbon.conf.example /opt/graphite/conf/carbon.conf
 cp /opt/graphite/conf/storage-schemas.conf.example /opt/graphite/conf/storage-schemas.conf
 cp /opt/graphite/conf/graphite.wsgi.example /opt/graphite/conf/graphite.wsgi
 cp /opt/graphite/examples/example-graphite-vhost.conf /etc/httpd/conf.d/graphite.conf
 cp /opt/graphite/webapp/graphite/local_settings.py.example /opt/graphite/webapp/graphite/local_settings.py




 python /opt/graphite/webapp/graphite/manage.py syncdb
 chown -R apache:apache /opt/graphite/storage/



wget -O /etc/init.d/carbon https://gist.github.com/raw/3652720/6978bda604c794e21165a81c6b879528d19e8bb4/carbon.init.sh
 chmod 0755 /etc/init.d/carbon
chkconfig --add carbon



 /opt/graphite/bin/carbon-cache.py start
 /etc/init.d/httpd start



yum install nodejs npm


cd /opt/graphite
git clone https://github.com/etsy/statsd.git
cd statsd
cp exampleConfig.js local.js



/etc/init.d/carbon start

/etc/init.d/memcached  start
/etc/init.d/statsd start



Configurations
--------------------
Graphite Configuration: /opt/graphite
Statsd configugration: /opt/graphite/statsd




Graphite/Statsd Logs
---------------------------

Graphite: /opt/graphite/storage/log/webapp
Statsd:  /var/log/statsd/
statsderr.log  statsd.log

Bash script to mask credit card number or any data in logs.

create a temp test file for testing your data

cat test.txt

hi this is my credit card number 1234567891234567
i have 22 this 22 is just for testing 1234567890123456 and this one

Here is the format which varies according to your requirement .
In my case there are 16 digits . 
I want to mask first 6 number here is the regular expression or bash command 


 sed -e :a -e "s/[0-9]\{6\}\([0-9]\{10\}\)/\*\*\*\*\*\*\1/;ta" test.txt 


here 6 and 10 are number of masks and number of visible characters respectively.
you can alter them according to your needs. 

And the stars can be replaced by # or the value you want to mask with .
The number of starts should be equal to number of masks in my case it is 6

Linux list all processes based on memory consumption .

list top 5 processes
ps -eo pmem,pcpu,vsize,pid,cmd | sort -k 1 -nr | head -5

list all processes in assending order .
ps -e -orss=,args= | sort -b -k1,1n | pr -TW$COLUMNS