Total Pageviews

Wednesday, May 14, 2014

python paramiko module

Paramiko is a module that implements the SSH2 protocol for secure (encrypted and authenticated)
connections to remote machines.
SSHClient is the main class provided by the paramkio module.

agnel@agn-lnx:~/scripts/python$ more ./ssh.py
#!/usr/bin/python
import paramiko
ssh = paramiko.SSHClient()
# “paramiko.AutoAddPolicy()” will auto-accept unknown keys.
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
conn=ssh.connect(hostname='localhost',port=22, username='agnel',password='welcome')
if conn is None:
         print "Successfully Authenticated"
stdin,stdout,stderr=ssh.exec_command("uname -a")
print stdout.readlines()
ssh.close()

agnel@agn-lnx:~/scripts/python$ ./ssh.py
Successfully Authenticated
['Linux agn-lnx 3.11.0-15-generic #23-Ubuntu SMP Mon Dec 9 18:17:04 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux\n']
agnel@agn-lnx:~/scripts/python$

Use an SSH key to connect :
import os
privatekeyfile = os.path.expanduser('~/.ssh/id_rsa')
mykey = paramiko.RSAKey.from_private_key_file(privatekeyfile)
ssh.connect('10.105.236.208', username = 'agnel', pkey = mykey)


Interactive commands :
cmd = "sudo /etc/init.d/apache2 restart"
stdin, stdout, stderr = ssh.exec_command(cmd)
stdin.write('secret\n')
stdin.flush()
print stdout.readlines()

Packet Capturing on linux

Using tshark
root@bng-sql-1:~# tshark -i eth2 -R diameter -V
tshark: Lua: Error during loading:
 [string "/usr/share/wireshark/init.lua"]:45: dofile has been disabled
Running as user "root" and group "root". This could be dangerous.
Capturing on eth2

Count how many times a string occurs in all files

grep -c string *
to get only files that have one or more occurrence:
grep -c string * | grep -v :0
for multiple occurrences per line:
grep -o string * | wc -l

Shared library management with ldconfig

Ldconfig is a basic system program which determines run-time
linkbindings between ld.so and shared libraries. Ldconfig scans a
running system and sets up the symbolic links that are used to load
shared libraries properly. It also creates a cache (/etc/ld.so.cache)
which speeds the loading of programs which use shared libraries.

To see lib used by a binary
[root@nsetcindia-web ~]# ldd /usr/bin/curl-loader
        libdl.so.2 => /lib64/libdl.so.2 (0x0000003b47e00000)
        libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003b48600000)
        librt.so.1 => /lib64/librt.so.1 (0x0000003b48e00000)
        libz.so.1 => /usr/lib64/libz.so.1 (0x0000003b48a00000)
        libcares.so.2 => not found
        libc.so.6 => /lib64/libc.so.6 (0x0000003b47a00000)
        /lib64/ld-linux-x86-64.so.2 (0x0000003b47600000)
[root@nsetcindia-web ~]#


Add your custom lib locations to /etc/ld.so.conf
and do
ldconfig
To see  
ldconfig -p | less

Delete a file with spaces in linux

Get innode Number
ls -il
find . -inum 270016 -exec rm -i {} \;