免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3032 | 回复: 0
打印 上一主题 下一主题

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

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2010-11-13 20:20 |只看该作者 |倒序浏览
shell配合ruby完成的。纯ruby的我还不会。。。


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

  4. work_dir="/root/tools/develop"
  5. ports_orig_file="${work_dir}/ports_orig"
  6. ports_current_file="${work_dir}/ports_current"

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

  12. touch ${ports_orig_file}
  13. touch ${ports_current_file}

  14. if [[ -e ${ports_orig_file} ]];then
  15. :
  16. else
  17.     touch ${ports_orig_file}
  18.     `netstat -ntlp > "${ports_orig_file}"`
  19. fi

  20. #生成一个ports_current文件
  21. netstat -ntlp > "${ports_current_file}"
  22. ./duibi.rb
  23. mv -f ${ports_current_file} ${ports_orig_file}
  24. #END
复制代码
duibi.rb
  1. #!/usr/bin/ruby

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

  4. puts "closed ports:"
  5. puts old_array-current_array

  6. puts "new open ports:"
  7. puts current_array-old_array
  8. #END
复制代码
执行的时候运行./1.sh即可。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP