顯示具有 Shell Script 標籤的文章。 顯示所有文章
顯示具有 Shell Script 標籤的文章。 顯示所有文章

2017年5月18日 星期四

iptables rule

~Staging ~

special iptables rule :


#!/bin/sh

/sbin/iptables -F
#newse
/sbin/iptables -A INPUT -p TCP -s IP --dport 22 -j ACCEPT #stage-se
/sbin/iptables -A INPUT -p TCP -s IP --dport 22 -j ACCEPT #stage-se
/sbin/iptables -A INPUT -p TCP -s IP --dport 22 -j ACCEPT #stage-se
/sbin/iptables -A INPUT -p TCP -s IP --dport 22 -j ACCEPT #stage-se
/sbin/iptables -A INPUT -p TCP -sIP --dport 22 -j ACCEPT #stage-vac02
/sbin/iptables -A INPUT -p TCP -s IP --dport 22 -j ACCEPT #stage-vac02
/sbin/iptables -I INPUT -p TCP -s any/0 --dport 25 -j ACCEPT #local mail services
/sbin/iptables -I OUTPUT -p TCP -s any/0 -d 127.0.0.1 --dport 25 -j ACCEPT #jblog mail
/sbin/iptables -I OUTPUT -p TCP -d IP --dport 25 -j ACCEPT #jblog mail
/sbin/iptables -I OUTPUT -p TCP -d IP--dport 25 -j ACCEPT #104 mail


/sbin/iptables -A INPUT -p TCP --dport 22 -j LOG --log-prefix "*Drop ssh*"
/sbin/iptables -A INPUT -p TCP --dport 22 -j DROP
#/sbin/iptables -A OUTPUT -p TCP --dport 25 -j LOG --log-prefix "*Drop smtp*"
/sbin/iptables -A OUTPUT -p TCP --dport 25 -j DROP

2016年12月27日 星期二

隨手筆記 指令

隨手筆記 指令

###change AP serevr output log #### 
:%s/Patch_program\/\(\S\+\)\/\S\+.log/Patch_log\/\1\/console.log/g  



####change AP crontab to each file #### 
grep '/opt/scripts/batch/ap_cron.sh' /etc/crontab | for i in `awk '{print $8}'` ; do grep " $i "  /etc/crontab > AP_$i ; done 


##delete postque yahoo domain 
postqueue -p | tail -n +2 | awk 'BEGIN { RS = "" } /@yahoo\.com/ { print $1 }' | tr -d '*!' | postsuper -d - 

2014年3月4日 星期二

for do done (openstack scrit)

type=web
for i in 17

do
   echo "nova-manage service disable $type-cp$i nova-compute"
   nova-manage service disable $type-cp$i nova-compute
   echo "nova-manage service disable $type-cp$i nova-network"
   nova-manage service disable $type-cp$i nova-network
done

nova-manage service list


2013年9月12日 星期四

Sed 運用

在每一行的行尾增加字符
sed -i 's/\($\)/aaa/g' /home/test.txt

#每行首增加 #
sed -i 's/\(^\)/#/g' /home/test.txt

 #替換第一.字符成#
sed -i 's/\(^.\)/#/g' /home/test.txt

搜尋 time 區間
sed -n '/2012:02:20:00/,/2012:02:50:00/p' /opt/httpd/logs/io_access_log20121019 | awk '{if ( $(NF-1) > '1000000' )print $0}'



2012年8月6日 星期一

shell script function 範例



Starttomcat(){
               
               /opt/tomcat/bin/start.sh

}



CHECK_START() {
                sleep 5
                STATUS=`tail -n 10 $INSTENCE_LOG | grep -c "Server startup in"`
                if [ "$STATUS" -eq "1" ];then
                        echo tomcat restart OK
                        CHECK_IPTABLE
                   else
                        Killtomcat
                        sleep 1
                        Starttomcat
                        CHECK_START
                fi
}


case $1 in
start)
        Starttomcat
        CHECK_START
#       StartApache
        ;;
stop)
        Killtomcat
        ;;
restart)
        Killtomcat
        sleep 1
        Starttomcat
        CHECK_START
        ;;
*)
        echo $"Usage: {start|stop|restart}"
        exit 1
        ;;
esac

2012年7月18日 星期三

分析 web access log 計算 IP 次數

如題: 只是為了計算來源IP的次數與統計

awk '{name[$1]++ }; END {for (count in name) print count,name[count]}' |sort -k 2 -nr $LOGFILE




Shell Script 搜尋出檔案,產生出清單後,對照另個清單動作

搜尋出檔案,產生出清單後,一比一比,對照另個清單動作(mv)

=======================分格線==========================


#!/bin/bash

backerno=( $@ ) ;
path_source="/data/mail/mailpool/" ;
dest="/data/mail/dest" ;
yahoodest="/data/mail/yahoodest"
dest_no=`cat $dest |wc -l` ;
yahoodest_no=`cat $yahoodest|wc -l`
yahoolist="/opt/logs/yahoo`date +%Y%m%d_%H_%M`_${backerno[0]}_$RANDOM"
filelist="/dev/shm/`date +%Y%m%d_%H_%M`_${backerno[0]}_$RANDOM"

create_list ()
        {
                if [ ${#backerno[*]}  -eq 0 ];then
                        echo "Userage: cmd <backer> <backer>"
                else
                        si=0;
                        while [ $si -lt ${#backerno[*]}  ]
                        do
                        echo ${backerno[$si]} create list done ;
                        #find $path_source${backerno[$si]} -name '*.?mail' |head -2000 |xargs grep -i "^To.*@yahoo.*" | awk -F ':' '{print $1}' >> $yahoolist     --> 此行是當初要抓出,要寄到yahoo信件所做的分?
                        find $path_source${backerno[$si]} -name '*.?mail' |head -800 | awk -F ':' '{print $1}' >> $filelist
                        #find $path_source${backerno[$si]} -name '*.?mail' -print >> $filelist
                                si=`expr $si + 1`;
                        done
                fi
        }

random_send ()
        {
                destogo=`awk 'NR=='$i'' $dest`
                mv -f $@ $destogo  
                i=`expr $i + 1 `        }

yahoo_send ()
        {
                destogo=`awk 'NR=='$i'' $yahoodest`
                mv -f $@ $destogo
                #echo  $@    ==== $destogo
                #echo "$@ move to $destogo"
                i=`expr $i + 1 `        }

random_main ()
        {
                i=1
                for file in `cat $filelist`
                        do
                                if [ $i -gt $dest_no ];then
                                i=1
                                random_send $file
                        else
                                random_send $file
                        fi
                done        }

yahoo_main ()
        {
                i=1
                for file in `cat $yahoolist`
                        do
                                if [ $i -gt $yahoodest_no ];then
                                i=1
                                yahoo_send $file
                        else
                                yahoo_send $file
                        fi
                done        }

x=1;
while [ $x -ge 1 ]
        do
                starttime=`date +'%Y%m%d %T'`
                perstart=`date +%s`
                create_list
                echo $filelist
                random_main
                perstop=`date +%s`
                tvalue=`expr $perstop - $perstart`
                totalfiles=`cat $filelist |wc -l`
                pervalue=`expr $totalfiles / $tvalue `
                echo  "Start at $starttime  done at `date +'%Y%m%d %T'` moved $totalfiles files in $pervalue per/sec" >> /data/mail/log/${backerno[0]}${backerno[1]}.log;
                rm -rf $filelist ;
                sleep 60;
done

Get ip username login root shell script

vi  /root/.bashrc



# which mail addres to send?
email='xxx@xxx.com.tw,root_check@xxx.com.tw'

#get user information
ipaddr=`who am i | awk '{print $6}' | cut -d '(' -f2 | cut -d ')' -f1`
tty=`who am i | awk '{print $2}'`
username=`ps aux | grep "$tty" | grep sshd | awk '{print $12}' | head -n1 | cut -d '@' -f1`

#check macaddr
ping "$ipaddr" -c1 &> /dev/null
macaddr=`arp -a $ipaddr | awk '{print $4}'`

#input from whitch interface
interface=`arp -a $ipaddr | awk '{print $7}'`

#check if can't receive macadr
if [ "$macaddr" == 'entries' ]; then
        macaddr="can't find macaddr in arp"
fi

if [ "$interface" == 'found.' ]; then
        interface="can't find interface in arp"
fi

#send mail now
echo -e "
  Somebody changing identity to [ root ] on `hostname`!! \n
  username:       $username \r
  ipaddr:         $ipaddr \r
  macaddr:        $macaddr \r
  interface:      $interface \r
  console:        $tty \r
  date:           `date` \n" | mail -s "Somebody changing identity to SUPERUSER on `hostname`!!" $email
shopt -s histappend
PROMPT_COMMAND="history -a; $PROMPT_COMMAND"