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

凭着回忆+搜资料,把当初写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\t%d\t",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

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

到这里就结束了

sh脚本实战的更多相关文章

  1. Shell脚本实战:日志关键字监控+自动告警

    一个执着于技术的公众号 该程序使用场景说明:主要用于Linux服务器监控程序日志,如出现关键字异常则触发相应的动作或告警操作,通知到邮件联系人. 一.安装邮件服务 1.解压 tar -jxf mail ...

  2. sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory

    在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...

  3. linux执行sh脚本文件命令

    linux执行sh脚本文件命令 很多时候需要多个命令来完成一项工作,而这个工作又常常是重复的,这个时候我们自然会想到将这些命令写成sh脚本,下次执行下这个脚本一切就都搞定了,下面就是发布代码的一个脚本 ...

  4. .sh脚本判断判断某一变量是否为某一数值

    .sh脚本中,判断某一变量(例如:OEM_CUSTOMER_SUPPORT)是否为某一数值(例如:0),并根据条件做不同处理,写法如下: if [ $OEM_CUSTOMER_SUPPORT -eq  ...

  5. sh脚本学习之: sh脚本 、sed、awk

    sh脚本 sh命令的批处理文件,支持更复杂的逻辑. Shell中的变量 参数 $0 当前脚本路径 $1....$n 脚本执行对应的第n个参数 条件判断 文件判断 test [op] path e存在 ...

  6. 安装GRID时跑root.sh脚本报错(ORA-27091: unable to queue I/O)

    在安装GRID过程中,运行root.sh脚本时报如下信息: Adding Clusterware entries to upstart CRS-2672: Attempting to start 'o ...

  7. sh脚本异常:bad interpreter: No such file or directory

    转:http://bluedest.iteye.com/blog/1674963 在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file o ...

  8. ubuntu sh脚本双击运行

    自从13.04以后,双击sh脚本文件就已经默认是geidt打开了,要想运行,从nautilus-->文件-->首选项-->行为-->可执行文件 有三个选项,默认是第二个,如果想 ...

  9. sh脚本执行Java程序

    1.不引用Jar包或者资源文件夹 最简单的程序Hello World. 首先创建Hello.java public class Hello { public static void main(Stri ...

随机推荐

  1. Ubuntu18.04系统执行语句时出现错误Failed to load module "canberra-gtk-module"

    Ubuntu18.04系统执行gnuradio-companion时,命令行提示错误Failed to load module "canberra-gtk-module",虽然看起 ...

  2. MyBatis---join 查询

    在实际业务中,经常能碰到多表关联查询 下面的Demo,讲举例join查询在MyBatis中的实现 User 类: package com.zy.domain; import java.io.Seria ...

  3. php类点滴---访问修饰符public protected private

    public可以被继承,可以外部访问(也就是实例化对象可以直接访问) protected受保护的,可以被子类继承,无法外部访问 private继承,外部访问都别想 <?phpclass coac ...

  4. Python 判断文件是否存在,不存在则将名称写入指定文件

    import os filename = '15464657761111111.pdf' pathDir = 'F:/tqcs/sr' # 判断文件是否存在 if os.path.exists(pat ...

  5. robotframework FOR循环

    #获取到的ID组装成一个list ${List_ID} Create List ${ID_1} ${ID_2} ${ID_3} ${ID_4} ${ID_5} ... ${ID_6} ${ID_7} ...

  6. pyinstall python文件打包成二进制exe文件

    pycharm + python3 + win7 1 pip install pyinstall  (官网) 2 准备 .py 文件 3 具体例子 from PyQt5.QtWidgets impor ...

  7. 01- ES6、jquery源码、node、webpack

    1.课程介绍 小马哥blog:https://www.cnblogs.com/majj/ 前端学习路径:https://www.processon.com/view/link/5d3a5947e4b0 ...

  8. Chrome安卓H5调试,连接手机检测不到页面

    Chrome安卓H5调试,连接手机检测不到页面,重启什么的都不行,未找到设备,或者offline,怎么办? 首先手机开启调试模式是必须的 然后用adb工具箱,cmd进来 运行命令 adb kill-s ...

  9. [Python之路] ORM(对象关系映射)

    一.概念 ORM是Python后端Web框架Django的核心思想,"Object Relational Mapping",即对象-关系映射,简称ORM. 一句话理解就是: 创建一 ...

  10. Codecombat 游戏攻略——JavaScript编辑语言——关卡(计算机科学四)Ⅱ

    第16关:潜伏 // 用findEnemies把敌人存在数组enemies中 // 只攻击萨满巫师,不要攻击牦牛! var enemies = hero.findEnemies(); var enem ...