python-后台服务器识别

python-后台服务器识别

后台服务器一般是Linux和Windows server 。

代码如下:

import re
import os
from optparse import OptionParser


def ttl_scan(ip):
    ttlstrmatch = re.compile(r'TTL=\d+')
    ttlmunmatch = re.compile(r'\d+')
    result = os.popen("ping -n 1" + ip)
    res = result.read()
    for line in res.splitlines():
        result = ttlstrmatch.findall(line)
        if result:
            ttl = ttlmunmatch.findall(result[0])
            if int(ttl[0]) <= 64:
                print("%s is Linux/Unix" % ip)
            else:
                print("%s is Windows" % ip)
        else:
            pass


def main():
    parser = OptionParser("Usage:%prog -i <target host>")
    parser.add_option('-i', type='string', dest='IP', help='specify target host')
    options, args = parser.parse_args()
    ip = options.IP
    ttl_scan(ip)


if __name__ == '__main__':
    main()

猜你喜欢

转载自blog.csdn.net/qq_48985780/article/details/121500533