#!/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 ===')
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 ===')