zoukankan      html  css  js  c++  java
  • 全国职业技能大赛信息安全管理与评估-操作系统判断脚本

    赛题:

    #coding=utf-8 
    import os
    import re
     
    def pingos(ip):
        a=os.popen('ping ' +Flag3+' -c 1 -w 10')
        b=a.read()
        c=re.search(r"ttl=[0-9]*",b)
        if c:
            c=c.group()
            ttl=re.search(r"[0-9]*[0-9]",c).group()
            if 128<int(ttl)<=255:
                print "unix"
            elif 64< int(ttl)<=128:
                print "windows /2000"
            elif 32<int(ttl) <=64:
                print "linux/win7"
            elif 0<int(ttl)<=32:
                print "windows 95/98"
    def main():
        fp = open('/root/ip.txt',Flag2)
        for ip in fp.readlines():    
            pingos(Flag1)
        fp.close()
    if __name__ == '__main__':
        main()
     
    #FLAG1 = ip
    #FLAG2 = r
    #FLAG3 = ip 
    
    #行吧 是我想多了 赛题出的空缺 完全没有re模块的

    考察了re模块的基础使用,和os模块命令回显的使用
    我重写后的代码:

    #!/usr/bin/python3
    import os
    import re
     
    def checkTTL(str_ip):
        #获取ping的值
        ping_command = "ping " + str(str_ip) + " -c 1" +"-w 10" 
       # print("ping_command:%s" %(ping_command))
        ping_res = os.popen(ping_command).read(); #read()函数 获取ping后命令的回显
       # print("ping_res:"+ping_res.read());
        ttl_str = re.search("ttl=[0-9]*",ping_res).group()
        print("ttl = %s" %(ttl_str))
        ttl = re.search("[0-9]*[0-9]",ttl_str).group()
        print("ttl:%s" % ttl)
        #通过2次正则表达式 得到出ttl的值 然后比较ttl的值
        ttl_value = int(ttl);
        #print("%d" % ttl_value);
        os_version ={128:'Windows NT/2000',
                    32:'Windows 95/98',
                    255:'Unix',
                    64:'Linux or Win7'
                    }
        if ttl_value in os_version:
            print(os_version[ttl_value])
        else:
            print("Other Os");
     
     
    def main():
        fp = open("ip.txt","r")
        for i in fp.readlines():
            ip = i.strip('
    ')
            checkTTL(ip);
     
     
     
     
     
    if __name__ == '__main__':
        main();

    运行结果:

  • 相关阅读:
    vim技巧2
    vim技巧1
    网站压力测试工具
    CentOS mysql安装
    破解root
    渐进式性能监测案例
    网络监测介绍
    I/O检测介绍
    虚拟内存介绍
    @Slf4j
  • 原文地址:https://www.cnblogs.com/nul1/p/10990133.html
Copyright © 2011-2022 走看看