java 如何端口扫描出ssh端口?

如题,java如何端口扫描出ssh端口?

可以通过socket链接确认端口是否开放,1024之内的有默认提供的服务,但是ssh端口一改,如何确认扫描出的就是ssh端口?

centos 7上测试了一下,比较简单粗暴.代码如下

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader; /** * Created by nicholas on 11/19/2016. */
public class GetProcessInfo { private static final String[] SHELL = {"sh", "-c", "lsof -Pnl +M -i4 | grep sshd"}; private static final String KEY = "LISTEN"; private static String runCommandString[] shell throws IOException, InterruptedException { String result = ""; Process pos = Runtime.getRuntime.execshell; pos.waitFor; InputStreamReader ir = new InputStreamReaderpos.getInputStream; LineNumberReader input = new LineNumberReaderir; String ln = ""; while ln = input.readLine != null { String temp = ln.toString; if temp.containsKEY { result = temp; } } input.close; ir.close; return result; } public static int getPort throws IOException, InterruptedException { String result = runCommandSHELL; if "".equalsresult { System.out.println"get sshd services fail...."; return 0; } String[] split = result.split":"[1].split" "; int port = Integer.valueOfsplit[0]; return port; } public static void mainString[] args throws IOException, InterruptedException { System.out.println"sshd port = " + getPort; }
}

执行结果

你需要 nmap。

其实你 telnet 到一个开着 ssh 服务的端口上就知道怎么确认了。一连上去 sshd 就跟你说,「你好,我是 OpenSSH 7.3,使用 SSH 2.0 协议~」。

发表评论

电子邮件地址不会被公开。 必填项已用*标注