Skip to content

Instantly share code, notes, and snippets.

View nyrahul's full-sized avatar
🐞

Rahul Jadhav nyrahul

🐞
View GitHub Profile
@nyrahul
nyrahul / hyperthreading_ctl.sh
Created January 10, 2018 06:03
Enable/Disable hyperthreading & multicore on x86 machines with linux (ubuntu distro tested)
#!/bin/bash
trap byefunc EXIT
byefunc()
{
[[ "$NO_CPUINFO" == "1" ]] && return
lscpu
}
@nyrahul
nyrahul / bashrc_ext
Created January 11, 2018 07:41
Set netns name and git branch name in bash-PS1
parse_git_branch() {
git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/ (\1)/'
}
netns_name() {
str=`ip netns identify $$`
[[ "$str" != "" ]] && echo "\033[1;35m[$str]\033[0m " && return
}
export PS1="$(netns_name)\u@\h:\[\033[32m\]\w\[\033[33m\]\$(parse_git_branch)\[\033[00m\]\$ "
#!/bin/bash
[[ "`whoami`" != "root" ]] && echo "Got to be root. Use sudo." && exit
export LOG_LEVEL=DBG
export OVERRIDE_IP=192.168.100.100
./server_proxy &
sleep 1
sysctl -w net.ipv4.ip_forward=1
ssh -L 4000:192.168.56.102:80 127.0.0.1
# 4000 = Remote port
# 192.168.50.102 = Remote IP
# 80 = Local port
# 127.0.0.1 = Local IP
What this means?
Whatever packets come to 127.0.0.1:80 forward it to 192.168.56.102:4000
@nyrahul
nyrahul / writepcap.c
Last active March 15, 2023 14:33
Write a packet buffer to a file in pcap format without using pcap libs/hdrs.
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <sys/time.h>
/*---------------------------------------------------------------------------*/
/* CITT CRC16 polynomial ^16 + ^12 + ^5 + 1 */
/*---------------------------------------------------------------------------*/
unsigned short crc16_add(unsigned char b, unsigned short acc)
{
@nyrahul
nyrahul / perf_stats.sh
Created March 9, 2018 09:52
Get Perf statistics over multiple run and show it in column format
#!/bin/bash
OUTFILE=perf_out.txt
rm perf*.txt
for((i=0;i<4;i++)); do
#timeout -s SIGINT 12 perf stat -e dTLB-loads -e cs,cycles,instructions -D 2000 -a -o perf$i.txt
#timeout -s SIGINT 12 perf stat -e dTLB-stores -e cs,cycles,instructions -D 2000 -a -o perf$i.txt
#timeout -s SIGINT 12 perf stat -e dTLB-load-misses,dTLB-store-misses -e cs,cycles,instructions -D 2000 -a -o perf$i.txt
timeout -s SIGINT 12 perf stat -e cs,cycles,instructions -D 2000 -a -o perf$i.txt
@nyrahul
nyrahul / my_cmds.sh
Last active March 1, 2019 14:31
important command ref
#!/bin/bash
#socat examples:
#recv msgs on udp port 60K and print to stdout. recv input on stdin and sendto connected socket.
socat - UDP4-LISTEN:60000
socat - UDP4-SENDTO:192.168.106.2:60000
# send contents of t.c to multiple TCP4 servers
socat GOPEN:t.c - | tee >(socat - tcp4:127.0.0.1:12345) >(socat - tcp4:127.0.0.1:33455) > /dev/null
@nyrahul
nyrahul / parse_cmdargs.sh
Created April 4, 2018 02:13
bash parse cmd args
usage()
{
echo "Usage: $0 <options>"
}
parse_cmdargs()
{
# -s | --server is the short and long option to be parsed
# Remember to specify : in cases where argument is nessary both in short and long options
OPTS=`getopt -o s: --long server:xyz: -n 'parse-options' -- "$@"`
#!/bin/bash
set -E
trap 'echo Failed line: $LINENO command: $BASH_COMMAND && exit $?' ERR
lcnt=1
nw_pref="192.168.10"
create_node()
{
ns="$1"; shift;
#!/bin/bash
set -E
trap 'echo Failed at $lineno COMMAND: $BASH_COMMAND && exit 1' ERR
SRV_IP="192.168.106.2"
SP_IP="192.168.105.2"
echo 1 > /proc/sys/net/ipv4/ip_forward
iptables -F