2013年6月25日 星期二

Shell Scripts Sample



Generate ssh key 


#!/bin/bash

cd ~

#ssh-keygen -t rsa1
ssh-keygen -t rsa
#ssh-keygen -t dsa

HOST=(1 2 3 4)

for ip in "${HOST[@]}"
do
        echo "puttings files to site $ip, please enter password"

        cat ~/.ssh/id_rsa.pub | ssh migi@192.168.1.$ip "cat >> ~/.ssh/authorized_keys ; chmod 600 ~/.ssh/authorized_keys"
done

Copying Files


#!/bin/bash

cd ~

HOST=(1 2 3 4)

for ip in "${HOST[@]"
do
        scp -r settings 192.168.1.$ip:~/
done


Apache Testing


http://www.cyberciti.biz/tips/howto-performance-benchmarks-a-web-server.html
ab -n 1000 -c 5 http://202.54.200.1/snkpage.html

get unique ip in a specific date from apache log


grep "09/Jan/2014" event-access_log | grep "rid=65" | awk '{print $1}' | sort | uniq | wc -l


check iostat


su root
lvdisplay|awk  '/LV Name/{n=$3} /Block device/{d=$3; sub(".*:","dm-",d); print d,n;}' ; iostat 1

Get specific line from a file

sed -n lineA,lineBp FILE


SED

[root@xxsj-web1 game]# more change_cp.sh
#!/bin/bash
# db config
addr=10.10.68.242:3306
user=game
pwd= pwd
db_main=hotgame_s1
db_logs=logDB_s1
db_stat=statisticData_s1
# get pwd base64
pwdbase64=`echo -n $pwd | base64`

# update cp.cf
find . -name cp.cfg | xargs -n 1 sed -i -e"3c\JDBCConnectionURL=jdbc:mysql://${addr}/${db_main}"
find . -name cp.cfg | xargs -n 1 sed -i -e"11c\User=${user}"
find . -name cp.cfg | xargs -n 1 sed -i -e"12c\Password=${pwdbase64}"
# update logdbconfig.cfg
find . -name logdbconfig.cfg | xargs -n 1 sed -i -e"1c\url:jdbc:mysql://${addr}/${db_logs}?useUnicode=true&characterEncoding=UTF-8"
find . -name logdbconfig.cfg | xargs -n 1 sed -i -e"2c\uname:${user}"
find . -name logdbconfig.cfg | xargs -n 1 sed -i -e"3c\pwd:${pwd}"
# update DBConfig.cfg
find . -name DBConfig.cfg | xargs -n 1 sed -i -e"1c\url:jdbc:mysql://${addr}/${db_stat}?useUnicode=true&characterEncoding=UTF-8"
find . -name DBConfig.cfg | xargs -n 1 sed -i -e"2c\uname:${user}"
find . -name DBConfig.cfg | xargs -n 1 sed -i -e"3c\pwd:${pwd}"
# update log4j.properties
find . -name log4j.properties | xargs -n 1 sed -i -e"1c\  log4j.rootLogger=error, stdout, R"


Get A Fields in Files and sum the number

ls Daily-2014-03* | xargs -n 1 sed -n 6,6p | awk '{n+=$1} END { print n;}'

 

Get Nestat count

netstat -tn | awk '{n[$6]++} END { for(k in n) { print k, n[k]; }}'
watch -tdn1 cat /proc/interrupts
http://www.server-world.info/en/note?os=CentOS_6&p=inotify 
  

Split a String

timeResult = "a b c"
#split the timeReulst with ' ' into timeArray
IFS=' ' read -ra timeArray <<< "$timeResult"
#echo "${timeArray[@]}"


沒有留言:

張貼留言