扫码关注官方订阅号
如题,java如何端口扫描出ssh端口?
(可以通过socket链接确认端口是否开放,1024之内的有默认提供的服务,但是ssh端口一改,如何确认扫描出的就是ssh端口?)
ringa_lee
在centos 7上测试了一下,比较简单粗暴.代码如下
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 runCommand(String[] shell) throws IOException, InterruptedException { String result = ""; Process pos = Runtime.getRuntime().exec(shell); pos.waitFor(); InputStreamReader ir = new InputStreamReader(pos.getInputStream()); LineNumberReader input = new LineNumberReader(ir); String ln = ""; while ((ln = input.readLine()) != null) { String temp = ln.toString(); if (temp.contains(KEY)) { result = temp; } } input.close(); ir.close(); return result; } public static int getPort() throws IOException, InterruptedException { String result = runCommand(SHELL); if ("".equals(result)) { System.out.println("get sshd services fail...."); return 0; } String[] split = result.split(":")[1].split(" "); int port = Integer.valueOf(split[0]); return port; } public static void main(String[] args) throws IOException, InterruptedException { System.out.println("sshd port = " + getPort()); } }
执行结果
你需要 nmap。
其实你 telnet 到一个开着 ssh 服务的端口上就知道怎么确认了。一连上去 sshd 就跟你说,「你好,我是 OpenSSH 7.3,使用 SSH 2.0 协议~」。
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
在
centos 7上测试了一下,比较简单粗暴.代码如下执行结果
你需要 nmap。
其实你 telnet 到一个开着 ssh 服务的端口上就知道怎么确认了。一连上去 sshd 就跟你说,「你好,我是 OpenSSH 7.3,使用 SSH 2.0 协议~」。