kongdeqiang
2025-12-02 3bd9c82bf2b67833ad60fc9522da0b50b63f5d2a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package com.wgcloud.service;
 
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
 
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
 
@Service
public class RouterConnectionService {
 
    private Session session;
 
//    /**
//     * 连接到路由器
//     */
//    public boolean connect() {
//        try {
//            JSch jsch = new JSch();
//            session = jsch.getSession(routerUsername, routerHost, 22);
//            session.setPassword(routerPassword);
//
//            // 设置不进行主机密钥检查
//            Properties config = new Properties();
//            config.put("StrictHostKeyChecking", "no");
//            session.setConfig(config);
//
//            session.connect(5000); // 5秒超时
//            return true;
//        } catch (JSchException e) {
//            System.err.println("连接路由器失败: " + e.getMessage());
//            return false;
//        }
//    }
 
    /**
     * 连接到路由器
     */
    public boolean connect(String username,String hostname,String password) {
        try {
            JSch jsch = new JSch();
            session = jsch.getSession(username, hostname, 22);
            session.setPassword(password);
 
            // 设置不进行主机密钥检查
            Properties config = new Properties();
            config.put("StrictHostKeyChecking", "no");
            session.setConfig(config);
 
            session.connect(5000); // 5秒超时
            return true;
        } catch (JSchException e) {
            System.err.println("连接路由器失败: " + e.getMessage());
            return false;
        }
    }
 
    /**
     * 断开连接
     */
    public void disconnect() {
        if (session != null && session.isConnected()) {
            session.disconnect();
        }
    }
 
    /**
     * 执行路由器命令
     */
    public String executeCommand(String command,String username,String password,String hostname) {
//        if (!isConnected()) {
//            if (!connect(username,hostname,password)) {
//                return null;
//            }
//        }
        if (!connect(username,hostname,password)) {
            return null;
        }
 
        try {
            ChannelExec channel = (ChannelExec) session.openChannel("exec");
            channel.setCommand(command);
            BufferedReader reader = new BufferedReader(new InputStreamReader(channel.getInputStream()));
            StringBuilder output = new StringBuilder();
            String line;
 
            while ((line = reader.readLine()) != null) {
                output.append(line).append("\n");
            }
 
            reader.close();
            channel.disconnect();
            disconnect();
            return output.toString();
        } catch (Exception e) {
            System.err.println("执行命令失败: " + e.getMessage());
            disconnect(); // 执行失败时断开连接,下次重连
            return null;
        }
    }
 
    /**
     * 检查连接状态
     */
    public boolean isConnected() {
        return session != null && session.isConnected();
    }
}