<commit_before>
----------------------------------ooOOOOoo---------------------------------------

libpcap:

Solution:
Install libpcap-dev package. libpcap install fails,

sudo apt-get install libpcap-dev
....
user@ubuntu:/$ whereis pcap
pcap: /usr/include/pcap.h /usr/include/pcap /usr/share/man/man3/pcap.3pcap.gz


Issue:
user@ubuntu:~/gDocuments/code/C-Snippets/Sandbox$ gcc -o ldev ldev.c -lpcap
ldev.c:12:46: fatal error: pcap.h: No such file or directory

user@ubuntu:~/gDocuments/code/C-Snippets/Sandbox$ whereis pcap.h
pcap:

user@ubuntu:/$ sudo apt-get libpcap
[sudo] password for user: 
E: Invalid operation libpcap


----------------------------------ooOOOOoo---------------------------------------

pcap_open_live:

unable to cpature as non root user.

Solution:

Do the following as root

Add a capture group and add yourself to it

groupadd pcap
usermod -a -G pcap $USER
Next, change the group of tcpdump and set permissions

chgrp pcap /usr/sbin/tcpdump
chmod 750 /usr/sbin/tcpdump
Finally, use setcap to give tcpdump the necessary permissions

setcap cap_net_raw,cap_net_admin=eip /usr/sbin/tcpdump
Be careful, that this will allow everybody from the group pcap to manipulate network interfaces and read raw packets!


http://askubuntu.com/questions/530920/tcpdump-permissions-problem

More details from

http://peternixon.net/news/2012/01/28/configure-tcpdump-work-non-root-user-opensuse-using-file-system-capabilities/

Issue:

DEV: eno16777736
pcap_open_live(): eno16777736: You don't have permission to capture on that device (socket: Operation not permitted)


----------------------------------ooOOOOoo---------------------------------------

wireshark:
sudo apt-get install wireshark

# Allowing non super users to capture the packets
sudo groupadd wireshark
sudo usermod -a -G wireshark $USER
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 750 /usr/bin/dumpcap
sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap
sudo getcap /usr/bin/dumpcap

# Wireshark display filter
ip.addr == 10.0.0.1 && ip.ttl == 111

# using tshark with capture filter and printing summary to console and capturing full
# packets to the file.
sudo tshark -f "host 172.16.17.32" -i enp0s8 -w /tmp/ars.pcap -P

# starting wireshark with a pcap file for analysis
 sudo wireshark /tmp/ars.pcap "ip.addr == 172.16.17.32"


https://ask.wireshark.org/questions/16343/install-wireshark-on-ubuntu
https://wiki.wireshark.org/CaptureFilters
https://www.wireshark.org/docs/wsug_html_chunked/AppToolstshark.html


----------------------------------ooOOOOoo---------------------------------------

git:

Once two factor authentication is enabled for password use the token when doing git operations using https.

https://help.github.com/articles/caching-your-github-password-in-git/


Unable to push after two factor authentication enabled.

http://stackoverflow.com/questions/17659206/git-push-results-in-authentication-failed

If you enabled two-factor authentication in your Github account you won't be able to push via HTTPS using your accounts password. Instead you need to generate a personal access token. This can be done in the application settings of your Github account. Using this token as your password should allow you to push to your remote repository via HTTPS. Use your username as usual.
https://help.github.com/articles/https-cloning-errors#provide-access-token-if-2fa-enabled

You may also need to update the origin for your repository if set to https:

git remote -v 
git remote remove origin 
// Did not work : git remote add origin <EMAIL>@github.com:user/repo.git  

you can also do git remote add origin https://username:access-token@github.com/username/repo.git to store your personal access token. 

// Did not work: https://help.github.com/articles/caching-your-github-password-in-git/

git remote set-url origin https://github.com/zkirkland/Random-Python-Tests.git
This one works. But after wards you need to add 
git push --set-upstream origin master

may have to do git pull origin master 

try git branch and git pull commands to see if they are fine and test with git remote -v

https://confluence.atlassian.com/bitbucketserver/permanently-authenticating-with-git-repositories-776639846.html

git config --global credential.helper 'cache --timeout 72000'
git config --global color.ui true

Check out a new repo
git clone git://location/proj.git

Actually see what’s going on
gitk --all

Interactive adding for commits
git add -i

Unstage changes
git reset HEAD

Return to last committed state
git reset --hard

Undo a merge
git reset --hard [ID]
git reset --hard HEAD~[number back]

Undo a commit with message
git reset --soft HEAD^

Modify a commit
git commit --amend

Add more files to existing commit
git add ...
git commit --amend -C HEAD

Branches have diverged – just get up stream
git reset --hard @{upstream}

Checkout specific version of a file
git checkout [ID] [FILE]

Updating submodules
git submodule init
git submodule sync
git submodule update

Go through every submodule and checkout the head
git submodule foreach git checkout master
git submodule foreach git pull origin master
Or automatically merge
git submodule update --merge

Show difference between two branches
git diff [BRANCH 1]..[BRANCH 2]

Difference between two commits
git diff [ID1] [ID2]

Merge a branch back into master
git checkout master
git merge [BRANCH]

Show all branches
git branch --all

Checkout and track remote branch
git checkout -t [BRANCH]


----------------------------------ooOOOOoo---------------------------------------
virtualbox:

Dyanmic size for the harddisk does not mean it will grow unlike vmware. So set the size next in the screen dialog to 20GB or else ubuntu updates will fail.

Guest additions need to be intalled prior to getting resized windows.

https://www.virtualbox.org/manual/ch04.html#idp46457703730512

Again, as with Linux hosts, we recommend using DKMS if it is available for the guest system. If it is not installed, use this command for Ubuntu/Debian systems:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install dkms

Insert the VBoxGuestAdditions.iso CD file into your Linux guest's virtual CD-ROM drive, exactly the same way as described for a Windows guest in Section 4.2.1.1, “Installation”.

Change to the directory where your CD-ROM drive is mounted and execute as root:

sh ./VBoxLinuxAdditions.run

Shared Folders from Guest OS:
With Linux guests, auto-mounted shared folders are mounted into the /media directory, along with the prefix sf_. For example, the shared folder myfiles would be mounted to /media/sf_myfiles on Linux and /mnt/sf_myfiles on Solaris.

The guest property /VirtualBox/GuestAdd/SharedFolders/MountPrefix determines the prefix that is used. Change that guest property to a value other than "sf" to change that prefix; see Section 4.7, “Guest properties” for details.

Note
Access to auto-mounted shared folders is only granted to the user group vboxsf, which is created by the VirtualBox Guest Additions installer. Hence guest users have to be member of that group to have read/write access or to have read-only access in case the folder is not mapped writable.
To change the mount directory to something other than /media, you can set the guest property /VirtualBox/GuestAdd/SharedFolders/MountDir.

usermod -a -G vboxsf $USER
reboot


----------------------------------ooOOOOoo---------------------------------------
Xterm:

http://scarygliders.net/2011/12/01/customize-xterm-the-original-and-best-terminal/

Introducing the .Xresources file.

and issue the following command.
xrdb -merge ~/.Xresources

Using this handy file, which you save in your home directory, you just plug in whatever you wish to customize your xterm sessions with.

Here’s my current .Xresources file contents…

xterm*faceName: DejaVu Sans Mono Book
xterm*faceSize: 11

! Every shell is a login shell by default (for inclusion of all necessary environment variables)
xterm*loginshell: true

! I like a LOT of scrollback...
xterm*savelines: 16384

! double-click to select whole URLs :D
xterm*charClass: 33:48,36-47:48,58-59:48,61:48,63-64:48,95:48,126:48

! DOS-box colours...
xterm*foreground: rgb:a8/a8/a8
xterm*background: rgb:00/00/00
xterm*color0: rgb:00/00/00
xterm*color1: rgb:a8/00/00
xterm*color2: rgb:00/a8/00
xterm*color3: rgb:a8/54/00
xterm*color4: rgb:00/00/a8
xterm*color5: rgb:a8/00/a8
xterm*color6: rgb:00/a8/a8
xterm*color7: rgb:a8/a8/a8
xterm*color8: rgb:54/54/54
xterm*color9: rgb:fc/54/54
xterm*color10: rgb:54/fc/54
xterm*color11: rgb:fc/fc/54
xterm*color12: rgb:54/54/fc
xterm*color13: rgb:fc/54/fc
xterm*color14: rgb:54/fc/fc
xterm*color15: rgb:fc/fc/fc

! right hand side scrollbar...
xterm*rightScrollBar: true
xterm*ScrollBar: true

! stop output to terminal from jumping down to bottom of scroll again
xterm*scrollTtyOutput: false



----------------------------------ooOOOOoo---------------------------------------
OVS

Adding new port to a bridge

sudo modprobe dummy
ip link setup dummy0
ip link set up dummy0
sudo ip link set up dummy0
sudo ovs-vsctl add-port s2 dummy0
sudo ovs-ctl show

Adding Mirror with all ports to a tap interface
sudo ovs-vsctl -- --id=@s2e2 get Port s2-eth2 -- --id=@m create Mirror name=tap select-all=true output-port=@s2e1 -- set bridge s2 mirrors=@m

Adding Mirror with a tap port and ignore any other ports inthe mirror
sudo ovs-vsctl -- --id=@m create mirror name=tap -- add bridge s2 mirrors @m -- --id=@p1 get port s2-eth3 -- --id=@p2 get port s2-eth4 -- --id=@tap get port s2-eth1 -- set mirror tap 'select_src_port=[@p1, @p2]' 'select_dst_port=[@p1, @p2]' -- set mirror tap output-port=@tap 

sudo ovs-vsctl list bridge s2
sudo ovs-vsctl list mirror tap

to remove a mirror 
sudo ovs-vsctl -- --id=@m get mirror tap -- remove bridge s2 mirrors @m

to remove all mirrors from a bridge
sudo ovs-vsctl clear bridge s2 mirrors

To check ovs status
service openvswitch-switch status
sudo ovs-vsctl show
sudo ovs-vsctl del-br

To stop ovs from starting on bootup

service openvswitch-switch stop

To start
service openvswitch-switch start





----------------------------------ooOOOOoo---------------------------------------

Mininet:

Create a topology by including the needed topology from the base topology as python class into the python script and choosing the same at startup.

tap topology file:

class mLinearTopo( Topo ):
    "Linear topology of k switches, with n hosts per switch."

    def build( self, k=2, n=1, **_opts):
        """k: number of switches
           n: number of hosts per switch"""
        self.k = k
        self.n = n

        if n == 1:
            genHostName = lambda i, j: 'h%s' % i
        else:
            genHostName = lambda i, j: 'h%ss%d' % ( j, i )

        lastSwitch = None
        for i in irange( 1, k ):
            # Add switch
            switch = self.addSwitch( 's%s' % i )
            # Add hosts to switch
            for j in irange( 1, n ):
                host = self.addHost( genHostName( i, j ) )
                self.addLink( host, switch )
                if i == 2:
                        print("Adding Second link for Switch 2 only")
                        self.addLink( host, switch )
            # Connect switch to previous
            if lastSwitch:
                self.addLink( switch, lastSwitch )
            lastSwitch = switch


Changes in the mn copy to run mininet:
from topo import mLinearTopo
TOPOS = { 'minimal': MinimalTopo,
          'linear': LinearTopo,
          'reversed': SingleSwitchReversedTopo,
          'single': SingleSwitchTopo,
          'tree': TreeTopo,
          'torus': TorusTopo,
          'mlinear' : mLinearTopo }


mininet> xterm h1
mininet> xterm h2
mininet> xterm h3
mininet> h3 python -m SimpleHTTPServer 80 &
mininet> h1 curl -I 10.0.0.3
mininet> h1 ping h3

----------------------------------ooOOOOoo---------------------------------------

curl programmatic compile dependencies.

sudo apt-get install libcurl4-openssl-dev

http://stackoverflow.com/questions/11471690/curl-h-no-such-file-or-directory


curl, repeated commands for bash

curl -I "10.0.0.3"

for (( ; ; )) ; do curl -I "10.0.0.3" ; done

for((i=0;i<100;i++)) do curl --connect-timeout 3 'http://www.google.com'; done;

for i in `seq 1 20`; do curl http://url; done

for i in $(seq 1 20); do curl "http://url"; done

ab -n 500 -c 20 http://www.example.com/

while :; do clear; your_command; sleep 2; done

To post usnig curl a JSON request 

curl -H "Content-Type: application/json" -X POST -d '[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]' http://127.0.0.1:8443/<name of the method that handles the request>

or put them in a file and pass it using 

curl -H "Content-Type: application/json" -X POST --data-binary @/tmp/rjson.txt http://127.0.0.1:8443/<name of the method that handles the request>

where the file is of the type 

[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]

to add a rule using CLI entirely so that we can manipulate the previous rule, use the following format 

echo '[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]' | url -H "Content-Type: application/json" -X POST -d @- http://127.0.0.1:8443/<name of the method that handles the request>

Simple curl command as above with tee can write to a file the output which can be listed out as part of the script and removed after use.

echo '[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]' | tee /tmp/scratch_pad.txt | curl -H "Content-Type: application/json" -X POST -d @- http://127.0.0.1:8443/<name of the method that handles the request>; echo ""; cat /tmp/scratch_pad.txt; rm /tmp/scratch_pad.txt

Simple curl command as above with tee can write to a file the output which can be listed out as part of the script and removed after use.
----------------------------------ooOOOOoo---------------------------------------

Simple webserver options 

while :; do nc -l 80 < simple_page.html ; done

simple HTML file

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

----------------------------------ooOOOOoo---------------------------------------

kernel source for ubuntu

Kernel Doxygen
http://kerneldox.com/


# First one installs headers for the image kernel
sudo apt-get source linux-image-$(uname -r)
# This installs the source for the image kernel
sudo apt-get install linux-source

# This may be needed 
sudo apt-get install libssl-dev


# After this if needed to allow kernel to be modified, do the following 
# per netmap instructions
*** kernel not configured.
*** 
*** The kernel directory must be ready for external module compilation.
*** You may need to issue the following or equivalent commands:
*** 
***     cd /usr/src/linux-headers-4.4.0-21
***     make oldconfig
***     make modules_prepare
***
***
***     Current configuration values:
***
*** kernel directory            /usr/src/linux-headers-4.4.0-21
*** linux version               - 
*** module file                 netmap.ko
*** subsystems                  generic monitor pipe vale
*** kernel sources              /usr/src/linux-source-4.4.0
*** requested drivers           i40e ixgbe igb e1000e e1000 veth.c forcedeth.c virtio_net.c r8169.c




https://wiki.ubuntu.com/Kernel/SourceCode

To enable dev_dbg messages in a specific kernel file:

#define DEBUG
before including <linux/device.h>

printk("%s: The calling process is \"%s\" (pid %i)\n",
        __FUNCTION__, current->comm, current->pid);

Writing Linux Device Drivers

The invaluable and comprehensive O’Reilly’s Linux Device Drivers book. The 3rd edition is for the 2.6 kernel and can be downloaded from http://lwn.net/Kernel/LDD3/
http://vger.kernel.org/~davem/skb_data.html – understanding skb’s for network drivers.
http://kerneltrap.org/
http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html – the massive but descriptive GNU C library
http://www.faqs.org/docs/kernel/index.html – Linux kernel module programming guide
http://people.nl.linux.org/ftp/pub/anoncvs/kernelnewbies/documents/kdoc/kernel-api/linuxkernelapi.html – The Linux kernel API


----------------------------------ooOOOOoo---------------------------------------
disk cleanup ubuntu

sudo apt-get clean
sudo apt-get autoclean
sudo apt-get autoremove
sudo apt-get remove --purge linux-image-X.X.XX-XX-generic
sudo apt-get remove --purge linux-image-X.X.XX-XX-generic
dpkg --get-selections | grep linux-image

du -sk * | sort -nr | head -10

ls -lh /boot

show top 10 biggest subdirs in the current dir.
du -sk * | sort -nr | head -10

du –sh /var/cache/apt/archives

# Run this and kill the process holding the files which are large.
sudo lsof | grep '(deleted)'


http://www.howtogeek.com/howto/28502/how-to-free-up-a-lot-of-disk-space-on-ubuntu-linux-by-deleting-cached-package-files/

use filelight or kDirStat to see where the disk space is going visually
check if you have old kernels for deletion
ls -lh /boot

cleaning packages
sudo apt-get autoremove

sudo apt-get autoclean

see list of all installed packages, sorted by size. If you see something big and don't use it - uninstall it

dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less

clean unused language files with translations (there are tons of them)
sudo apt-get install localepurge

check content of /var/tmp/
du -sh /var/tmp/

check also
man deborphan

Search for big files:
find / -type f -size +1024k

or

find / -size +50000  -exec ls -lahg {} \;


uname -r
to check your current version, then

dpkg -l linux-image-* linux-headers-*
to see all the old kernels and header files, then

sudo apt-get remove linux-image-<XYZ> linux-headers-<XYZ>
the apt-get remove command supports wildcards, so you can do apt-get remove linux-image 3.0.* linux-headers-3.0.* for example, to get rid of many at once.


Remove all old kernel versions automatically
Attention: If you've just upgraded the kernel, reboot before deleting the older versions!
Remember to check which kernel you are using type:

uname -r
Then as root:

sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
read that thread to be safe not to remove needed kernels with this command!


sudo du -h /var/log
Or as rubo77 points out in commends, you could use the NCurses disk usage tool:

sudo ncdu /var/log


http://askubuntu.com/questions/5980/how-do-i-free-up-disk-space

----------------------------------ooOOOOoo---------------------------------------
Identifying the NIC card in ubuntu

lspci | awk '/net/ {print $1}' | xargs -i% lspci -ks %
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
	Subsystem: Intel Corporation PRO/1000 MT Desktop Adapter
	Kernel driver in use: e1000
	Kernel modules: e1000
.....

05:00.0 Ethernet controller: Intel Corporation I210 Gigabit Network Connection (rev 03)
	Subsystem: Super Micro Computer Inc Device 1533
	Kernel driver in use: igb

lspci | grep -i net

sudo ls /sys/class/net

----------------------------------ooOOOOoo---------------------------------------
virtualenv error on virtualbox 

virtualenv --always-copy testenv

https://github.com/pypa/virtualenv/issues/209

later ffi.h error for pip install

sudo apt-get update
sudo apt-get install libffi-dev g++ libssl-dev

https://github.com/bigchaindb/bigchaindb/issues/24

----------------------------------ooOOOOoo---------------------------------------
Getting X window applications from remote headless server without Desktop installed on the server

ssh -XC user@192.168.10.140

http://askubuntu.com/questions/163155/how-do-i-access-my-remote-ubuntu-server-via-x-windows-from-my-mac

----------------------------------ooOOOOoo---------------------------------------
Link up/down

ifconfig eth0 up
ifconfig eth0 down
----------------------------------ooOOOOoo---------------------------------------
adding users

sudo useradd -d /home/<username> -m <username>

sudo passwd <username>

sudo chsh -s /bin/bash <username>

sudo adduser <username> sudo


http://www.howtogeek.com/howto/ubuntu/add-a-user-on-ubuntu-server/

https://help.ubuntu.com/lts/serverguide/user-management.html

http://askubuntu.com/questions/325807/arrow-keys-tab-complete-not-working

http://askubuntu.com/questions/7477/how-can-i-add-a-new-user-as-sudoer-using-the-command-line

----------------------------------ooOOOOoo---------------------------------------
Setting prompt to include user name and path 

export PS1='$(whoami)@$(hostname):$(pwd)$ '
export PS1='$(whoami)@${HOSTNAME%%.*} [$(pwd)]# '



http://superuser.com/questions/601181/how-to-display-current-path-in-command-prompt-in-linuxs-sh-not-bash


----------------------------------ooOOOOoo---------------------------------------

Search only c and h files including cpp

alias grepch="grep --include=*.[ch] --include=*.cpp -nr"
alias grepc="grep --include=*.[c] --include=*.cpp -nr"
alias greph="grep --include=*.[h] -nr"

https://austinmarton.wordpress.com/
----------------------------------ooOOOOoo---------------------------------------

Perform some operation in a loop from bash shell

Perform some operation for every line in the output of a command, e.g. add .txt to every file in a directory:

ls /var/* | while read fname; do cp $fname $fname.txt; done

Instead of:

for fname in `ls /var/*`; do cp $fname $fname.txt; done

http://tldp.org/LDP/abs/html/string-manipulation.html

----------------------------------ooOOOOoo---------------------------------------

useful linux commands

Check all running services
ps xa

Find a particular process
ps xa | grep processName

Kill a process
kill -KILL processPID

Close the current process
ctrl + c

Sleep a process
ctrl + z

Put the process to the background (where n is job number)
bg %n

Bring the process to the foreground (where n is job number)
fg %n

Show the list of current jobs
jobs

Search command history
ctrl + r

View a file page by page
cat somefile | less

Search for a file containing some string
grep -lir "searchstring" ~/somedir/*

Display the current path
pwd

Show kernel modules currently loaded
lsmod

Find a file
locate file

Create a gzip archive
tar -czf output.tar.gz input files

Secure copy from local to remote
scp localfile username@servername:/home/username/

Find all files that end in ‘.c’, contain the text “what i want” but not the text “what i dont”:
find . -name "*.c" -exec grep -lR 'what/ i/ want' {} \; | xargs grep -L 'what/ i/ dont'

Check all running services
ps xa

Find a particular process
ps xa | grep processName

Kill a process
kill -KILL processPID

Close the current process
ctrl + c

Sleep a process
ctrl + z

Put the process to the background (where n is job number)
bg %n

Bring the process to the foreground (where n is job number)
fg %n

Show the list of current jobs
jobs

Search command history
ctrl + r

View a file page by page
cat somefile | less

Search for a file containing some string
grep -lir "searchstring" ~/somedir/*

Display the current path
pwd

Show kernel modules currently loaded
lsmod

Find a file
locate file

Remount the filesystem in read/write
mount -o remount,rw /

Determine file type, architecture, etc:
file somefile

Search for sometext in a number of PDF files:
ls *.pdf | xargs -I{} pdftotext {} - | grep "sometext"

Read a binary file in hex:
od -c filename | less

----------------------------------ooOOOOoo---------------------------------------

C pointer arithmetic and warnings.

Compliant Solution
Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value (see INT36-EX2). The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.

#include <stdint.h>
 
void f(void) {
  char *ptr;
  /* ... */
  uintptr_t number = (uintptr_t)ptr; 
  /* ... */
}

----------------------------------ooOOOOoo---------------------------------------

C packing and alignment 

typedef struct
{
     char Data1;
     int Data2;
     unsigned short Data3;
     char Data4;

}__attribute__((packed, aligned(1))) sSampleStruct;  


http://stackoverflow.com/questions/11770451/what-is-the-meaning-of-attribute-packed-aligned4?rq=1

why you should not 

http://digitalvampire.org/blog/index.php/2006/07/31/why-you-shouldnt-use-__attribute__packed/

http://www.catb.org/esr/structure-packing/


----------------------------------ooOOOOoo---------------------------------------
changing hostname 

Ubuntu change hostname command


sudo hostname <new_name> 

but if that name is not known in /etc/hosts need to add an entry for 127.0.0.1 along with localhost to map to <new_name>


The procedure to change the computer name on Ubuntu Linux:

Type the following command to edit /etc/hostname using nano or vi text editor:
sudo nano /etc/hostname

Delete the old name and setup new name.
Next Edit the /etc/hosts file:
sudo nano /etc/hosts
This file contains the hostname, if not chaning for some reason go ahead and modify this file.
sudo nano /etc/hostname

Replace any occurrence of the existing computer name with your new one.
Reboot the system to changes take effect:
sudo reboot

http://www.cyberciti.biz/faq/ubuntu-change-hostname-command/

----------------------------------ooOOOOoo---------------------------------------
pip and boto, openssl and cherrypy

sudo apt install python-pip
pip install --upgrade pip
sudo pip install boto
sudo pip install pyopenssl
sudo pip install pyOpenSSL
sudo apt-get update
sudo apt-get install libffi-dev g++ libssl-dev



Installing
CherryPy can be easily installed via common Python package managers such as setuptools or pip.

$ easy_install cherrypy
$ pip install cherrypy
You may also get the latest CherryPy version by grabbing the source code from Github:

$ git clone https://github.com/cherrypy/cherrypy
$ cd cherrypy
$ python setup.py install

http://docs.cherrypy.org/en/latest/install.html

----------------------------------ooOOOOoo---------------------------------------

renewing dhcp ip address 

sudo dhclient -r

# For restarting networking in case of issues
sudo /etc/init.d/networking restart

http://www.howtogeek.com/168168/renew-dhcp-address-in-linux/
----------------------------------ooOOOOoo---------------------------------------

simple loops in bash

for i in `seq 1 100000`; do  echo $i; curl -I "192.168.127.12"; done

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
 
----------------------------------ooOOOOoo---------------------------------------

Buring an ISO image on MAC osX

Convert the iso to dmg image

hdiutil convert -format UDRW -o /path/to/target.img /path/to/ubuntu.iso

diskutil list

find the disk by removing and repeating and inserting and repeating the command to ensure the right drive is selected.

if disk3 is selected you can proceed to burn the dmg as follows

sudo dd if=/path/to/ubuntu-14.04-desktop-i386.dmg of=/dev/rdiskN bs=1m

where rdiskN will be rdisk3 for disk3. r is required part of the name.

http://computers.tutsplus.com/tutorials/how-to-create-a-bootable-ubuntu-usb-drive-for-pc-on-a-mac--cms-21187

If facing an error resource busy....

try this and repeat the dd command 

sudo diskutil unmountDisk /dev/disk2

http://raspberrypi.stackexchange.com/questions/9217/resource-busy-error-when-using-dd-to-copy-disk-img-to-sd-card


----------------------------------ooOOOOoo---------------------------------------

mounting usb drives in ubuntu

Step 1. Check the blockdevices and the file systems that are assigned to those block devices.

lsblk


Here you see the blokdevice sdb with partition /sdb1. But it's not mounted. There's no file assigned to it.

Step 2. What kind of device is sdb?

sudo lshw 
or

sudo lshw | less


So the USB stick - the block device /sdb - has the logical name /dev/sdb. And the FAT32 filesystem on that stick has the logical name /dev/sdb1.

Step 3. Mounting the USB-stick

We will mount /dev/sdb1 to /media/usbstick

sudo mkdir /media/usbstick

sudo mount -t vfat /dev/sdb1 /media/usbstick 
Read the manpage of mount for other options.

Step 4. Did it work?

lsblk

Yes, we can see that the filesystem on the USB stick is mounted to /media/usbstick

Addendum : if there are no logical names like /dev/sdb, you should first create them. See this information about setting up and controling loop devices with the losetup command

http://askubuntu.com/questions/285539/detect-and-mount-devices


----------------------------------ooOOOOoo---------------------------------------<commit_msg>cronjob using scripts<commit_after>
----------------------------------ooOOOOoo---------------------------------------

libpcap:

Solution:
Install libpcap-dev package. libpcap install fails,

sudo apt-get install libpcap-dev
....
user@ubuntu:/$ whereis pcap
pcap: /usr/include/pcap.h /usr/include/pcap /usr/share/man/man3/pcap.3pcap.gz


Issue:
user@ubuntu:~/gDocuments/code/C-Snippets/Sandbox$ gcc -o ldev ldev.c -lpcap
ldev.c:12:46: fatal error: pcap.h: No such file or directory

user@ubuntu:~/gDocuments/code/C-Snippets/Sandbox$ whereis pcap.h
pcap:

user@ubuntu:/$ sudo apt-get libpcap
[sudo] password for user: 
E: Invalid operation libpcap


----------------------------------ooOOOOoo---------------------------------------

pcap_open_live:

unable to cpature as non root user.

Solution:

Do the following as root

Add a capture group and add yourself to it

groupadd pcap
usermod -a -G pcap $USER
Next, change the group of tcpdump and set permissions

chgrp pcap /usr/sbin/tcpdump
chmod 750 /usr/sbin/tcpdump
Finally, use setcap to give tcpdump the necessary permissions

setcap cap_net_raw,cap_net_admin=eip /usr/sbin/tcpdump
Be careful, that this will allow everybody from the group pcap to manipulate network interfaces and read raw packets!


http://askubuntu.com/questions/530920/tcpdump-permissions-problem

More details from

http://peternixon.net/news/2012/01/28/configure-tcpdump-work-non-root-user-opensuse-using-file-system-capabilities/

Issue:

DEV: eno16777736
pcap_open_live(): eno16777736: You don't have permission to capture on that device (socket: Operation not permitted)


----------------------------------ooOOOOoo---------------------------------------

wireshark:
sudo apt-get install wireshark

# Allowing non super users to capture the packets
sudo groupadd wireshark
sudo usermod -a -G wireshark $USER
sudo chgrp wireshark /usr/bin/dumpcap
sudo chmod 750 /usr/bin/dumpcap
sudo setcap cap_net_raw,cap_net_admin=eip /usr/bin/dumpcap
sudo getcap /usr/bin/dumpcap

# Wireshark display filter
ip.addr == 10.0.0.1 && ip.ttl == 111

# using tshark with capture filter and printing summary to console and capturing full
# packets to the file.
sudo tshark -f "host 172.16.17.32" -i enp0s8 -w /tmp/ars.pcap -P

# starting wireshark with a pcap file for analysis
 sudo wireshark /tmp/ars.pcap "ip.addr == 172.16.17.32"


https://ask.wireshark.org/questions/16343/install-wireshark-on-ubuntu
https://wiki.wireshark.org/CaptureFilters
https://www.wireshark.org/docs/wsug_html_chunked/AppToolstshark.html


----------------------------------ooOOOOoo---------------------------------------

git:

Once two factor authentication is enabled for password use the token when doing git operations using https.

https://help.github.com/articles/caching-your-github-password-in-git/


Unable to push after two factor authentication enabled.

http://stackoverflow.com/questions/17659206/git-push-results-in-authentication-failed

If you enabled two-factor authentication in your Github account you won't be able to push via HTTPS using your accounts password. Instead you need to generate a personal access token. This can be done in the application settings of your Github account. Using this token as your password should allow you to push to your remote repository via HTTPS. Use your username as usual.
https://help.github.com/articles/https-cloning-errors#provide-access-token-if-2fa-enabled

You may also need to update the origin for your repository if set to https:

git remote -v 
git remote remove origin 
// Did not work : git remote add origin <EMAIL>:user/repo.git  

you can also do git remote add origin https://username:access-token@github.com/username/repo.git to store your personal access token. 

// Did not work: https://help.github.com/articles/caching-your-github-password-in-git/

git remote set-url origin https://github.com/zkirkland/Random-Python-Tests.git
This one works. But after wards you need to add 
git push --set-upstream origin master

may have to do git pull origin master 

try git branch and git pull commands to see if they are fine and test with git remote -v

https://confluence.atlassian.com/bitbucketserver/permanently-authenticating-with-git-repositories-776639846.html

git config --global credential.helper 'cache --timeout 72000'
git config --global color.ui true

Check out a new repo
git clone git://location/proj.git

Actually see what’s going on
gitk --all

Interactive adding for commits
git add -i

Unstage changes
git reset HEAD

Return to last committed state
git reset --hard

Undo a merge
git reset --hard [ID]
git reset --hard HEAD~[number back]

Undo a commit with message
git reset --soft HEAD^

Modify a commit
git commit --amend

Add more files to existing commit
git add ...
git commit --amend -C HEAD

Branches have diverged – just get up stream
git reset --hard @{upstream}

Checkout specific version of a file
git checkout [ID] [FILE]

Updating submodules
git submodule init
git submodule sync
git submodule update

Go through every submodule and checkout the head
git submodule foreach git checkout master
git submodule foreach git pull origin master
Or automatically merge
git submodule update --merge

Show difference between two branches
git diff [BRANCH 1]..[BRANCH 2]

Difference between two commits
git diff [ID1] [ID2]

Merge a branch back into master
git checkout master
git merge [BRANCH]

Show all branches
git branch --all

Checkout and track remote branch
git checkout -t [BRANCH]


----------------------------------ooOOOOoo---------------------------------------
virtualbox:

Dyanmic size for the harddisk does not mean it will grow unlike vmware. So set the size next in the screen dialog to 20GB or else ubuntu updates will fail.

Guest additions need to be intalled prior to getting resized windows.

https://www.virtualbox.org/manual/ch04.html#idp46457703730512

Again, as with Linux hosts, we recommend using DKMS if it is available for the guest system. If it is not installed, use this command for Ubuntu/Debian systems:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install dkms

Insert the VBoxGuestAdditions.iso CD file into your Linux guest's virtual CD-ROM drive, exactly the same way as described for a Windows guest in Section 4.2.1.1, “Installation”.

Change to the directory where your CD-ROM drive is mounted and execute as root:

sh ./VBoxLinuxAdditions.run

Shared Folders from Guest OS:
With Linux guests, auto-mounted shared folders are mounted into the /media directory, along with the prefix sf_. For example, the shared folder myfiles would be mounted to /media/sf_myfiles on Linux and /mnt/sf_myfiles on Solaris.

The guest property /VirtualBox/GuestAdd/SharedFolders/MountPrefix determines the prefix that is used. Change that guest property to a value other than "sf" to change that prefix; see Section 4.7, “Guest properties” for details.

Note
Access to auto-mounted shared folders is only granted to the user group vboxsf, which is created by the VirtualBox Guest Additions installer. Hence guest users have to be member of that group to have read/write access or to have read-only access in case the folder is not mapped writable.
To change the mount directory to something other than /media, you can set the guest property /VirtualBox/GuestAdd/SharedFolders/MountDir.

usermod -a -G vboxsf $USER
reboot


----------------------------------ooOOOOoo---------------------------------------
Xterm:

http://scarygliders.net/2011/12/01/customize-xterm-the-original-and-best-terminal/

Introducing the .Xresources file.

and issue the following command.
xrdb -merge ~/.Xresources

Using this handy file, which you save in your home directory, you just plug in whatever you wish to customize your xterm sessions with.

Here’s my current .Xresources file contents…

xterm*faceName: DejaVu Sans Mono Book
xterm*faceSize: 11

! Every shell is a login shell by default (for inclusion of all necessary environment variables)
xterm*loginshell: true

! I like a LOT of scrollback...
xterm*savelines: 16384

! double-click to select whole URLs :D
xterm*charClass: 33:48,36-47:48,58-59:48,61:48,63-64:48,95:48,126:48

! DOS-box colours...
xterm*foreground: rgb:a8/a8/a8
xterm*background: rgb:00/00/00
xterm*color0: rgb:00/00/00
xterm*color1: rgb:a8/00/00
xterm*color2: rgb:00/a8/00
xterm*color3: rgb:a8/54/00
xterm*color4: rgb:00/00/a8
xterm*color5: rgb:a8/00/a8
xterm*color6: rgb:00/a8/a8
xterm*color7: rgb:a8/a8/a8
xterm*color8: rgb:54/54/54
xterm*color9: rgb:fc/54/54
xterm*color10: rgb:54/fc/54
xterm*color11: rgb:fc/fc/54
xterm*color12: rgb:54/54/fc
xterm*color13: rgb:fc/54/fc
xterm*color14: rgb:54/fc/fc
xterm*color15: rgb:fc/fc/fc

! right hand side scrollbar...
xterm*rightScrollBar: true
xterm*ScrollBar: true

! stop output to terminal from jumping down to bottom of scroll again
xterm*scrollTtyOutput: false



----------------------------------ooOOOOoo---------------------------------------
OVS

Adding new port to a bridge

sudo modprobe dummy
ip link setup dummy0
ip link set up dummy0
sudo ip link set up dummy0
sudo ovs-vsctl add-port s2 dummy0
sudo ovs-ctl show

Adding Mirror with all ports to a tap interface
sudo ovs-vsctl -- --id=@s2e2 get Port s2-eth2 -- --id=@m create Mirror name=tap select-all=true output-port=@s2e1 -- set bridge s2 mirrors=@m

Adding Mirror with a tap port and ignore any other ports inthe mirror
sudo ovs-vsctl -- --id=@m create mirror name=tap -- add bridge s2 mirrors @m -- --id=@p1 get port s2-eth3 -- --id=@p2 get port s2-eth4 -- --id=@tap get port s2-eth1 -- set mirror tap 'select_src_port=[@p1, @p2]' 'select_dst_port=[@p1, @p2]' -- set mirror tap output-port=@tap 

sudo ovs-vsctl list bridge s2
sudo ovs-vsctl list mirror tap

to remove a mirror 
sudo ovs-vsctl -- --id=@m get mirror tap -- remove bridge s2 mirrors @m

to remove all mirrors from a bridge
sudo ovs-vsctl clear bridge s2 mirrors

To check ovs status
service openvswitch-switch status
sudo ovs-vsctl show
sudo ovs-vsctl del-br

To stop ovs from starting on bootup

service openvswitch-switch stop

To start
service openvswitch-switch start





----------------------------------ooOOOOoo---------------------------------------

Mininet:

Create a topology by including the needed topology from the base topology as python class into the python script and choosing the same at startup.

tap topology file:

class mLinearTopo( Topo ):
    "Linear topology of k switches, with n hosts per switch."

    def build( self, k=2, n=1, **_opts):
        """k: number of switches
           n: number of hosts per switch"""
        self.k = k
        self.n = n

        if n == 1:
            genHostName = lambda i, j: 'h%s' % i
        else:
            genHostName = lambda i, j: 'h%ss%d' % ( j, i )

        lastSwitch = None
        for i in irange( 1, k ):
            # Add switch
            switch = self.addSwitch( 's%s' % i )
            # Add hosts to switch
            for j in irange( 1, n ):
                host = self.addHost( genHostName( i, j ) )
                self.addLink( host, switch )
                if i == 2:
                        print("Adding Second link for Switch 2 only")
                        self.addLink( host, switch )
            # Connect switch to previous
            if lastSwitch:
                self.addLink( switch, lastSwitch )
            lastSwitch = switch


Changes in the mn copy to run mininet:
from topo import mLinearTopo
TOPOS = { 'minimal': MinimalTopo,
          'linear': LinearTopo,
          'reversed': SingleSwitchReversedTopo,
          'single': SingleSwitchTopo,
          'tree': TreeTopo,
          'torus': TorusTopo,
          'mlinear' : mLinearTopo }


mininet> xterm h1
mininet> xterm h2
mininet> xterm h3
mininet> h3 python -m SimpleHTTPServer 80 &
mininet> h1 curl -I 10.0.0.3
mininet> h1 ping h3

----------------------------------ooOOOOoo---------------------------------------

curl programmatic compile dependencies.

sudo apt-get install libcurl4-openssl-dev

http://stackoverflow.com/questions/11471690/curl-h-no-such-file-or-directory


curl, repeated commands for bash

curl -I "10.0.0.3"

for (( ; ; )) ; do curl -I "10.0.0.3" ; done

for((i=0;i<100;i++)) do curl --connect-timeout 3 'http://www.google.com'; done;

for i in `seq 1 20`; do curl http://url; done

for i in $(seq 1 20); do curl "http://url"; done

ab -n 500 -c 20 http://www.example.com/

while :; do clear; your_command; sleep 2; done

To post usnig curl a JSON request 

curl -H "Content-Type: application/json" -X POST -d '[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]' http://127.0.0.1:8443/<name of the method that handles the request>

or put them in a file and pass it using 

curl -H "Content-Type: application/json" -X POST --data-binary @/tmp/rjson.txt http://127.0.0.1:8443/<name of the method that handles the request>

where the file is of the type 

[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]

to add a rule using CLI entirely so that we can manipulate the previous rule, use the following format 

echo '[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]' | url -H "Content-Type: application/json" -X POST -d @- http://127.0.0.1:8443/<name of the method that handles the request>

Simple curl command as above with tee can write to a file the output which can be listed out as part of the script and removed after use.

echo '[{ "key1:"val1", "key2":"val2", "key3":"val3", "key4":"val4", "key5":"val5", "key6":"val6" }]' | tee /tmp/scratch_pad.txt | curl -H "Content-Type: application/json" -X POST -d @- http://127.0.0.1:8443/<name of the method that handles the request>; echo ""; cat /tmp/scratch_pad.txt; rm /tmp/scratch_pad.txt

Simple curl command as above with tee can write to a file the output which can be listed out as part of the script and removed after use.
----------------------------------ooOOOOoo---------------------------------------

Simple webserver options 

while :; do nc -l 80 < simple_page.html ; done

simple HTML file

<!DOCTYPE html>
<html>
<body>

<h1>My First Heading</h1>

<p>My first paragraph.</p>

</body>
</html>

----------------------------------ooOOOOoo---------------------------------------

kernel source for ubuntu

Kernel Doxygen
http://kerneldox.com/


# First one installs headers for the image kernel
sudo apt-get source linux-image-$(uname -r)
# This installs the source for the image kernel
sudo apt-get install linux-source

# This may be needed 
sudo apt-get install libssl-dev


# After this if needed to allow kernel to be modified, do the following 
# per netmap instructions
*** kernel not configured.
*** 
*** The kernel directory must be ready for external module compilation.
*** You may need to issue the following or equivalent commands:
*** 
***     cd /usr/src/linux-headers-4.4.0-21
***     make oldconfig
***     make modules_prepare
***
***
***     Current configuration values:
***
*** kernel directory            /usr/src/linux-headers-4.4.0-21
*** linux version               - 
*** module file                 netmap.ko
*** subsystems                  generic monitor pipe vale
*** kernel sources              /usr/src/linux-source-4.4.0
*** requested drivers           i40e ixgbe igb e1000e e1000 veth.c forcedeth.c virtio_net.c r8169.c




https://wiki.ubuntu.com/Kernel/SourceCode

To enable dev_dbg messages in a specific kernel file:

#define DEBUG
before including <linux/device.h>

printk("%s: The calling process is \"%s\" (pid %i)\n",
        __FUNCTION__, current->comm, current->pid);

Writing Linux Device Drivers

The invaluable and comprehensive O’Reilly’s Linux Device Drivers book. The 3rd edition is for the 2.6 kernel and can be downloaded from http://lwn.net/Kernel/LDD3/
http://vger.kernel.org/~davem/skb_data.html – understanding skb’s for network drivers.
http://kerneltrap.org/
http://www.cs.utah.edu/dept/old/texinfo/glibc-manual-0.02/library_toc.html – the massive but descriptive GNU C library
http://www.faqs.org/docs/kernel/index.html – Linux kernel module programming guide
http://people.nl.linux.org/ftp/pub/anoncvs/kernelnewbies/documents/kdoc/kernel-api/linuxkernelapi.html – The Linux kernel API


----------------------------------ooOOOOoo---------------------------------------
disk cleanup ubuntu

sudo apt-get clean
sudo apt-get autoclean
sudo apt-get autoremove
sudo apt-get remove --purge linux-image-X.X.XX-XX-generic
sudo apt-get remove --purge linux-image-X.X.XX-XX-generic
dpkg --get-selections | grep linux-image

du -sk * | sort -nr | head -10

ls -lh /boot

show top 10 biggest subdirs in the current dir.
du -sk * | sort -nr | head -10

du –sh /var/cache/apt/archives

# Run this and kill the process holding the files which are large.
sudo lsof | grep '(deleted)'


http://www.howtogeek.com/howto/28502/how-to-free-up-a-lot-of-disk-space-on-ubuntu-linux-by-deleting-cached-package-files/

use filelight or kDirStat to see where the disk space is going visually
check if you have old kernels for deletion
ls -lh /boot

cleaning packages
sudo apt-get autoremove

sudo apt-get autoclean

see list of all installed packages, sorted by size. If you see something big and don't use it - uninstall it

dpkg-query -W --showformat='${Installed-Size} ${Package}\n' | sort -nr | less

clean unused language files with translations (there are tons of them)
sudo apt-get install localepurge

check content of /var/tmp/
du -sh /var/tmp/

check also
man deborphan

Search for big files:
find / -type f -size +1024k

or

find / -size +50000  -exec ls -lahg {} \;


uname -r
to check your current version, then

dpkg -l linux-image-* linux-headers-*
to see all the old kernels and header files, then

sudo apt-get remove linux-image-<XYZ> linux-headers-<XYZ>
the apt-get remove command supports wildcards, so you can do apt-get remove linux-image 3.0.* linux-headers-3.0.* for example, to get rid of many at once.


Remove all old kernel versions automatically
Attention: If you've just upgraded the kernel, reboot before deleting the older versions!
Remember to check which kernel you are using type:

uname -r
Then as root:

sudo apt-get remove --purge $(dpkg -l 'linux-*' | sed '/^ii/!d;/'"$(uname -r | sed "s/\(.*\)-\([^0-9]\+\)/\1/")"'/d;s/^[^ ]* [^ ]* \([^ ]*\).*/\1/;/[0-9]/!d')
read that thread to be safe not to remove needed kernels with this command!


sudo du -h /var/log
Or as rubo77 points out in commends, you could use the NCurses disk usage tool:

sudo ncdu /var/log


http://askubuntu.com/questions/5980/how-do-i-free-up-disk-space

----------------------------------ooOOOOoo---------------------------------------
Identifying the NIC card in ubuntu

lspci | awk '/net/ {print $1}' | xargs -i% lspci -ks %
00:03.0 Ethernet controller: Intel Corporation 82540EM Gigabit Ethernet Controller (rev 02)
	Subsystem: Intel Corporation PRO/1000 MT Desktop Adapter
	Kernel driver in use: e1000
	Kernel modules: e1000
.....

05:00.0 Ethernet controller: Intel Corporation I210 Gigabit Network Connection (rev 03)
	Subsystem: Super Micro Computer Inc Device 1533
	Kernel driver in use: igb

lspci | grep -i net

sudo ls /sys/class/net

----------------------------------ooOOOOoo---------------------------------------
virtualenv error on virtualbox 

virtualenv --always-copy testenv

https://github.com/pypa/virtualenv/issues/209

later ffi.h error for pip install

sudo apt-get update
sudo apt-get install libffi-dev g++ libssl-dev

https://github.com/bigchaindb/bigchaindb/issues/24

----------------------------------ooOOOOoo---------------------------------------
Getting X window applications from remote headless server without Desktop installed on the server

ssh -XC user@192.168.10.140

http://askubuntu.com/questions/163155/how-do-i-access-my-remote-ubuntu-server-via-x-windows-from-my-mac

----------------------------------ooOOOOoo---------------------------------------
Link up/down

ifconfig eth0 up
ifconfig eth0 down
----------------------------------ooOOOOoo---------------------------------------
adding users

sudo useradd -d /home/<username> -m <username>

sudo passwd <username>

sudo chsh -s /bin/bash <username>

sudo adduser <username> sudo


http://www.howtogeek.com/howto/ubuntu/add-a-user-on-ubuntu-server/

https://help.ubuntu.com/lts/serverguide/user-management.html

http://askubuntu.com/questions/325807/arrow-keys-tab-complete-not-working

http://askubuntu.com/questions/7477/how-can-i-add-a-new-user-as-sudoer-using-the-command-line

----------------------------------ooOOOOoo---------------------------------------
Setting prompt to include user name and path 

export PS1='$(whoami)@$(hostname):$(pwd)$ '
export PS1='$(whoami)@${HOSTNAME%%.*} [$(pwd)]# '



http://superuser.com/questions/601181/how-to-display-current-path-in-command-prompt-in-linuxs-sh-not-bash


----------------------------------ooOOOOoo---------------------------------------

Search only c and h files including cpp

alias grepch="grep --include=*.[ch] --include=*.cpp -nr"
alias grepc="grep --include=*.[c] --include=*.cpp -nr"
alias greph="grep --include=*.[h] -nr"

https://austinmarton.wordpress.com/
----------------------------------ooOOOOoo---------------------------------------

Perform some operation in a loop from bash shell

Perform some operation for every line in the output of a command, e.g. add .txt to every file in a directory:

ls /var/* | while read fname; do cp $fname $fname.txt; done

Instead of:

for fname in `ls /var/*`; do cp $fname $fname.txt; done

http://tldp.org/LDP/abs/html/string-manipulation.html

----------------------------------ooOOOOoo---------------------------------------

useful linux commands

Check all running services
ps xa

Find a particular process
ps xa | grep processName

Kill a process
kill -KILL processPID

Close the current process
ctrl + c

Sleep a process
ctrl + z

Put the process to the background (where n is job number)
bg %n

Bring the process to the foreground (where n is job number)
fg %n

Show the list of current jobs
jobs

Search command history
ctrl + r

View a file page by page
cat somefile | less

Search for a file containing some string
grep -lir "searchstring" ~/somedir/*

Display the current path
pwd

Show kernel modules currently loaded
lsmod

Find a file
locate file

Create a gzip archive
tar -czf output.tar.gz input files

Secure copy from local to remote
scp localfile username@servername:/home/username/

Find all files that end in ‘.c’, contain the text “what i want” but not the text “what i dont”:
find . -name "*.c" -exec grep -lR 'what/ i/ want' {} \; | xargs grep -L 'what/ i/ dont'

Check all running services
ps xa

Find a particular process
ps xa | grep processName

Kill a process
kill -KILL processPID

Close the current process
ctrl + c

Sleep a process
ctrl + z

Put the process to the background (where n is job number)
bg %n

Bring the process to the foreground (where n is job number)
fg %n

Show the list of current jobs
jobs

Search command history
ctrl + r

View a file page by page
cat somefile | less

Search for a file containing some string
grep -lir "searchstring" ~/somedir/*

Display the current path
pwd

Show kernel modules currently loaded
lsmod

Find a file
locate file

Remount the filesystem in read/write
mount -o remount,rw /

Determine file type, architecture, etc:
file somefile

Search for sometext in a number of PDF files:
ls *.pdf | xargs -I{} pdftotext {} - | grep "sometext"

Read a binary file in hex:
od -c filename | less

----------------------------------ooOOOOoo---------------------------------------

C pointer arithmetic and warnings.

Compliant Solution
Any valid pointer to void can be converted to intptr_t or uintptr_t and back with no change in value (see INT36-EX2). The C Standard guarantees that a pointer to void may be converted to or from a pointer to any object type and back again and that the result must compare equal to the original pointer. Consequently, converting directly from a char * pointer to a uintptr_t, as in this compliant solution, is allowed on implementations that support the uintptr_t type.

#include <stdint.h>
 
void f(void) {
  char *ptr;
  /* ... */
  uintptr_t number = (uintptr_t)ptr; 
  /* ... */
}

----------------------------------ooOOOOoo---------------------------------------

C packing and alignment 

typedef struct
{
     char Data1;
     int Data2;
     unsigned short Data3;
     char Data4;

}__attribute__((packed, aligned(1))) sSampleStruct;  


http://stackoverflow.com/questions/11770451/what-is-the-meaning-of-attribute-packed-aligned4?rq=1

why you should not 

http://digitalvampire.org/blog/index.php/2006/07/31/why-you-shouldnt-use-__attribute__packed/

http://www.catb.org/esr/structure-packing/


----------------------------------ooOOOOoo---------------------------------------
changing hostname 

Ubuntu change hostname command


sudo hostname <new_name> 

but if that name is not known in /etc/hosts need to add an entry for 127.0.0.1 along with localhost to map to <new_name>


The procedure to change the computer name on Ubuntu Linux:

Type the following command to edit /etc/hostname using nano or vi text editor:
sudo nano /etc/hostname

Delete the old name and setup new name.
Next Edit the /etc/hosts file:
sudo nano /etc/hosts
This file contains the hostname, if not chaning for some reason go ahead and modify this file.
sudo nano /etc/hostname

Replace any occurrence of the existing computer name with your new one.
Reboot the system to changes take effect:
sudo reboot

http://www.cyberciti.biz/faq/ubuntu-change-hostname-command/

----------------------------------ooOOOOoo---------------------------------------
pip and boto, openssl and cherrypy

sudo apt install python-pip
pip install --upgrade pip
sudo pip install boto
sudo pip install pyopenssl
sudo pip install pyOpenSSL
sudo apt-get update
sudo apt-get install libffi-dev g++ libssl-dev



Installing
CherryPy can be easily installed via common Python package managers such as setuptools or pip.

$ easy_install cherrypy
$ pip install cherrypy
You may also get the latest CherryPy version by grabbing the source code from Github:

$ git clone https://github.com/cherrypy/cherrypy
$ cd cherrypy
$ python setup.py install

http://docs.cherrypy.org/en/latest/install.html

----------------------------------ooOOOOoo---------------------------------------

renewing dhcp ip address 

sudo dhclient -r

# For restarting networking in case of issues
sudo /etc/init.d/networking restart

http://www.howtogeek.com/168168/renew-dhcp-address-in-linux/
----------------------------------ooOOOOoo---------------------------------------

simple loops in bash

for i in `seq 1 100000`; do  echo $i; curl -I "192.168.127.12"; done

http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-7.html
 
----------------------------------ooOOOOoo---------------------------------------

Buring an ISO image on MAC osX

Convert the iso to dmg image

hdiutil convert -format UDRW -o /path/to/target.img /path/to/ubuntu.iso

diskutil list

find the disk by removing and repeating and inserting and repeating the command to ensure the right drive is selected.

if disk3 is selected you can proceed to burn the dmg as follows

sudo dd if=/path/to/ubuntu-14.04-desktop-i386.dmg of=/dev/rdiskN bs=1m

where rdiskN will be rdisk3 for disk3. r is required part of the name.

http://computers.tutsplus.com/tutorials/how-to-create-a-bootable-ubuntu-usb-drive-for-pc-on-a-mac--cms-21187

If facing an error resource busy....

try this and repeat the dd command 

sudo diskutil unmountDisk /dev/disk2

http://raspberrypi.stackexchange.com/questions/9217/resource-busy-error-when-using-dd-to-copy-disk-img-to-sd-card


----------------------------------ooOOOOoo---------------------------------------

mounting usb drives in ubuntu

Step 1. Check the blockdevices and the file systems that are assigned to those block devices.

lsblk


Here you see the blokdevice sdb with partition /sdb1. But it's not mounted. There's no file assigned to it.

Step 2. What kind of device is sdb?

sudo lshw 
or

sudo lshw | less


So the USB stick - the block device /sdb - has the logical name /dev/sdb. And the FAT32 filesystem on that stick has the logical name /dev/sdb1.

Step 3. Mounting the USB-stick

We will mount /dev/sdb1 to /media/usbstick

sudo mkdir /media/usbstick

sudo mount -t vfat /dev/sdb1 /media/usbstick 
Read the manpage of mount for other options.

Step 4. Did it work?

lsblk

Yes, we can see that the filesystem on the USB stick is mounted to /media/usbstick

Addendum : if there are no logical names like /dev/sdb, you should first create them. See this information about setting up and controling loop devices with the losetup command

http://askubuntu.com/questions/285539/detect-and-mount-devices


----------------------------------ooOOOOoo---------------------------------------
cronjab using commandline 

crontab -l | { cat; echo "0 0 0 0 0 some entry"; } | crontab -


command="php $INSTALL/indefero/scripts/gitcron.php"
job="0 0 * * 0 $command"
cat <(fgrep -i -v "$command" <(crontab -l)) <(echo "$job") | crontab -


http://stackoverflow.com/questions/878600/how-to-create-cronjob-using-bash

----------------------------------ooOOOOoo---------------------------------------
----------------------------------ooOOOOoo---------------------------------------
{'fraction_non_alphanumeric': 0.1646182367481461, 'fraction_numerical': 0.023928865696237298, 'mean_word_length': 2.618671967202932, 'pattern_counts': {'":': 40, '<': 60, '<?xml version=': 0, '>': 69, 'https://': 32, 'lorem ipsum': 0, 'www.': 22, 'xml': 0}, 'pii_count': 30, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4296771', 'n_tokens_mistral': 19067, 'n_tokens_neox': 17399, 'n_words': 6731}