Skip to content

Instantly share code, notes, and snippets.

@PSJoshi
PSJoshi / join-strings
Created April 28, 2015 09:34
python - join multiple strings
Instead of doing a + b + c, use ''.join([a, b, c]) syntax for concatenating multiple strings.
Similarly, you want to join multiple paths, you will use dirname + 'xxx.jpg' to form a path.
Instead, use this python syntax - os.path.join(os.path.sep, dirname, 'xxx.jpg')
@PSJoshi
PSJoshi / logstash-with-strace
Created May 5, 2015 10:20
logstash with strace
logstash with strace
----------------------
strace -o /tmp/strace.log -fe trace=network /opt/logstash/bin/logstash -f your.conf
check logstash configuration
------------------------------
/opt/logstash/bin/logstash -f your.conf --configtest
@PSJoshi
PSJoshi / files-per-process
Last active August 29, 2015 14:21
Find no of files in use by process
All the limits are declared in in /etc/limits.conf - or in /etc/limits.d and these are displayed by 'ulimit -n'.
'nofile' - the number of open files
You can make use of 'lsof' for monitoring open files per processes.
root@psj-desktop:~# lsof | awk '$4 ~ /[0-9]+[rwu -].*/{p[$1"\t"$2"\t"$3]=p[$1"\t"$2"\t"$3]+1}END{for (i in p) print p[i],i}' | sort -n | tail
Otherwise, you can make use of /proc file system on linux:
@PSJoshi
PSJoshi / kmeans.py
Last active August 29, 2015 14:21 — forked from larsmans/kmeans.py
#!/usr/bin/python
#
# K-means clustering using Lloyd's algorithm in pure Python.
# Written by Lars Buitinck. This code is in the public domain.
#
# The main program runs the clustering algorithm on a bunch of text documents
# specified as command-line arguments. These documents are first converted to
# sparse vectors, represented as lists of (index, value) pairs.
from collections import defaultdict
input {
tcp {
type => "syslog"
host => "127.0.0.1"
port => 3514
}
tcp {
type => "eventlog"
host => "10.1.1.2"
port => 3515
function and attributes of python module
>>> import django
>>> dir(django)
['VERSION', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'get_version']
# find path for a module
>>> print django.__file__
'/usr/local/lib/python2.6/dist-packages/django/__init__.pyc'
@PSJoshi
PSJoshi / python3-on-centos
Last active August 29, 2015 14:27
python3 on CentOS
#Install Python 3.4 on CentOS
$ wget https://www.python.org/ftp/python/3.4.3/Python-3.4.3.tar.xz
$ tar -xvf Python-3.4.3.tar.xz
$ cd Python-3.4.3
$ su
# yum groupinstall "Development tools"
# exit
$ sudo ./configure
$ sudo make
$ sudo make install
@PSJoshi
PSJoshi / iptables.sh
Created August 23, 2015 16:03
iptable rules
#!/bin/bash
# Reference link - http://www.cyberciti.biz/tips/linux-iptables-examples.html
# Clear any previous rules.
/sbin/iptables -F
# Default drop policy.
/sbin/iptables -P INPUT DROP
/sbin/iptables -P OUTPUT ACCEPT
# Allow anything over loopback and vpn.
/sbin/iptables -A INPUT -i lo -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
/sbin/iptables -A OUTPUT -o lo -s 127.0.0.1 -d 127.0.0.1 -j ACCEPT
@PSJoshi
PSJoshi / subprocess_example.py
Last active November 30, 2015 05:16
python subprocess example
#!/usr/bin/env python
# use of subprocess
import subprocess
# check whether kernel.exec-shield=1 is True or not using - sysctl -a |grep 'kernel.exec-shield=1'
#result = subprocess.Popen('sysctl -a | grep "kernel.exec-shield=1"', shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
result = subprocess.Popen('sysctl -a | grep "kernel.exec-shield=1"', shell=True,stdout=subprocess.PIPE,stderr=subprocess.PIPE)
stdout,stderr=result.communicate()
# the result will be a byte buffer. convert bytes to string:
print stdout.decode('utf-8')
@PSJoshi
PSJoshi / ip-in-network.py
Created October 2, 2015 06:50
python:Check if ip is in a network
>>>from netaddr import IPNetwork, IPAddress
>>>if IPAddress("192.168.0.1") in IPNetwork("192.168.0.0/24"):
print "Yes!"
>>>import ipaddress
>>> ipaddress.ip_address('192.168.0.1') in ipaddress.ip_network('192.168.0.0/24')
True