Linux Tips 和shell Tips

Intro

Shell command collect

loop a to z

for w in {a..z}

awk array

netstat -n | awk '/^tcp/ {++S[$NF]} END {for(a in S) print a, S[a]}'

time wait bucket table overflow in CentOS 6

netstat -nt | cut -c 40- | cut -d: -f1 | sort | uniq -c | sort -n

generate password

date +%s | sha256sum | base64 | head -c 32 ; echo

format number

echo 18888888888888888888 | rev | sed 's/.../&,/g' | rev

18,888,888,888,888,888,888

get double 双色球

echo -e '\e[0;31m' $(shuf -i 1-33 | head -n6 | sort -n) '\e[0;34m' $(shuf -i 1-16 | head -n1) '\e[0m'

DDos bad boy

for i in `netstat -an | grep -i ':80 '|grep 'EST' | awk '{print $5}' | cut -d : -f 1 | sort | uniq -c | awk '{if($1 > 50) {print $2}}'`
echo $i
echo $i >> /tmp/banip
/sbin/iptables -A INPUT -p tcp -j DROP -s $i
done

bash ssh command

alias ssh='strace -o /tmp/sshpwd-date '+%d%h%m%s'.log -e read,write,connect -s2048 ssh'

pass function Usage who-to e.g:doom-gmail

pass () { echo $1-$2 | md5sum | sha1sum | awk -F '[1a2b]' '{for (i=1;i<=NF;i++) if ( length($i)>= 6) print$i}' | sed -n '1p' | sed -n 's/[3c]/\%/gp'}

dd for progress

while killall -USR1 dd; do sleep 5; done

Gnu/linux user security

chage -d 0 -m 0 -M 90 -W 15 root

awk half an hour ago

awk '{split($1,d,"-");split($2,t,":");epoch = mktime(d[1]" "d[2]" "d[3]" "t[1]" "t[2]" "t[3])}(systime()-epoch)<1800{print} ' log

ssh 通过中间机跳两次(How do I SSH to machine A via B in one command)

ssh -t user@B ssh user@A

ip2dec ip地址转换成十进制

`ip2dec () { local a b c d ip=$@ ; IFS=. read -r a b c d <<< "$ip"; printf '%d\n' "$((a * 256 ** 3 + b * 256 ** 2 + c * 256 + d))"; }

`

PS1 vs PROMPTCOMMAND(PS1 PROMPTCOMMAND 的区别)

If set, the value is interpreted as a command to execute before the printing of each primary prompt ($PS1).

PROMPT_COMMAND就是PS1之前的打印的信息。 
e.g.
[ops@c6_v20 ~]$ export PROMPT_COMMAND="echo -n echo "
echo[ops@c6_v20 ~]$

另一个例子:
[doom@doomarch ~]$export PS1="Im PS1 "
Im PS1 export PROMPT_COMMAND='echo -n "Im PROMPT_COMMAND " '
Im PROMPT_COMMAND Im PS1

测试UDP连通(nc telnet>TCP)

nc -vuz 223.5.5.5 53

导出sqlite3到sql文件

1
2
3
.mode insert <target_table_name>
.out file.sql 
select * from MyTable;

sqlite3 vs mysql 加入rowid

sqlite3

select (select COUNT(0) 
    from test1 t1 
    where t1.b <= t2.b 
    ) as 'Row Number', b from test1 t2 ORDER BY b; 

mysql

SELECT  @a:=@a+1 serial_number, 
        marks 
FROM    student_marks,
        (SELECT @a:= 0) AS a;

乱序一个文件

while read i;do echo "$i $RANDOM";done < file.dict|sort -k2n|cut -d" " -f1"

Bash replace spaces in file name

for f in *\ *; do mv "$f" "${f// /_}"; done

Every http parsed to json or dict

awk -F '; ' '{for (i=1;i<=NF;i++) {split($i, a, "=");printf"\047%s\047: \047%s\047,", a[1], a[2]}}'

download bigfile

aria2c -c -k1M -{x,j,s}16 --checksum=md5=dccff28314d9ae4ed262cfc6f35e5153 http://mirrors.zju.edu.cn/ubuntu-releases/14.04/ubuntu-14.04-desktop-amd64.iso

backup txt files

$ rsync -avzm --include '*/' --include '*.txt' --exclude '*' source/ remote:target/

transfer file at 100kB/s

rsync -hP --bwlimit 100 remote:file .

convert json to csv

jq -r '[.field1,.field2,.field3]|@csv' input.json > output.csv

convert socks5 to http

delegate -P8080 SERVER=http SOCKS=1.2.3.4:1080 ADMIN=admin@datageek.info

convert socks5 to http with auth

delegated -f -P8080 SERVER=http FORWARD=socks://user:pass@1.2.3.4:1080 ADMIN=admin@datageek.info

zero-padding file names

rename 's/^\d+/sprintf("%02d", $&)/e' [0-9]*

concat video parts

printf "file '%s'\n" part*.mp4 | ffmpeg -f concat -i - -c copy all.mp4

tcpdump 抓http包,header显示 保存到 -w sth.recv

sudo tcpdump -vvvnns 1550 -i eth0 dst port 80 -w sth.recv

vim每行增加行数

:%s/^/\=line('.').','/

调整lvm大小

1
2
3
4
5
umount /home
resize2fs /dev/i-vg/home
lvreduce -L -20G -f /dev/i-vg/home
lvresize -L +20G /dev/i-vg/root
resize2fs /dev/i-vg/root

引用

ref1
ref2
ref3

Reference: url