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\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脚本实战的更多相关文章
- Shell脚本实战:日志关键字监控+自动告警
一个执着于技术的公众号 该程序使用场景说明:主要用于Linux服务器监控程序日志,如出现关键字异常则触发相应的动作或告警操作,通知到邮件联系人. 一.安装邮件服务 1.解压 tar -jxf mail ...
- sh脚本异常:/bin/sh^M:bad interpreter: No such file or directory
在Linux中执行.sh脚本,异常/bin/sh^M: bad interpreter: No such file or directory. 分析:这是不同系统编码格式引起的:在windows系统中 ...
- linux执行sh脚本文件命令
linux执行sh脚本文件命令 很多时候需要多个命令来完成一项工作,而这个工作又常常是重复的,这个时候我们自然会想到将这些命令写成sh脚本,下次执行下这个脚本一切就都搞定了,下面就是发布代码的一个脚本 ...
- .sh脚本判断判断某一变量是否为某一数值
.sh脚本中,判断某一变量(例如:OEM_CUSTOMER_SUPPORT)是否为某一数值(例如:0),并根据条件做不同处理,写法如下: if [ $OEM_CUSTOMER_SUPPORT -eq ...
- sh脚本学习之: sh脚本 、sed、awk
sh脚本 sh命令的批处理文件,支持更复杂的逻辑. Shell中的变量 参数 $0 当前脚本路径 $1....$n 脚本执行对应的第n个参数 条件判断 文件判断 test [op] path e存在 ...
- 安装GRID时跑root.sh脚本报错(ORA-27091: unable to queue I/O)
在安装GRID过程中,运行root.sh脚本时报如下信息: Adding Clusterware entries to upstart CRS-2672: Attempting to start 'o ...
- 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 ...
- ubuntu sh脚本双击运行
自从13.04以后,双击sh脚本文件就已经默认是geidt打开了,要想运行,从nautilus-->文件-->首选项-->行为-->可执行文件 有三个选项,默认是第二个,如果想 ...
- sh脚本执行Java程序
1.不引用Jar包或者资源文件夹 最简单的程序Hello World. 首先创建Hello.java public class Hello { public static void main(Stri ...
随机推荐
- Ubuntu18.04安装破解版MATLAB2018b
参考链接: Ubuntu 18.04安装破解Matlab 2018b及创建快捷方式的详细教程(附图) Linux下安装JDK遇到的问题之cp: 无法获取"jdk-8u191-linux-i5 ...
- UVa10615 Andy's First Dictionary(集合set)
这道题主要用到了set容器和stringstream,用起来非常方便,我第一次见识到,觉得十分的炫酷…… 而且,竟然可以将自己写的单词按照字典序排列,真的太酷了. 下面是书上的代码,目前还处于初学状态 ...
- 不使用C库函数(Sprintf)将void* 指针转换为十六进制字符串
#include <stdio.h> #include <stdint.h> #include <stdlib.h> void hexDump(void *ptr, ...
- pytorch tensor 维度理解.md
torch.randn torch.randn(*sizes, out=None) → Tensor(张量) 返回一个张量,包含了从标准正态分布(均值为0,方差为 1)中抽取一组随机数,形状由可变参数 ...
- 在jquery中,使用ajax上传文件和文本
function onSubmit (data) { //获取文本 var callingContent = $('#callingContent').val() // 获取文件 var files ...
- vue cli3 + cube-ui 配置rem踩坑记录
在install cube-ui时,选择了后编译,选中使用rem的时候会发现,怎么刷新页面的html字体被设置成了37.5px 感觉太大了,于是去寻找修改的办法,第一反应是webpack的配置,于是去 ...
- EntityManager的merge()方法
EntityManager的merge()方法相当于hibernate中session的saveOrUpdate()方法: 用于实体的插入和更新操作:
- vue中全局组件与局部组件的注册,以及动态绑定props值
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 美团小程序框架mpvue入门教程
mpvue是一个使用 Vue.js 开发小程序的前端框架.框架基于 Vue.js 核心,mpvue 修改了 Vue.js 的 runtime 和 compiler 实现,使其可以运行在小程序环境中,从 ...
- shell 之 用linux定时任务crontab和watchdog.sh脚本做软件看门狗
1.简介 看门狗的作用是定期检测服务正常运行,如果发现服务不在了,会重新拉起服务:linux中可以利用系统的定时任务功能crontab定期的去执行watchdog.sh脚本,而watchdog.sh脚 ...