niedziela, 30 października 2016

hacker range bash - case about tr switch signs

marek@ubuntu:~/dev/bash$ echo "int i=(int)5.8 (23 + 5)*2" | tr '()' '[]'
int i=[int]5.8 [23 + 5]*2
marek@ubuntu:~/dev/bash$ echo "int i=(int)5.8 (23 + 5)*2" | tr "(" "[" | tr ")" "]"

selective file remove

shopt -s extglob
root@ubuntu:~/dev/python/tmp# for i in  1 2 3 4 5; do touch $i; done
root@ubuntu:~/dev/python/tmp# ll
razem 0
-rw-r--r-- 1 root root 0 paź 30 14:37 3
-rw-r--r-- 1 root root 0 paź 30 14:37 2
-rw-r--r-- 1 root root 0 paź 30 14:37 1
-rw-r--r-- 1 root root 0 paź 30 14:37 5
-rw-r--r-- 1 root root 0 paź 30 14:37 4

root@ubuntu:~/dev/python/tmp# rm -v !("3")
usunięty '1'
usunięty '2'
usunięty '4'
usunięty '5'

root@ubuntu:~/dev/python/tmp# ls -ltr
razem 0

piątek, 28 października 2016

Sum Dir Size awk

[root@dimlogsrvvmd1 indices]# du -sk * | sort -nr | grep prod-crm | awk '{total = total + $1}END{print "CRM log sum = "total/1024/1024 " gb"}'

CRM log sum = 97.6993 gb

czwartek, 27 października 2016

check app deployment on docker wildfly

#!/bin/bash

for i in `docker ps -q`; do
        for j in `cat lista.dat| grep curl | grep -v \# |  awk '{print $NF}' | awk -F"/" '{print $2}'`; do
                echo $i && docker exec -it $i /opt/wildfly9/bin/jboss-cli.sh --connect "deployment-info --name=$j"
        done
done

1. first loop get docker container id
2. second loop are get list of deployed artifact's

example of loop in loop - nested loop


#!/bin/bash


outer=1             # Set outer loop counter.

# Beginning of outer loop.
for a in 1 2 3 4 5
do
  echo "Pass $outer in outer loop."
  echo "---------------------"
  inner=1           # Reset inner loop counter.

  # ===============================================
  # Beginning of inner loop.
  for b in 1 2 3 4 5
  do
    echo "Pass $inner in inner loop."
    let "inner+=1"  # Increment inner loop counter.
  done
  # End of inner loop.
  # ===============================================

  let "outer+=1"    # Increment outer loop counter. 
  echo              # Space between output blocks in pass of outer loop.
done               
# End of outer loop.

exit 0

Other way to print few first and last line of file

[marek.borkowski@wildvmi2.polsatc ~]$ cat line_test | awk 'NR > 3 { exit }; 1'
1 line
2 line
3 line

[marek.borkowski@wildvmi2.polsatc ~]$ cat line_test | awk 'END { print }'
5 line

sobota, 22 października 2016

sed - switch char in file and make backup file


marek@ubuntu:~/dev/bash$ sed -i.bak 's/;/:/' test.dat
marek@ubuntu:~/dev/bash$ ls -ltr
razem 16
-rwxr-xr-x 1 marek marek 108 paź 22 14:48 read.sh
-rw-r--r-- 1 marek marek 380 paź 22 14:50 food_list.txt
-rw-rw-r-- 1 marek marek  55 paź 22 15:01 test.dat.bak
-rw-rw-r-- 1 marek marek  55 paź 22 15:01 test.dat
marek@ubuntu:~/dev/bash$ cat test.dat.bak
marek;borkowski;admin;1983
wiktor;borkowski;user;2015

marek@ubuntu:~/dev/bash$ cat test.dat
marek:borkowski;admin;1983
wiktor:borkowski;user;2015

piątek, 14 października 2016

fuser - lsof substitute - another swiss army knife

[root@dimlogsrvvmd1 logstash]# fuser -n tcp -v 80
                     USER        PID ACCESS COMMAND
80/tcp:              root       1970 F.... httpd
                     apache     6015 F.... httpd
                     apache     6016 F.... httpd
                     apache     6019 F.... httpd
                     apache     6024 F.... httpd
                     apache    25308 F.... httpd
                     apache    61429 F.... httpd
                     apache    61430 F.... httpd
                     apache    61431 F.... httpd
                     apache    61432 F.... httpd
                     apache    61433 F.... httpd

środa, 12 października 2016

Check Top Processes sorted by RAM or CPU Usage in Linux

root@ubuntu:~# ps -eo pid,ppid,cmd,%mem,%cpu --sort=-%mem | head

  PID  PPID CMD                         %MEM %CPU
 1254     1 /usr/lib/jvm/java-8-openjdk  1.7  0.8
 1096     1 /usr/bin/docker daemon -H f  0.4  0.1
 1355  1096 containerd -l /var/run/dock  0.1  0.0
  954     1 /usr/lib/snapd/snapd         0.1  0.0
 1353  1098 sshd: marek [priv]           0.0  0.0
 1645  1098 sshd: marek [priv]           0.0  0.0
  914     1 /usr/lib/accountsservice/ac  0.0  0.0
 1285  1282 /usr/sbin/apache2 -k start   0.0  0.0
 1286  1282 /usr/sbin/apache2 -k start   0.0  0.0

poniedziałek, 10 października 2016

sobota, 8 października 2016

bash command statistic

root@ubuntu:~# fc -l
16       tail -f zorka.log
17       ll
18       ssh marek@ny1.hashbang.sh
19       echo 'obase=17;1234' | bc
20       echo 'obase=8;1234' | bc
21       echo 'obase=2;1234' | bc
22       echo 'obase=10;1234' | bc
23       root@ubuntu:~# echo 'obase=17;1234' | bc
24       root@ubuntu:~# echo 'obase=8;1234' | bc
25       2322
26       root@ubuntu:~# echo 'obase=2;1234' | bc
27       10011010010
28       root@ubuntu:~# echo 'obase=10;1234' | bc
29       1234
30       col() { awk '{print $'$(echo $* | sed -e 's/ /,$/g')'}'; }
31       fcc -l

bash math converter

root@ubuntu:~# echo 'obase=17;1234' | bc
 04 04 10

root@ubuntu:~# echo 'obase=8;1234' | bc
2322

root@ubuntu:~# echo 'obase=2;1234' | bc
10011010010

root@ubuntu:~# echo 'obase=10;1234' | bc
1234

piątek, 7 października 2016

find swaping process

[root@app01.prod.crm.polsatc marek.borkowski]# for i in `ps -ef | grep java |grep -v grep| awk '{print $2}'`;do echo $i && cat /proc/$i/status | grep VmSwap && echo "------"; done
60582
VmSwap:   184464 kB
------
60912
VmSwap:    83324 kB
------
61242
VmSwap:     9840 kB

wtorek, 4 października 2016

get file from server with telnet

 $ (echo 'GET /'; echo; sleep 1; ) | telnet www.google.com 80

Find recent logs that contain the string "Exception"

find . -name '*.log' -mtime -2 -exec grep -Hc Exception {} \; | grep -v :0$

run simple http server - nc swiss knife ;)

while true; do echo "tcp server is running ...." && nc -l -p 8081 < hashbang.sh; done

binary clock :)

root@ubuntu:~# perl -e 'for(;;sleep 1){printf"\r"."%.4b "x6,split"",`date +%H%M%S`}'
0010 0010 0011 1001 0101 0110

nice port scanner

root@ubuntu:~# netcat -z -n -v 127.0.0.1 8000-10000 2>&1 | grep succeeded
Connection to 127.0.0.1 8005 port [tcp/*] succeeded!
Connection to 127.0.0.1 8080 port [tcp/*] succeeded!

share file from terminal

root@ubuntu:~# curl --upload-file hashbang.sh https://transfer.sh/bang.sh

https://transfer.sh/14HMjM/bang.sh

Thread count per user

ps -u jboss -o nlwp= | awk '{ num_threads += $1 } END { print num_threads }'

get the external ip address

root@ubuntu:~# curl -s httpbin.org/ip | jq -r .origin
31.179.165.22

run command in sequence

for i in {1..10}; do time curl http://localhost:8000 >/dev/null; done 2>&1 | grep real
root@ubuntu:~# for i in {1..10}; do time curl http://localhost:8000 >/dev/null; done 2>&1 | grep real
real    0m0.011s
real    0m0.006s
real    0m0.005s
real    0m0.006s
real    0m0.006s
real    0m0.005s
real    0m0.009s
real    0m0.011s
real    0m0.005s
real    0m0.005s

print load average

python -c 'import os;print os.getloadavg()[0]'

10 largest open files

lsof / | awk '{ if($7 > 1048576) print $7/1048576 "MB" " " $9 " " $1 }' | sort -n -u | tail

Display connected host to out server based on port.

clear;while x=0; do clear;date;echo "";echo "  [Count] | [IP ADDR]";echo "-------------------";netstat -np|grep :80|grep -v LISTEN|awk '{print $5}'|cut -d: -f1|uniq -c; sleep 5;done

poniedziałek, 3 października 2016