linux shell实现批量关闭局域网中主机端口
假设局域网中有多台主机,只能开通ssh服务(端口22),如果发现其他服务打开,则全部关闭。通过运行一个shell脚本,完成以上功能。在实际运维中,可以通过puppet等工具更快更好的完成这个功能,所以本案例仅仅用来练手,为了熟悉sed, awk, grep等常见的shell命令而已。
1、通过nmap命令查询局域网中所有主机打开的端口,并存入文件nmap1.txt中。
# 通过nmap命令查询局域网中所有主机打开的端口,并存入文件nmap1.txt中
mkdir -p /wuhao/sh/files
nmap $ > /wuhao/sh/files/nmap1.txt
以nmap 192.168.20.1-10为例,输出结果为:
Starting Nmap 5.51 ( http://nmap.org ) at 2016-03-03 16:37 CST
Nmap scan report for oos01 (192.168.20.1)
Host is up (.0000040s latency).
Not shown: closed ports
PORT STATE SERVICE
/tcp open ftp
/tcp open ssh
/tcp filtered http Nmap scan report for oos02 (192.168.20.2)
Host is up (.000099s latency).
Not shown: closed ports
PORT STATE SERVICE
/tcp open ssh
/tcp open http
/tcp open mysql
MAC Address: :1C::FF:5A:B5 (Parallels) Nmap scan report for oos03 (192.168.20.3)
Host is up (.000097s latency).
Not shown: closed ports
PORT STATE SERVICE
/tcp open ssh
/tcp open http
/tcp open mysql
MAC Address: :1C::::3C (Parallels) Nmap done: IP addresses ( hosts up) scanned in 1.57 seconds
2、从文件nmap1.txt中提取出需要的信息(主机ip,以及端口状态)。
# 从文件nmap1.txt中提取出需要的信息(主机ip,以及端口状态)
sed -n '/\(Nmap scan report for\|^[0-9]\+\/\)/p' /wuhao/sh/files/nmap1.txt > /wuhao/sh/files/nmap2.txt
hosts=($(grep -on '(.*)' /wuhao/sh/files/nmap2.txt | sed -n 's/(\|)//gp'))
declare -i len=${#hosts[*]}
declare -i i=
while [[ $i -lt $len ]]
do
lines[$i]=$(echo ${hosts[$i]} | awk -F ':' '{print $1}')
ips[$i]=$(echo ${hosts[$i]} | awk -F ':' '{print $2}')
i=$i+
done
# echo ${lines[*]}=
# echo ${ips[*]}=192.168.20.1 192.168.20.2 192.168.20.3
3、在端口状态行首添加所对应的主机ip信息,并将结果保存到文件nmap2.txt中。
# 在端口状态行首添加所对应的主机ip信息
declare -i j=
while [[ $j -lt $len ]]
do
declare -i k=$j+
if [ $j -ne $(($len-)) ]; then
sed -i "$((${lines[$j]}+1)),$((${lines[$k]}-1))s/^/${ips[$j]} /" /wuhao/sh/files/nmap2.txt
else
sed -i "$((${lines[$j]}+1)),$""s/^/${ips[$j]} /" /wuhao/sh/files/nmap2.txt
fi
j=$j+
done # 将多个空格以及/替换为一个空格
sed -i 's/ \+\|\// /g' /wuhao/sh/files/nmap2.txt
nmap2.txt文件内容为:
Nmap scan report for oos01 (192.168.20.1)
192.168.20.1 tcp open ftp
192.168.20.1 tcp open ssh
192.168.20.1 tcp filtered http
Nmap scan report for oos02 (192.168.20.2)
192.168.20.2 tcp open ssh
192.168.20.2 tcp open http
192.168.20.2 tcp open mysql
Nmap scan report for oos03 (192.168.20.3)
192.168.20.3 tcp open ssh
192.168.20.3 tcp open http
192.168.20.3 tcp open mysql
4、提取出需要关闭的端口(除了端口22之外,其余端口全部关闭)。通过sshpass远程登录到各主机,并且在iptables执行关闭端口命令。
# 提取出需要关闭的端口(除了端口22之外,其余端口如果打开则全部关闭)
awk '{if($4~/open/ && $2!=22) print $0}' /wuhao/sh/files/nmap2.txt > /wuhao/sh/files/nmap3.txt hostip=($(awk -F " " '{print $1}' /wuhao/sh/files/nmap3.txt))
port=($(awk -F " " '{print $2}' /wuhao/sh/files/nmap3.txt))
protocol=($(awk -F " " '{print $3}' /wuhao/sh/files/nmap3.txt)) # 通过sshpass远程登录到各主机,并且在iptables执行关闭端口命令
for((m=;m<${#hostip[*]};m=m+))
do
sshpass -p 123456 ssh root@${hostip[$m]} "iptables -A INPUT -p ${protocol[$m]} --dport ${port[$m]} -j DROP;service iptables save;service iptables restart;exit"
done echo "success!"
5、运行脚本,查看结果。
[root@oos01 sh]# sh shutdownport.sh 192.168.20.1-
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
iptables: Saving firewall rules to /etc/sysconfig/iptables: [ OK ]
iptables: Setting chains to policy ACCEPT: filter [ OK ]
iptables: Flushing firewall rules: [ OK ]
iptables: Unloading modules: [ OK ]
iptables: Applying firewall rules: [ OK ]
success!
linux shell实现批量关闭局域网中主机端口的更多相关文章
- Linux shell脚本 批量创建多个用户
Linux shell脚本 批量创建多个用户 #!/bin/bash groupadd charlesgroup for username in charles1 charles2 charles3 ...
- 使用shell查看局域网中主机的IP地址
此脚本只是快速查看所在局域网中其它主机的IP地址,如果对方设置了禁ping,则无法显示出此主机的IP地址: #!/bin/bash ` do ping -c2 .$i &>/dev/nu ...
- linux shell脚本:在脚本中实现读取键盘输入,根据输入判断下一步的分支
echo please input “runbip” to run bip. variableName="null" while [ $variableName != " ...
- Linux - Shell - 在多个文件中查找关键字
1. 概述 在多个文件中 查找内容 2. 想干啥 目的 在 多个文件 中, 查找内容 准备 之前在 单个文件里 查找过内容 工具 awk 前提 文件有固定格式 查找时有字段的要求 例子 # print ...
- linux安装后ping不通局域网其他主机的解决方式
安装了linux后尝试进行机器间的相互通讯,发现自己虚拟机并不能查看ip地址,也不能够ping通任何一台局域网内的主机 上网查了一下发现是网卡并没有打开,需要进行如下配置 查看ls 一下/etc/sy ...
- Linux shell下批量创建缩略图
一.背景 今天,突然发现手机客户端上的最新新闻缩略图都不显示了,上服务器上看了看, 发现新的新闻图片根本没有生成缩略图. 这套新闻发布系统是很老的程序了,查了一下,问题的原因是不支持png格式的图片, ...
- shell脚本批量配置多台主机静态ip
关于脚本 服务器使用之前,都需要先配置静态IP,那就将这种简单重复的工作,交给脚本来处理吧,让我们运维有更多的时间喝茶看报刷微博 脚本使用 sh ssh.sh ip.txt ssh.sh 为脚本的名称 ...
- Linux Shell基础 多个命令中的分号(;)、与(&&) 、 或(||)
概述 在 Bash 中,如果需要让多条命令按顺序执行,则有这样方法,如表 1 所示. 多命令执行符 格 式 作 用 : 命令1 ; 命令2 多条命令顺序执行,命令之间没有任何逻辑关系 &&am ...
- shell下批量除去文件名中的空格
rename 's/ /_/g' * 上述命令可以将当前文件夹内所有文件的名字中得所有空格替换为_.其中g代表所有,如果不加g,如果文件名字中有多个空格,仅替换第一个.
随机推荐
- [C#.Net]启动外部程序的几种常用方法汇总
本文汇总了C#启动外部程序的几种常用方法,非常具有实用价值,主要包括如下几种方法: 1. 启动外部程序,不等待其退出. 2. 启动外部程序,等待其退出. 3. 启动外部程序,无限等待其退出. 4. 启 ...
- 2018.12.31 bzoj3992: [SDOI2015]序列统计(生成函数+ntt+快速幂)
传送门 生成函数简单题. 题意:给出一个集合A={a1,a2,...as}A=\{a_1,a_2,...a_s\}A={a1,a2,...as},所有数都在[0,m−1][0,m-1][0,m− ...
- PC Access的使用
需要copy xxx.dll 到windows/syswow64 目录下 运行com注册 启动电脑后,自动锁定(在启动目录下架锁定程序) using System; using System.Col ...
- 【终极答案】搭建selenium3.11 +Firefox+python3.6自动化UI测试环境踩的坑
1 运行之后,出现如下报错 Selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs ...
- php $_SERVER中的一些选项说明
1, $_SERVER['SCRIPT_FILENAME'] 和 常量 __FILE__的区别(一般情况下两者的显示相同,都是显示文件的绝对路径,包括文件名,显示的起点是 电脑根目录 /) ①,如果在 ...
- 3-具体学习git--reset回到过去的版本(commit间穿梭),checkout单个文件穿梭
git log --oneline 命令可以在一块儿显示做过的改动. 我在change 2时忘了一条,想在change 1后再添加一个语句或一个操作,然后这个状态再提交仍作为change 2.将这个s ...
- hadoop yarn组件介绍
Yarn的产生 mapReduc1.0 1单点故障 2扩展效率低 3资源利用率高 降低运维成本 方便数据共享 多计算框架支持 MapReduce Spark Storm Yarn的架构图 Yarn模块 ...
- VIP之Clipper
裁剪器II提供方法从视频流中选择有效区域并丢弃剩余部分. 指定有效区域的方式是从到边界的偏移量,或者给出有效区左上角的像素坐标和有效区的宽及高度. 裁剪器IP核通过读取Avalon-ST视频流中的控制 ...
- C#重点内容之:委托(delegate)
为了记忆方便,提取了重点. 委托类似于指针,可以理解为函数指针的升级版,这是理解委托最关键的地方. Action和Func 系统自带的两种委托: Action Func Action型委托要求委托的目 ...
- C++ 中的异常机制分析
C++异常机制概述 异常处理是C++的一项语言机制,用于在程序中处理异常事件.异常事件在C++中表示为异常对象.异常事件发生时,程序使用throw关键字抛出异常表达式,抛出点称为异常出现点,由操作系统 ...