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 ...
随机推荐
- springmvc后台获取表单提交的数据——@ModelAttribute等方式
1.通过注解ModelAttribute直接映射表单中的参数到POJO.在from中的action写提交的路径,在input的name写参数的名称. package com.demo.model; p ...
- Linux/Ubantu 安装 idea
wget 使用 wget url (这里的url就是你要下载idea的网站) 在idea官网中 找到 direct link 右键复制链接 在 linux 中 打开 终端命令窗口 (Ctrl +Alt ...
- Java 转发和重定向的区别
转发是服务器行为,重定向是客户端行为 1.转发在服务器端完成的;重定向是在客户端完成的 2.转发的速度快;重定向速度慢 3.转发的是同一次请求;重定向是两次不同请求 4.转发不会执行转发后的代码;重定 ...
- RestFul是啥
1. 什么是REST REST全称是Representational State Transfer,中文意思是表述(编者注:通常译为表征)性状态转移. 它首次出现在2000年Roy Fielding的 ...
- 【CUDA 基础】3.5 展开循环
title: [CUDA 基础]3.5 展开循环 categories: - CUDA - Freshman tags: - 展开归约 - 归约 - 模板函数 toc: true date: 2018 ...
- css实现9宫格
html <div class="nine"> <ul> <li>1</li> <li>2</li> < ...
- HDU 5119 Happy Matt Friends ——(背包DP)
题意:有最多40个数字,取任意个数字他们的异或和>=k则是可行的方案,问有多少种可行的方案. 分析:dp[now][j]表示当前这个值的种类数,那么转移方程为dp[now][j] = dp[pr ...
- [题解] [SDOI2010] 古代猪文
题面 题解 题目所求即为 \[ G ^ {\sum_{d | n}C_{n}^{d}} \bmod {999911659} \] 考虑到有这样一个式子 \[ a ^ b \equiv a ^ {b \ ...
- typedef void(*Fun) (void)是什么意思 函数指针(回调函数) 和函数对象总结
https://blog.csdn.net/FreeApe/article/details/49124043 bool (*pf)(const string &,const string &a ...
- Zookeeper执行原理的详细概述
文章作者:Holy Null,来源:http://holynull.leanote.com/post/Zookeeper,非常感谢作者提供如此优秀的原创文章,作者通过俩个月的努力将<Hadoop ...