Search This Blog

Monday, June 17, 2013

Openstack or Linux or bash cheat sheet

This post is a work in progress.
  • How to generate a list of commands base on input list. 
Per one input line one output command
# echo a b c | xargs -n 1 echo 'this is ' 
this is  a
this is  b
this is  c

Practical demo of how to delete all your cloud servers
# nova --no-cache list | grep '[|]' | awk '{print $2}' | tail -n +2 | xargs -n1 echo nova delete
nova delete 0dafascd-e7e5-4531-9542-25132338a3fc
nova delete ffasff56-ef5a-42e8-aa96-594d14538def
nova delete ad509afa-0cc8-111b-a681-7c56cc354957
nova delete b9bfafaf-073d-4732-a9c0-2e6720938357
  • Testing if you can establish a TCP session
$ nc -v -p 1185 92.52.111.222 80
Connection to 92.52.111.222 80 port [tcp/http] succeeded!
  • some of the useful CLIs
fold - Filter for folding lines. This breaks the lines to have a maximum of x width column position (or bytes).
column - columnate lists

  • How to check TCP / UDP network and socket statistics 
export file=/tmp/netstat.txt
netstat  -nntulpa &> $file

cat $file | grep tcp | awk ' { print $6 } ' | sort | uniq
cat $file 2 | grep udp

cat $file | grep tcp | awk ' { print $6 } ' | sort | uniq
CLOSE_WAIT
CLOSING
ESTABLISHED
FIN_WAIT1
FIN_WAIT2
LAST_ACK
LISTEN
SYN_RECV
SYN_SENT
TIME_WAIT

cat $file | grep tcp | awk ' { print $6 } ' | sort | uniq | while read STATE; do echo $STATE; grep $STATE $file | wc -l; done
CLOSE_WAIT
2
CLOSING
8
ESTABLISHED
53
FIN_WAIT1
15
FIN_WAIT2
0
LAST_ACK
136
LISTEN
20
SYN_RECV
166
SYN_SENT
0
TIME_WAIT
2

Other useful links: link1link2link3
  • How to sort files based on file size 
$ find . -mount -type f -ls|sort -rnk7 |head -30|awk '{printf "%10d MB\t%s\n",($7/1024)/1024,$NF}'

        52 MB   ./lib/libwireshark.so.2.0.2
        17 MB   ./lib/x86_64-linux-gnu/libicudata.so.48.1.1

  • How to cat and highlight a word in text
$ cat file | egrep --color=always "pattern|$"
$echo -n 'ello' | ( read a; read -u1 b ; echo "1st read : - $a -"; echo "2th read : = $b =" )
test
1st read : - ello -
2th read : = test =
  • How to truncated and shrink the text output to your terminal screen width
$ tcpdump -l -s0 -nn -i 0.0 'host 192.168.99.126 and port 443 and ( tcp[13]=2 )' | cut -c -$(tput cols)

  • How to print a file without the first line

  • $ cat tmp1
    a1
    a2
    a3
    a4
    

    Remove the fist line
    $ cat tmp1 | tail -n+2
    a2
    a3
    a4
    

    Remove the line #2 and #3
    cat tmp1 | sed '2,3d'
    a1
    a4
    

    Remove the first 2 lines
    $ cat tmp1 | tail -n+3
    a3
    a4
    

  • How to extract IP address from tcpdump output

  • $ tcpdump -nr attack.log
    21:35:49.553423 IP 162.13.0.27.22 > 82.44.149.5.51227: Flags [P.], seq 567291273:567291325, ack 2916928547, win 312, length 52
    21:35:49.573227 IP 82.44.149.5.51227 > 162.13.0.27.22: Flags [.], ack 52, win 16516, length 0
    

    Extract source IP and port
    $ tcpdump -nr attack.log | tmp.xt |awk '{print $3}'
    162.13.0.27.22
    82.44.149.5.51227
    

    Strip of the port number
    $ tcpdump -nr attack.log | awk '{print $3}' | grep -oE '[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}\.[0-9]{1,}'
    162.13.0.27
    82.44.149.5
    
  • How to count strings in a text using awk
$ cat  | awk '  { count+=NF } END { print count;}'
1 2 aaaa :rrr :ddjf -dd rrd ccc zz
1 2 3 4 444; -d df
16

No comments:

Post a Comment