nc结合iptables

本文涉及的产品
函数计算FC,每月15万CU 3个月
公网NAT网关,每月750个小时 15CU
简介:
#!/bin/bash
#############################################################
#
#  全局定义部分
#
#############################################################
#到网关的主目录
cd /ssl/bin
#定义一个null的变量
NULL=/dev/null
#使网关开启路由转发功能
echo 1 > /proc/sys/net/ipv4/ip_forward
#卸载cp的别名,程序拷贝文件时,不需要交互。
alias cp='cp -i'
unalias cp
#创建nc脚本使用的目录
  if [ ! -d /ssl/bin/.nc ] ; then
    mkdir .nc
  fi
#创建文件,用于比较是否相同
touch .nc/ipaddandport3
touch .nc/ipaddandport4
touch .nc/dnsipaddport2
touch .nc/awk.txt2
#判断程序是否能开机启动
START=`grep /ssl/bin/nc.sh /etc/rc.local >$NULL ; echo $? `
  
  if [ $START -eq 1 ] ; then
echo "nohup /ssl/bin/nc.sh &" >>/etc/rc.local
  fi
#创建数据库脚本文件
  if [ ! -e /ssl/bin/.nc/derby.sql ] ; then
    #创建数据查询语句
    echo "connect 'jdbc:derby:/ssl/bin/.nc/Server.Box.Derby2';" > .nc/derby.sql
    echo "select * from NCMAPTABLE ;" >> .nc/derby.sql
  fi
#更新数据库文件,可以让开机再加载一次NC的配置
DIFFFILE=`grep "touch /ssl/bin/.nc/Server.Box.Derby2/diff" /etc/rc.local >$NULL; echo $?`
  if [ $DIFFFILE -eq 1 ] ; then
    echo " touch /ssl/bin/.nc/Server.Box.Derby2/diff " >> /etc/rc.local
  fi
#############################################################
#
#  脚本循环执行部分
#
#############################################################
while [ 1 ] 
do
#比较俩个目录是否相同,用来确定是否需要重新复制数据库
DIRFC=`diff -r Server.Box.Derby .nc/Server.Box.Derby2/ &>$NULL ; echo $?`
  if [ ! $DIRFC -eq 0 ] ; then
    #复制数据库文件
    rm -rf .nc/Server.Box.Derby2
    cp /ssl/bin/Server.Box.Derby/ /ssl/bin/.nc/Server.Box.Derby2 -r
    logger  "cp /ssl/bin/Server.Box.Derby/ /ssl/bin/.nc/Server.Box.Derby2 -r"
  fi
#运行数据库程序,提取NC数据库文件
java -jar /ssl/tomcat/webapps/SSL/WEB-INF/lib/derbyrun.jar ij /ssl/bin/.nc/derby.sql > .nc/ncmap
#判断NC服务是否启动,定义一个值。
NCSTAT=`ifconfig | grep t[trap.tun0] >$NULL ; echo $?`
#获取NC服务的原地址段,就是客户机启动虚拟地址段。
SNATSADD=`cat NcServ.config | grep Client |awk -F'<Client>' '{print $2}' |awk -F'</Client>' '{print $1}' `
#提取nc服务的配置信息,提取目的服务器ip地址,端口信息。
cat .nc/ncmap |egrep -v '^-|rows|ij|MAPID' |column -t |awk '{print $2"  "$3}'  |awk -F'|' '{print $2 $3 $5}' |egrep ^[a-z] > .nc/dnsipaddport
cat .nc/ncmap |egrep -v 'ij|'^-'|rows|MAPID' |column -t |awk '{print $2"  "$3}'|awk -F'|' '{print $2 $3 $5}' |egrep -v ^[a-z] | grep -v - > .nc/ipaddandport
cat .nc/ncmap |egrep -v 'ij|'^-'|rows|MAPID' |column -t |awk '{print $2"  "$3}'|awk -F'|' '{print $2 $3 $5}' |egrep -v  ^[a-z]|grep - | sed s/-/:/g >.nc/ipaddandport2    
cat .nc/ncmap |egrep -v '^-|rows|ij|MAPID' |column -t |grep ';TCP' |awk -F'|' '{print $2" "$3}' |awk -F';' '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" "$18 }'  | awk -F: '{print $1" "$3" "$5" "$7" "$9" "$11" "$13" "$15" "$17" "$19" "$21""$23" "$25" "$27" "$29" "$31" "$33" "$35" "$37 }' |sed s/-/:/g > .nc/awk.txt
#比较俩个文件是否相同,生成值。
diff .nc/ipaddandport .nc/ipaddandport3 >$NULL; echo $?  > .nc/FC
diff .nc/ipaddandport2 .nc/ipaddandport4 >$NULL; echo $? >>.nc/FC
diff .nc/dnsipaddport .nc/dnsipaddport2 >$NULL; echo $? >>.nc/FC
diff .nc/awk.txt .nc/awk.txt2 >$NULL; echo $? >>.nc/FC
 
#判断ipaddandport文件是否更改过,定义一个值
FCSTAT=`grep 1 .nc/FC >$NULL ; echo $?`
  if [ $NCSTAT -eq 0 ] ;then
    #判断ipaddandport文件是否更改过,更改过就执行下面的配置。
    if [ $FCSTAT -eq 0 ] ; then                     
      #复制一个nc服务的配置文件,作为旧的与新的做比较
      cp .nc/ipaddandport .nc/ipaddandport3
      cp .nc/ipaddandport2 .nc/ipaddandport4
      cp .nc/dnsipaddport .nc/dnsipaddport2
      cp .nc/awk.txt .nc/awk.txt2
      #清空iptables脚本文件
      > .nc/iptables.sh
      
      #清空用DNS解析得到的ip地址文件
      >.nc/dnsip
      
      #清空存放网关地址的文件
      >.nc/gatewayadd
      #清空存放目的服务地址的文件
      >.nc/targetadd
      #定义每个文件的行数。
      IPADDLINE=`wc -l .nc/ipaddandport |awk '{print $1}'`
      IPADDLINE2=`wc -l .nc/ipaddandport2 |awk '{print $1}'`
      IPADDLINE3=`wc -l .nc/dnsipaddport |awk '{print $1}'`
      AWKFILELINE=`wc -l .nc/awk.txt |awk '{print $1}'`
      
      ##用于生成iptables.sh脚本文件
      #定义循环的次数,根据配置文件ip地址的行数判断
      for ((i=1;i<=$IPADDLINE;i++)) 
      do
        #提取目标服务器的ip地址
        for i1 in `cat .nc/ipaddandport |awk '{print $1}' | head -n $i | tail -n 1 `
        do
          #提取目标地址到文件 
          echo $i1 >> .nc/targetadd
   
          #提取到达目标服务器的网关地址
          for i2 in `tracepath -n $i1 | head -n 1 |awk '{print $2}'`
          do
           logger tracepath -n $i1 route address $i2  
           #提取网关地址到文件
           echo $i2 >> .nc/gatewayadd
           
           #生成iptables脚本文件
           cat .nc/ipaddandport |awk -F: '{print $1" "$2"   "$3}' | awk -vSNAT="$SNATSADD" '{print "iptables -t nat -I POSTROUTING -s"" "SNAT" ""-d"" "$1" ""-p"" "$2" ""--dport"" "$4" ""-j SNAT --to"}' |  awk -vSNATTOADD="$i2" '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" " SNATTOADD}' |head -n $i |tail -n 1 >> .nc/iptables.sh 
          done
        done
      done 
      
      #用于生成iptables.sh脚本文件
      for ((i=1;i<=$IPADDLINE2;i++))
      do
        TIME=`date +%s`
        echo $TIME > .nc/time
        sh -x .nc/while &>$NULL &
        #提取目标服务器的ip地址
        for i2 in `cat .nc/ipaddandport2 |awk '{print $1}' | head -n $i | tail -n 1 `
        do
          #提取目标地址到文件 
          echo $i2 >> .nc/targetadd
          
          #提取到达目标服务器的网关地址
          for i3 in `tracepath -n $i2 | head -n 1 |awk '{print $2}'`
          do 
            logger tracepath $i2 route address $i3
            #提取网关地址到文件
            echo $i3 >> .nc/gatewayadd
            #生成iptables脚本文件
            cat .nc/ipaddandport2 |cut -d : -f 1,3-4 | awk -F: '{print $1"  "$2" "$3$4}' | awk -vSNAT="$SNATSADD" '{print "iptables -t nat -I POSTROUTING -s"" "SNAT" ""-d"" "$1" ""-p"" "$2" ""--dport"" "$3":"$4" ""-j SNAT --to"}' | grep -v ': -j' |grep -v ';' |  awk -vSNATTOADD="$i3" '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" " SNATTOADD}' |head -n $i |tail -n 1 >>.nc/iptables.sh
          done
        done
      done
   
      ##用于生成iptables.sh脚本文件 
      #定义循环的次数,根据配置文件ip地址的行数判断   
      for ((i=1;i<=$IPADDLINE3;i++))
      do
        #提取目标服务的域名地址
        for i2 in `cat .nc/dnsipaddport |awk '{print $1}' | head -n $i |tail -n 1 `
        do
          #将域解析成iP地址
          nslookup $i2 |tail -n 2 |awk '{print $2}' |head -n 1 |head -n $i |tail -n 1 >> .nc/dnsip
          #提取解析完成的iP地址
          for i5 in `cat .nc/dnsip | head -n $i | tail -n 1 `
          do
            #提取目标地址到文件 
            echo $i5 >> .nc/targetadd
           
            #提取到达目标服务器的网关地址
            for i3 in `tracepath -n $i5 | head -n 1 |awk '{print $2}'`  
            do
               logger tracepath $i5 route address $i3
              #提取网关地址到文件
              echo $i3 >> .nc/gatewayadd
              
              #生成iptables的脚本文件
              cat .nc/dnsipaddport | cut -d : -f 1,3-4 | awk -vIPADD="$i5" -F: '{print IPADD" "$1"  "$2 }'|awk '{print $1" "$3" "$4}'   | awk -vSNAT="$SNATSADD" '{print "iptables -t nat -I POSTROUTING -s"" "SNAT" ""-d"" " $1 " ""-p"" "$2" ""--dport"" "$3" "$4" ""-j SNAT --to"}' |  awk -vSNATTOADD="$i3" '{print $1 " "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" " SNATTOADD}' |head -n $i |tail -n 1 >> .nc/iptables.sh 
                       
            done
          done
        done
      done
 
      for ((i=0;i<=$AWKFILELINE;i++))
      do
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$2" "$3}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$4" "$5}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$6" "$7}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$8" "$9}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$10" "$11}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$12" "$13}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$14" "$15}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$16" "$17}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$18" "$19}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$20" "$21}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$22" "$23}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$24" "$25}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$26" "$27}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$28" "$29}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$30" "$31}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$32" "$33}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$34" "$35}' >>.nc/awkaddre.txt
        cat .nc/awk.txt |head -n $i |tail -n 1 | awk '{print $1" "$36" "$37}' >>.nc/awkaddre.txt
        cat .nc/awkaddre.txt  |sort -u | grep TCP  > .nc/awkadd ; cat .nc/awkadd >> .nc/awkaddress.txt
        cat .nc/awkaddre.txt  |sort -u | grep UDP  > .nc/awkadd ; cat .nc/awkadd >> .nc/awkaddress.txt
       done 
       
       #生成包含一次配置很多条目的iptables脚本
       AWKFILELINE=`wc -l .nc/awkaddress.txt |awk '{print $1}'`
       for ((i=1;i<=$AWKFILELINE;i++))
       do
         for i2 in `cat .nc/awkaddress.txt |awk '{print $1}' | head -n $i | tail -n 1 ` 
         do
           #提取目标地址到文件 
           echo $i2 >> .nc/targetadd
           #提取到达目标服务器的网关地址
           for i3 in `tracepath -n $i2 | head -n 1 |awk '{print $2}'`
           do 
             logger tracepath $i2 route address $i3
             #提取网关地址到文件
             echo $i3 >> .nc/gatewayadd
             
             #生成iptables脚本文件
             filestatus=`grep ':' .nc/awkaddress.txt > $NULL ; echo $?`
             #当filestatus等于0时,说明包含多个有同个ip地址有多个端口的条目
               if [ $filestatus -eq 0 ] ; then
                
               cat .nc/awkaddress.txt | grep -v ':' > .nc/awkadd ; cat .nc/awkadd > .nc/awkaddress2.txt  
               cat .nc/awkaddress2.txt | awk -vSNAT="$SNATSADD" '{print "iptables -t nat -I POSTROUTING -s"" "SNAT" ""-d"" "$1" ""-p"" "$2" ""--dport"" "$3" ""-j SNAT --to"}' | awk -vSNATTOADD="$i3" '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" " SNATTOADD}' |head -n $i |tail -n 1 >>.nc/iptables.sh
  
               cat .nc/awkaddress.txt | grep ':' > .nc/awkadd ; cat .nc/awkadd > .nc/awkaddress3.txt
  
               cat .nc/awkaddress3.txt | awk -vSNAT="$SNATSADD" '{print "iptables -t nat -I POSTROUTING -s"" "SNAT" ""-d"" "$1" ""-p"" "$2" ""--dport"" "$3":"$4}' | cut -d : -f 1,2  |  awk -vSNATTOADD="$i3" '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17 "-j SNAT --to"" " SNATTOADD}' |  head -n $i |tail -n 1 >>.nc/iptables.sh              
   
             else
               cat .nc/awkaddress.txt | awk -vSNAT="$SNATSADD" '{print "iptables -t nat -I POSTROUTING -s"" "SNAT" ""-d"" "$1" ""-p"" "$2" ""--dport"" "$3" ""-j SNAT --to"}' | awk -vSNATTOADD="$i3" '{print $1" "$2" "$3" "$4" "$5" "$6" "$7" "$8" "$9" "$10" "$11" "$12" "$13" "$14" "$15" "$16" "$17" " SNATTOADD}' |head -n $i |tail -n 1 >>.nc/iptables.sh
                
             fi
           done
         done
       done
     
      #配置icmp的iptables,允许ping。
      ICMPFILELINE=`wc -l .nc/gatewayadd |awk '{print $1}'`
      for ((i=1;i<=$ICMPFILELINE;i++))
      do
        for i2 in `cat .nc/gatewayadd |head -n $i |tail -n 1 `
        do
          for i3 in `cat .nc/targetadd |head -n $i |tail -n 1 `
          do 
            echo "iptables -t nat -I POSTROUTING -s $SNATSADD -p icmp -d $i3 -j SNAT --to $i2" >> .nc/iptables.sh
          done
        done
      done
              
      #清空之前配置的iptables,并执行iptables.sh脚本
      cat .nc/iptables.sh |sort -u > .nc/awkipadd ; cat .nc/awkipadd > .nc/iptables.sh
      iptables -t nat -F
      iptables -t nat -X
      chmod a+x .nc/iptables.sh
      ./.nc/iptables.sh
    fi
  fi
       
sleep 5
done
 

本文转自zhaoyun00 51CTO博客,原文链接:http://blog.51cto.com/zhaoyun/717052
相关文章
|
消息中间件 监控 Java
一次线上Flink 背压情况分析之重新认识java dump 文件
一次线上Flink 背压情况分析之重新认识java dump 文件
386 0
|
3天前
|
弹性计算 运维 搜索推荐
三翼鸟携手阿里云ECS g9i:智慧家庭场景的效能革命与未来生活新范式
三翼鸟是海尔智家旗下全球首个智慧家庭场景品牌,致力于提供覆盖衣、食、住、娱的一站式全场景解决方案。截至2025年,服务近1亿家庭,连接设备超5000万台。面对高并发、低延迟与稳定性挑战,全面升级为阿里云ECS g9i实例,实现连接能力提升40%、故障率下降90%、响应速度提升至120ms以内,成本降低20%,推动智慧家庭体验全面跃迁。
|
4天前
|
数据采集 人工智能 自然语言处理
3分钟采集134篇AI文章!深度解析如何通过云无影AgentBay实现25倍并发 + LlamaIndex智能推荐
结合阿里云无影 AgentBay 云端并发采集与 LlamaIndex 智能分析,3分钟高效抓取134篇 AI Agent 文章,实现 AI 推荐、智能问答与知识沉淀,打造从数据获取到价值提炼的完整闭环。
372 91
|
5天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
SQL 人工智能 自然语言处理
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
随着生成式AI的普及,Geo优化(Generative Engine Optimization)已成为企业获客的新战场。然而,缺乏标准化流程(Geo优化sop)导致优化效果参差不齐。本文将深入探讨Geo专家于磊老师提出的“人性化Geo”优化体系,并展示Geo优化sop标准化如何帮助企业实现获客效率提升46%的惊人效果,为企业在AI时代构建稳定的流量护城河。
385 156
Geo优化SOP标准化:于磊老师的“人性化Geo”体系如何助力企业获客提效46%
|
4天前
|
数据采集 缓存 数据可视化
Android 无侵入式数据采集:从手动埋点到字节码插桩的演进之路
本文深入探讨Android无侵入式埋点技术,通过AOP与字节码插桩(如ASM)实现数据采集自动化,彻底解耦业务代码与埋点逻辑。涵盖页面浏览、点击事件自动追踪及注解驱动的半自动化方案,提升数据质量与研发效率,助力团队迈向高效、稳定的智能化埋点体系。(238字)
267 156