sbl.py

6 篇文章 0 订阅
4 篇文章 0 订阅
#!/usr/bin/python3
import subprocess
import time
import pexpect
#this is the IP LIST file name and path
IPLIST = 'sbliplist.txt'
DOMAINLIST = ['.hc.cn','.homecredit.cn']
#this is the log file location
LOGFILE = 'iplistlog.txt'

def sshopen(hostip):
    try:
        ret = subprocess.getoutput('nmap -sV -p 22 %s'%hostip)
        if ret.find('22/tcp open  ssh') != -1:
            return 1
        else:
            return 0
    except Exception as err:
        return 2

def connectssh(host,username,password):
    try:
        child = pexpect.spawn('ssh %s@%s'%(username,host),timeout=3)
        ret = child.expect([pexpect.TIMEOUT,'yes/no','assword'])
        if ret == 1:
            child.sendline('yes')
            ret = child.expect([pexpect.TIMEOUT,'yes/no','assword'])
        if ret == 0:
            return 2
        if ret == 2:
            child.sendline(password)
            return 1
        if ret == 1:
            return 0
    except Exception as err:
        return 0

def dnscheck(host,dnsserver=None):
    for i in DOMAINLIST:
        if dnsserver == None:
            out = subprocess.getoutput('nslookup %s%s'%(host,i))
        else:
            out = subprocess.getoutput('nslookup %s%s %s'%(host,i,dnsserver))
        p = out.find('Name:')
        if p == -1:
            pass
        else:
            p = out[p:].find('Address: ')+9
            return [1,'%s%s'%(host,i),out[p:]]
    return [0,host,host]
            
def pingcheck(host):
    #ip = dnscheck(host,'10.24.2.1')
    #if ip == None:
        #ip = host
    ret = subprocess.getoutput('ping %s -c 1 -W 1'%host)
    if ret.find('1 received') != -1:
        return 1
    else:
        return 0

def rdpopen(host):
    ret = subprocess.getoutput('nmap -sV -p 3389 %s'%host)
    if ret.find('3389/tcp open  ms-wbt-server') != -1:
        return 1
    else:
        return 0

fh = open(IPLIST)
fh2 = open(LOGFILE,'a')
print('%-15s %-15s %-7s %-10s %s'%('HOSTNAME','IP','PING','PORTOCOL','STATUS'),end='')   
for i in fh.readlines():
    i = i.strip('\n')
    try:
        host = dnscheck(i)
        hostname = host[1]
        hostip = host[2]
        print('\n%-15s %-15s'%(hostname,hostip),end=' ')
        r1 = pingcheck(hostip)
        if r1 == 0:
            print('%-7s'%'NOPING ',end=' ')
        else:
            print('%-7s'%'PING',end=' ')
        r2 = sshopen(hostip)
        if r2 == 2:
            print('%-10s'%'  ',end=' ')
        elif r2 == 1:
            print('%-10s'%'SSH OPEN ',end=' ')
            ret = connectssh(hostip,hostip,'testpassword')
            if ret == 1:
                print('ssh connected',end='')
            elif ret == 2:
                print('ssh connection error',end='')
            elif ret == 0:
                print('Error in SSH',end='')
        elif r2 == 0:
            r3 = rdpopen(hostip)
            if r3 == 1:
                print('%-10s'%'RDP OPEN',end=' ')
            elif r3 == 0:
                print('%-10s'%' ',end='')
    except Exception as err:
        print('ERR!',end='')
        #pass
        #print(err)

fh.close()
fh2.close()
print('\n=== END ===')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值