zoukankan      html  css  js  c++  java
  • sh脚本实战

    做了什么东西还是要尽快移动到博客上,不然回头看自己写的东西已经看不懂了。。。

    凭着回忆+搜资料,把当初写sh脚本的过程写上来。

    首先新建一个.sh文件,用vim就可以

    在sh的第一行,写上

    #!/bin/sh

    这是告诉系统,这个文件是脚本文件。一定要有

    接下来就可以把它当做一个控制台,你需要在控制台里怎么操作,就可以把命令原样地贴在这里

    例如创建一个文件:

    cd ~/Downloads
    mkdir Geronimo

    也可以在sh中运行其他sh脚本:

    sh pcapname.sh ~/Downloads/Geronimo

    这条命令,就是运行pcapname脚本,并传入路径参数~/Downloads/Geronimo

    在pcapname.sh中,$1就是传入的参数,即~/Downloads/Geronimo,如果传入了更多参数,就是$2,$3以此类推

    而要对$1文件夹下的所有文件进行操作:

    for var in $1/*
    do
        echo $var
    done

    不过这里的$var表示整条路径:~/Downloads/Geronimo/filename

    所以当需要获取文件名的时候,需要提取字符串。

    filepath=${var%.*}
    filename=${filepath##*/}

    这里的filepath是去除了后缀名的字符串,filename是去除了/左边所有字符串的文件名称。

    需要拼接字符串时,如下:

    tcpfile=$filepath/${filename}_TCP.txt

    引用的字符串需要添加$,而为了不与后面的字符串混淆,在filename外添加{}。

    sh中的字符串的截取和拼接可以参考这里

    实战部分:

    我在linux中使用tranalyzer从流量中提取流量特征,生成_flows.txt文件和_pl_iat.txt文件,使用tawk脚本可以从_flows.txt文件中提取所有的tcp特征。

    我的流量在mei1和mei2文件夹中,每个文件夹下有50个子文件夹,包含了50种流量,每个子文件夹下有20个pcap文件

    对每种流量,提取出flows,pl_iat,tcp内容,放到三个文件夹内,每个文件夹生成50个.txt文件,每个.txt文件包含了子文件夹的20个pcap文件的特征。

    我使用了三个.sh文件层层调用,分别为main.sh,pcapname.sh,extractor.sh

    main.sh

    #!/bin/sh
    mkdir /mnt/hgfs/mei/mei1/mei1
    mkdir /mnt/hgfs/mei/mei1/mei1/flowsfiles
    mkdir /mnt/hgfs/mei/mei1/mei1/tcpfiles
    mkdir /mnt/hgfs/mei/mei1/mei1/pl_iatfiles
    
    mkdir /mnt/hgfs/mei/mei2/mei2
    mkdir /mnt/hgfs/mei/mei2/mei2/flowsfiles
    mkdir /mnt/hgfs/mei/mei2/mei2/tcpfiles
    mkdir /mnt/hgfs/mei/mei2/mei2/pl_iatfiles
    
    sh pcapname.sh /mnt/hgfs/mei/mei1
    sh pcapname.sh /mnt/hgfs/mei/mei2

    这里其实也可以写循环实现,但是当时时间紧急,就用笨办法了,反正不多

    pcapname.sh

    #!/bin/sh
    
    #$1 is the directory of the upper level file of the .pcap file
    #var is the name of the directory of the .pcap file 
    # ${var#*ww_} remove the prefix, and input it to the extractor.sh
    for var in $1/*
    do
            sh extractor.sh $var ${var#*ww_} $1/${1##*/}
    done

    这里用来获取每个子文件夹的名称,每个文件夹的名称形式是WWW_52PK_com,我需要从中提取52PK关键字

    .extractor.sh

    #!/bin/sh
    
    #$1 is the directory of the .pcap file(don't including the ***.pcap file)
    #$2 is the name of the directory of the .pcap file(remove the prefix name)
    #var is the name of the .pcap file
    
    
    
    for var in $1/*
    do
            filepath=${var%.*}
            filename=${filepath##*/}
    
            #cd ~/Downloads/tranalyzer2-0.8.2lm2/tranalyzer2-0.8.2/trunk/tranalyzer2/src/
            #./tranalyzer -r $var -w $filepath/
    
            #cd ~/Downloads/tranalyzer2-0.8.2lm2/tranalyzer2-0.8.2/trunk/scripts/tawk/
            flowsfile=$filepath/${filename}_flows.txt
            tcpfile=$filepath/${filename}_TCP.txt
            pl_iatfile=$filepath/${filename}_pl_iat.txt
            #./tawk 'tcp()' $flowsfile > $tcpfile
    
            #./tawk -t -H '{
            #        n = split($L2L3L4Pl_Iat,A,";");
            #       for(i=1;i<=n;i++){
            #               split(A[i],B,"_");
            #               printf "%f	%d	",B[2],B[1];
            #       }
    
            cp $flowsfile $3/flowsfiles/$2_${flowsfile##*/}
            cp $tcpfile $3/tcpfiles/$2_${tcpfile##*/}
            cp $pl_iatfile $3/pl_iatfiles/$2_${pl_iatfile##*/}
    
    done

    这里用#的地方是我的功能性代码

    到这里就结束了

  • 相关阅读:
    收集一些.NET开发资源站点和部分优秀.NET开源项目
    对DataTable数据进行查询过滤
    转:CommandArgument 传多个值到另外页面的方法
    转:SqlServer中的datetime类型的空值和c#中的DateTime的空值的研究
    (转)SQL语句Select Case和If else
    .net 使用NPOI或MyXls把DataTable导出到Excel
    pycharm+PyQt5+python最新开发环境配置,踩坑过程详解
    sql server2008系统表详细说明sys.开头的表
    SqlServer中Sql查看存储过程
    SQL SERVER导入EXCEL文件:未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序
  • 原文地址:https://www.cnblogs.com/masonmei/p/11585498.html
Copyright © 2011-2022 走看看