96 lines
1.8 KiB
Bash
96 lines
1.8 KiB
Bash
#!/bin/bash
|
|
#
|
|
# udp2raw startup script for the Udp2Raw Client
|
|
#
|
|
#
|
|
# chkconfig: 345 80 20
|
|
# description: start the udp2raw deamon
|
|
#
|
|
# Source function library
|
|
. /etc/rc.d/init.d/functions
|
|
|
|
prog=Udp2Raw-Client
|
|
#程序目录
|
|
HOME_DIR=/usr/local/udptools
|
|
#程序文件名
|
|
BIN_NAME=udp2raw_x86
|
|
#配置文件
|
|
CONFIG_FILE=$HOME_DIR/udp2raw-client.conf
|
|
#日志文件
|
|
LOG_FILE=$HOME_DIR/udp2raw-client.log
|
|
|
|
#Function
|
|
checkSet(){
|
|
#获取远程端口和地址
|
|
SERVER_PORT=`cat $CONFIG_FILE | grep '\-r ' | awk -F ":" '{print $2}'`
|
|
SERVER_ADDRESS=`cat $CONFIG_FILE | grep '\-r ' | awk -F ":" '{print $1}' | awk '{print $2}'`
|
|
#检查iptables规则
|
|
IPTALBES=`iptables -nvL | grep DROP | grep tcp | grep $SERVER_PORT | grep $SERVER_ADDRESS`
|
|
if [ ! -n "$IPTALBES" ]; then
|
|
echo "Adding iptables rules."
|
|
#添加iptables规则
|
|
RULES=`$HOME_DIR/$BIN_NAME --conf-file $CONFIG_FILE -g | grep iptables |grep -v rule`
|
|
$RULES
|
|
fi
|
|
#赋权
|
|
setcap cap_net_raw+ep $HOME_DIR/$BIN_NAME
|
|
}
|
|
|
|
start(){
|
|
checkSet
|
|
#启动进程
|
|
sudo -u nobody -b $HOME_DIR/$BIN_NAME --conf-file $CONFIG_FILE >> $LOG_FILE 2>&1
|
|
}
|
|
|
|
stop(){
|
|
#结束进程
|
|
PID=`ps aux|grep $BIN_NAME|grep $CONFIG_FILE|grep -v root|grep -v grep | awk '{print $2}'`
|
|
kill $PID >/dev/null 2>&1
|
|
}
|
|
|
|
status(){
|
|
PID=`ps aux|grep $BIN_NAME|grep $CONFIG_FILE|grep -v root|grep -v grep | awk '{print $2}'`
|
|
if [ ! -n "$PID" ]; then
|
|
echo "$prog is stopped."
|
|
else
|
|
echo "$prog is running. pid $PID"
|
|
fi
|
|
}
|
|
|
|
showLog(){
|
|
cat $LOG_FILE | tail -n 50
|
|
}
|
|
|
|
case "$1" in
|
|
start)
|
|
echo "Starting $prog..."
|
|
start
|
|
;;
|
|
|
|
stop)
|
|
echo "Stopping $prog..."
|
|
stop
|
|
;;
|
|
|
|
restart)
|
|
echo "Stopping $prog..."
|
|
stop
|
|
sleep 2
|
|
echo "Starting $prog..."
|
|
start
|
|
;;
|
|
|
|
status)
|
|
status
|
|
;;
|
|
|
|
log)
|
|
showLog
|
|
;;
|
|
|
|
*)
|
|
echo "Usage: $prog {start|stop|restart|status|log}"
|
|
;;
|
|
esac
|
|
exit 0
|