主要用于探测主机是否开机以及查看端口的监听状态
使用Nmap作为底层探查工具,具体使用请访问Nmap官网。使用python模块python-nmap封装调用命令完成扫描。通过使用jython接口完成工具类方法的封装。
安装: Nmap
依赖: jython(已将python-nmap模块添加到jython中)
ping:
{“10.4.45.235”: “C4:0A:CB:1D:F0:C0”}
端口扫描:
## pingscan.py
#!/usr/bin/python
import json
import sys
import os
import nmap
def pingscan(h):
nm = nmap.PortScanner()
r = nm.scan(hosts=h, arguments='-n -sn --host-timeout 5')
result={}
for ip,content in r["scan"].items():
addrs = content.get("addresses",{})
result[ip]=addrs.get("mac","")
return json.dumps(result)
def scanports(hosts, ports):
try :
nm = nmap.PortScanner()
except nmap.PortScannerError :
print('nmap not found',sys.exc_info()[0])
sys.exit()
except :
print('unexpected error:',sys.exc_info()[0])
sys.exit()
try:
if ports.strip():
arg_scan = ' -v -sS -sU -p ' + ports
else:
arg_scan = ' -v -sS -sU'
nm.scan(hosts=hosts,arguments=arg_scan)
except Exception as e:
print('scan error:'+str(e))
result = []
lhosts = nm.all_hosts()
lhosts = sorted(lhosts, key = lambda x: int(''.join((lambda a:lambda v:a(a,v))(lambda s,x: x if len(x) == 3 else s(s, '0'+x))(i) for i in x.split('.'))))
for host in lhosts:
hostinfo = {}
hostinfo['host'] = host
hostinfo['state'] = nm[host].state()
hostinfo['scan'] = []
for proto in nm[host].all_protocols():
portsinfo = {}
portsinfo['protocol'] = proto
lport = nm[host][proto].keys()
lport.sort(key=int)
ports = []
for port in lport:
portinfo = {}
portinfo['port'] = port
portinfo['state'] = nm[host][proto][port]['state']
portinfo['service'] = nm[host][proto][port]['name']
ports.append(portinfo )
portsinfo['ports'] = ports
hostinfo['scan'].append(portsinfo)
result.append(hostinfo)
return json.dumps(result)
def scanportstcp(hosts, ports):
try :
nm = nmap.PortScanner()
except nmap.PortScannerError :
print('nmap not found',sys.exc_info()[0])
sys.exit()
except :
print('unexpected error:',sys.exc_info()[0])
sys.exit()
try:
if ports.strip():
arg_scan = ' -v -sS -p ' + ports
else:
arg_scan = ' -v -sS'
nm.scan(hosts=hosts,arguments=arg_scan)
except Exception as e:
print('scan error:'+str(e))
result = []
lhosts = nm.all_hosts()
lhosts = sorted(lhosts, key = lambda x: int(''.join((lambda a:lambda v:a(a,v))(lambda s,x: x if len(x) == 3 else s(s, '0'+x))(i) for i in x.split('.'))))
for host in lhosts:
hostinfo = {}
hostinfo['host'] = host
hostinfo['state'] = nm[host].state()
hostinfo['scan'] = []
for proto in nm[host].all_protocols():
portsinfo = {}
portsinfo['protocol'] = proto
lport = nm[host][proto].keys()
lport.sort(key=int)
ports = []
for port in lport:
portinfo = {}
portinfo['port'] = port
portinfo['state'] = nm[host][proto][port]['state']
portinfo['service'] = nm[host][proto][port]['name']
ports.append(portinfo )
portsinfo['ports'] = ports
hostinfo['scan'].append(portsinfo)
result.append(hostinfo)
return json.dumps(result)
def scanportsudp(hosts, ports):
try :
nm = nmap.PortScanner()
except nmap.PortScannerError :
print('nmap not found',sys.exc_info()[0])
sys.exit()
except :
print('unexpected error:',sys.exc_info()[0])
sys.exit()
try:
if ports.strip():
arg_scan = ' -v -sU -p ' + ports
else:
arg_scan = ' -v -sU'
nm.scan(hosts=hosts,arguments=arg_scan)
except Exception as e:
print('scan error:'+str(e))
result = []
lhosts = nm.all_hosts()
lhosts = sorted(lhosts, key = lambda x: int(''.join((lambda a:lambda v:a(a,v))(lambda s,x: x if len(x) == 3 else s(s, '0'+x))(i) for i in x.split('.'))))
for host in lhosts:
hostinfo = {}
hostinfo['host'] = host
hostinfo['state'] = nm[host].state()
hostinfo['scan'] = []
for proto in nm[host].all_protocols():
portsinfo = {}
portsinfo['protocol'] = proto
lport = nm[host][proto].keys()
lport.sort(key=int)
ports = []
for port in lport:
portinfo = {}
portinfo['port'] = port
portinfo['state'] = nm[host][proto][port]['state']
portinfo['service'] = nm[host][proto][port]['name']
ports.append(portinfo )
portsinfo['ports'] = ports
hostinfo['scan'].append(portsinfo)
result.append(hostinfo)
return json.dumps(result)
// PingUtil.java
/**
* 说明:
* 将上面的python脚本放到固定路径下,如 D:\\pingscan.py,调用相应方法获取返回结果,此文件依赖IPUtil.java,用于IP转换。
* processPingscan返回结果为可ping通的主机,并返回MAC地址;
* processPingscanSeg传入网段,如10.4.45.1-100,或者指定起始及结束范围;
* processPingscanSub传入掩码信息,如ip:10.4.45.0, mask:24;
* scanPorts使用TCP和UPD协议扫描指定端口,查看端口情况
*
**/
import java.io.File;
import java.util.List;
import org.json.JSONObject;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.python.core.PyFunction;
import org.python.core.PyObject;
import org.python.core.PyString;
import org.python.util.PythonInterpreter;
public class PingUtil {
private static final Log log = LogFactory.getLog(PingUtil.class);
public static final String PINGSCAN_LOCATION = "D:\\pingscan.py";
private static PythonInterpreter interpreter;
static {
interpreter = new PythonInterpreter();
interpreter.execfile(PINGSCAN_LOCATION);
}
/**
* 调用python获取ping结果
*
* @return
*/
public static Object processPingscan(List resourceIPList) {
Object jsonObject = null;
try {
if (!CollectionUtils.isEmpty(resourceIPList)) {
StringBuffer scanIPs = new StringBuffer();
for (String ip : resourceIPList) {
scanIPs.append(ip);
scanIPs.append(" ");
}
PyFunction pingscan = (PyFunction) interpreter.get("pingscan",
PyFunction.class);
PyObject pyobj = pingscan.__call__(new PyString(scanIPs.toString()));
if (!StringUtils.isEmpty(pyobj.toString())) {
jsonObject = JSONObject.stringToValue(pyobj.toString());
}
}
} catch (Exception e) {
log.error("ping nmap task" + e);
}
return jsonObject;
}
/**
* 调用python获取ping结果
*
* @param ipRange
* 10.4.45.1-100
* @param
* @return
*/
public static Object processPingscanSeg(String ipRange) {
Object jsonObject = null;
try {
PyFunction pingscan = (PyFunction) interpreter.get("pingscan",
PyFunction.class);
PyObject pyobj = pingscan.__call__(new PyString(ipRange));
if (!StringUtils.isEmpty(pyobj.toString())) {
jsonObject = JSONObject.stringToValue(pyobj.toString());
}
} catch (Exception e) {
log.error("ping nmap task" + e);
}
return jsonObject;
}
/**
* 调用python获取ping结果
*
* @param ipRange
* 10.4.45.1-100
* @param
* @return
*/
public static Object processPingscanSeg(String ipfrom, String ipto) {
Object jsonObject = null;
try {
List resourceIPList = IPUtil.parseIpRange(ipfrom, ipto);
jsonObject = processPingscan(resourceIPList);
} catch (Exception e) {
log.error("ping nmap task" + e);
}
return jsonObject;
}
/**
* 调用python获取ping结果
*
* @param ip
* @param mask
* @return
*/
public static Object processPingscanSub(String ip, String mask) {
Object jsonObject = null;
try {
if (!StringUtils.isEmpty(ip) && !StringUtils.isEmpty(mask)
&& IPUtil.isIP(ip)) {
PyFunction pingscan = (PyFunction) interpreter.get("pingscan",
PyFunction.class);
PyObject pyobj = pingscan.__call__(new PyString(ip + "/" + mask));
if (!StringUtils.isEmpty(pyobj.toString())) {
jsonObject = JSONObject.stringToValue(pyobj.toString());
}
}
} catch (Exception e) {
log.error("ping nmap task" + e);
}
return jsonObject;
}
/**
* 调用python获取扫描端口结果
*
* @return
*/
public static Object scanPorts(List resourceIPList, List portsList) {
Object jsonObject = null;
try {
if (!CollectionUtils.isEmpty(resourceIPList) && !CollectionUtils.isEmpty(portsList)) {
String ips = StringUtils.join(resourceIPList, " ");
String ports = StringUtils.join(portsList, ",");
PyFunction scanports = (PyFunction) interpreter.get("scanports",
PyFunction.class);
PyObject pyobj = scanports.__call__(new PyString(ips), new PyString(ports));
if (!StringUtils.isEmpty(pyobj.toString())) {
jsonObject = JSONObject.stringToValue(pyobj.toString());
}
}
} catch (Exception e) {
log.error("scan nmap task" + e);
}
return jsonObject;
}
/**
* 调用python获取扫描端口结果
*
* @return
*/
public static Object scanPortsByTCP(List resourceIPList, List portsList) {
Object jsonObject = null;
try {
if (!CollectionUtils.isEmpty(resourceIPList) && !CollectionUtils.isEmpty(portsList)) {
String ips = StringUtils.join(resourceIPList, " ");
String ports = StringUtils.join(portsList, ",");
PyFunction scanportstcp = (PyFunction) interpreter.get("scanportstcp",
PyFunction.class);
PyObject pyobj = scanportstcp.__call__(new PyString(ips), new PyString(ports));
if (!StringUtils.isEmpty(pyobj.toString())) {
jsonObject = JSONObject.stringToValue(pyobj.toString());
}
}
} catch (Exception e) {
log.error("scan nmap task" + e);
}
return jsonObject;
}
/**
* 调用python获取扫描端口结果
*
* @return
*/
public static Object scanPortsByUDP(List resourceIPList, List portsList) {
Object jsonObject = null;
try {
if (!CollectionUtils.isEmpty(resourceIPList) && !CollectionUtils.isEmpty(portsList)) {
String ips = StringUtils.join(resourceIPList, " ");
String ports = StringUtils.join(portsList, ",");
PyFunction scanportsudp = (PyFunction) interpreter.get("scanportsudp",
PyFunction.class);
PyObject pyobj = scanportsudp.__call__(new PyString(ips), new PyString(ports));
if (!StringUtils.isEmpty(pyobj.toString())) {
jsonObject = JSONObject.stringToValue(pyobj.toString());
}
}
} catch (Exception e) {
log.error("scan nmap task" + e);
}
return jsonObject;
}
/**
* 调用python获取扫描端口结果-tcp
*
* @return
*/
public static Object scanallPortsByTCP(List resourceIPList) {
Object jsonObject = null;
try {
if (!CollectionUtils.isEmpty(resourceIPList)) {
String ips = StringUtils.join(resourceIPList, " ");
PyFunction scanportstcp = (PyFunction) interpreter.get("scanportstcp",
PyFunction.class);
PyObject pyobj = scanportstcp.__call__(new PyString(ips), new PyString(""));
if (!StringUtils.isEmpty(pyobj.toString())) {
jsonObject = JSONObject.stringToValue(pyobj.toString());
}
}
} catch (Exception e) {
log.error("scan nmap task" + e);
}
return jsonObject;
}
}
// IPUtil.java
/**
* 实现ip地址转换
**/
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class IPUtil {
private static final Log log = LogFactory.getLog(IPUtil.class);
public static boolean isIP(String addr) {
if (addr.length() < 7 || addr.length() > 15 || "".equals(addr)) {
return false;
}
/**
* 判断IP格式和范围
*/
String rexp = "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
Pattern pat = Pattern.compile(rexp);
Matcher mat = pat.matcher(addr);
boolean isIpAddress = mat.find();
return isIpAddress;
}
public static List parseIpMaskRange(String ip, String mask) {
List list = new ArrayList();
if(isIP(ip)) {
if ("32".equals(mask)) {
list.add(ip);
} else {
String startIp = getBeginIpStr(ip, mask);
String endIp = getEndIpStr(ip, mask);
if (!"31".equals(mask)) {
String subStart = startIp.split("\\.")[0] + "."
+ startIp.split("\\.")[1] + "."
+ startIp.split("\\.")[2] + ".";
String subEnd = endIp.split("\\.")[0] + "."
+ endIp.split("\\.")[1] + "." + endIp.split("\\.")[2]
+ ".";
startIp = subStart
+ (Integer.valueOf(startIp.split("\\.")[3]) + 1);
endIp = subEnd + (Integer.valueOf(endIp.split("\\.")[3]) - 1);
}
list = parseIpRange(startIp, endIp);
}
} else {
log.error("parseIpMaskRange: ip is invalid");
}
return list;
}
public static List parseIpRange(String ipfrom, String ipto) {
List ips = new ArrayList();
if(isIP(ipfrom) && isIP(ipto) && getIpFromString(ipfrom) < getIpFromString(ipto)) {
String[] ipfromd = ipfrom.split("\\.");
String[] iptod = ipto.split("\\.");
int[] int_ipf = new int[4];
int[] int_ipt = new int[4];
for (int i = 0; i < 4; i++) {
int_ipf[i] = Integer.parseInt(ipfromd[i]);
int_ipt[i] = Integer.parseInt(iptod[i]);
}
for (int A = int_ipf[0]; A <= int_ipt[0]; A++) {
for (int B = (A == int_ipf[0] ? int_ipf[1] : 0); B <= (A == int_ipt[0] ? int_ipt[1]
: 255); B++) {
for (int C = (B == int_ipf[1] ? int_ipf[2] : 0); C <= (B == int_ipt[1] ? int_ipt[2]
: 255); C++) {
for (int D = (C == int_ipf[2] ? int_ipf[3] : 0); D <= (C == int_ipt[2] ? int_ipt[3]
: 255); D++) {
ips.add(new String(A + "." + B + "." + C + "." + D));
}
}
}
}
} else {
log.error("parseIpRange: ip is invalid!");
}
return ips;
}
/**
* 把long类型的Ip转为一般Ip类型:xx.xx.xx.xx
*
* @param ip
* @return
*/
public static String getIpFromLong(Long ip) {
String s1 = String.valueOf((ip & 4278190080L) / 16777216L);
String s2 = String.valueOf((ip & 16711680L) / 65536L);
String s3 = String.valueOf((ip & 65280L) / 256L);
String s4 = String.valueOf(ip & 255L);
return s1 + "." + s2 + "." + s3 + "." + s4;
}
/**
* 把xx.xx.xx.xx类型的转为long类型的
*
* @param ip
* @return
*/
public static Long getIpFromString(String ip) {
Long ipLong = 0L;
String ipTemp = ip;
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
ipLong = ipLong * 256
+ Long.parseLong(ipTemp.substring(0, ipTemp.indexOf(".")));
ipTemp = ipTemp.substring(ipTemp.indexOf(".") + 1, ipTemp.length());
ipLong = ipLong * 256 + Long.parseLong(ipTemp);
return ipLong;
}
/**
* 根据掩码位获取掩码
*
* @param maskBit
* 掩码位数,如"28"、"30"
* @return
*/
public static String getMaskByMaskBit(String maskBit) {
return StringUtils.isEmpty(maskBit) ? "error, maskBit is null !"
: maskBitMap().get(maskBit);
}
/**
* 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30
*
* @param ip
* 给定的IP,如218.240.38.69
* @param maskBit
* 给定的掩码位,如30
* @return 起始IP的字符串表示
*/
public static String getBeginIpStr(String ip, String maskBit) {
return getIpFromLong(getBeginIpLong(ip, maskBit));
}
/**
* 根据 ip/掩码位 计算IP段的起始IP 如 IP串 218.240.38.69/30
*
* @param ip
* 给定的IP,如218.240.38.69
* @param maskBit
* 给定的掩码位,如30
* @return 起始IP的长整型表示
*/
public static Long getBeginIpLong(String ip, String maskBit) {
return getIpFromString(ip) & getIpFromString(getMaskByMaskBit(maskBit));
}
/**
* 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30
*
* @param ip
* 给定的IP,如218.240.38.69
* @param maskBit
* 给定的掩码位,如30
* @return 终止IP的字符串表示
*/
public static String getEndIpStr(String ip, String maskBit) {
return getIpFromLong(getEndIpLong(ip, maskBit));
}
/**
* 根据 ip/掩码位 计算IP段的终止IP 如 IP串 218.240.38.69/30
*
* @param ip
* 给定的IP,如218.240.38.69
* @param maskBit
* 给定的掩码位,如30
* @return 终止IP的长整型表示
*/
public static Long getEndIpLong(String ip, String maskBit) {
return getBeginIpLong(ip, maskBit)
+ ~getIpFromString(getMaskByMaskBit(maskBit));
}
/**
* 根据子网掩码转换为掩码位 如 255.255.255.252转换为掩码位 为 30
*
* @param netmarks
* @return
*/
public static int getNetMask(String netmarks) {
StringBuffer sbf;
String str;
int inetmask = 0, count = 0;
String[] ipList = netmarks.split("\\.");
for (int n = 0; n < ipList.length; n++) {
sbf = toBin(Integer.parseInt(ipList[n]));
str = sbf.reverse().toString();
count = 0;
for (int i = 0; i < str.length(); i++) {
i = str.indexOf('1', i);
if (i == -1) {
break;
}
count++;
}
inetmask += count;
}
return inetmask;
}
/**
* 计算子网大小
*
* @param netmask
* 掩码位
* @return
*/
public static int getPoolMax(int maskBit) {
if (maskBit <= 0 || maskBit >= 32) {
return 0;
}
return (int) Math.pow(2, 32 - maskBit) - 2;
}
private static StringBuffer toBin(int x) {
StringBuffer result = new StringBuffer();
result.append(x % 2);
x /= 2;
while (x > 0) {
result.append(x % 2);
x /= 2;
}
return result;
}
/*
* 存储着所有的掩码位及对应的掩码 key:掩码位 value:掩码(x.x.x.x)
*/
private static Map maskBitMap() {
Map maskBit = new HashMap();
maskBit.put("1", "128.0.0.0");
maskBit.put("2", "192.0.0.0");
maskBit.put("3", "224.0.0.0");
maskBit.put("4", "240.0.0.0");
maskBit.put("5", "248.0.0.0");
maskBit.put("6", "252.0.0.0");
maskBit.put("7", "254.0.0.0");
maskBit.put("8", "255.0.0.0");
maskBit.put("9", "255.128.0.0");
maskBit.put("10", "255.192.0.0");
maskBit.put("11", "255.224.0.0");
maskBit.put("12", "255.240.0.0");
maskBit.put("13", "255.248.0.0");
maskBit.put("14", "255.252.0.0");
maskBit.put("15", "255.254.0.0");
maskBit.put("16", "255.255.0.0");
maskBit.put("17", "255.255.128.0");
maskBit.put("18", "255.255.192.0");
maskBit.put("19", "255.255.224.0");
maskBit.put("20", "255.255.240.0");
maskBit.put("21", "255.255.248.0");
maskBit.put("22", "255.255.252.0");
maskBit.put("23", "255.255.254.0");
maskBit.put("24", "255.255.255.0");
maskBit.put("25", "255.255.255.128");
maskBit.put("26", "255.255.255.192");
maskBit.put("27", "255.255.255.224");
maskBit.put("28", "255.255.255.240");
maskBit.put("29", "255.255.255.248");
maskBit.put("30", "255.255.255.252");
maskBit.put("31", "255.255.255.254");
maskBit.put("32", "255.255.255.255");
return maskBit;
}
/**
* 根据掩码位获取掩码
*
* @param masks
* @return
*/
public static String getMaskByMaskBit(int masks) {
String ret = "";
if (masks == 1)
ret = "128.0.0.0";
else if (masks == 2)
ret = "192.0.0.0";
else if (masks == 3)
ret = "224.0.0.0";
else if (masks == 4)
ret = "240.0.0.0";
else if (masks == 5)
ret = "248.0.0.0";
else if (masks == 6)
ret = "252.0.0.0";
else if (masks == 7)
ret = "254.0.0.0";
else if (masks == 8)
ret = "255.0.0.0";
else if (masks == 9)
ret = "255.128.0.0";
else if (masks == 10)
ret = "255.192.0.0";
else if (masks == 11)
ret = "255.224.0.0";
else if (masks == 12)
ret = "255.240.0.0";
else if (masks == 13)
ret = "255.248.0.0";
else if (masks == 14)
ret = "255.252.0.0";
else if (masks == 15)
ret = "255.254.0.0";
else if (masks == 16)
ret = "255.255.0.0";
else if (masks == 17)
ret = "255.255.128.0";
else if (masks == 18)
ret = "255.255.192.0";
else if (masks == 19)
ret = "255.255.224.0";
else if (masks == 20)
ret = "255.255.240.0";
else if (masks == 21)
ret = "255.255.248.0";
else if (masks == 22)
ret = "255.255.252.0";
else if (masks == 23)
ret = "255.255.254.0";
else if (masks == 24)
ret = "255.255.255.0";
else if (masks == 25)
ret = "255.255.255.128";
else if (masks == 26)
ret = "255.255.255.192";
else if (masks == 27)
ret = "255.255.255.224";
else if (masks == 28)
ret = "255.255.255.240";
else if (masks == 29)
ret = "255.255.255.248";
else if (masks == 30)
ret = "255.255.255.252";
else if (masks == 31)
ret = "255.255.255.254";
else if (masks == 32)
ret = "255.255.255.255";
return ret;
}
}