laohuanggua 发表于 2010-11-13 20:20

利用ruby简洁的数组对比来完成的检查服务器开启关闭端口的程序

shell配合ruby完成的。纯ruby的我还不会。。。


shell部分#!/bin/bash
#date:2010-11-13
#purpose:check new service

work_dir="/root/tools/develop"
ports_orig_file="${work_dir}/ports_orig"
ports_current_file="${work_dir}/ports_current"

if [[ -d ${work_dir} ]];then
    mkdir -p ${work_dir}
fi
#首先判断文件ports_orig是否存在
#如果没有就用netstat -ntlp来生成一个。

touch ${ports_orig_file}
touch ${ports_current_file}

if [[ -e ${ports_orig_file} ]];then
:
else
    touch ${ports_orig_file}
    `netstat -ntlp > "${ports_orig_file}"`
fi

#生成一个ports_current文件
netstat -ntlp > "${ports_current_file}"
./duibi.rb
mv -f ${ports_current_file} ${ports_orig_file}
#END
duibi.rb#!/usr/bin/ruby

old_array=File.open("ports_orig").readlines
current_array=File.open("ports_current").readlines

puts "closed ports:"
puts old_array-current_array

puts "new open ports:"
puts current_array-old_array
#END执行的时候运行./1.sh即可。
页: [1]
查看完整版本: 利用ruby简洁的数组对比来完成的检查服务器开启关闭端口的程序