-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSHConnectorApp.java
More file actions
178 lines (148 loc) · 5.91 KB
/
SSHConnectorApp.java
File metadata and controls
178 lines (148 loc) · 5.91 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import com.jcraft.jsch.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
public class SSHConnectorApp extends JFrame {
private JTextField hostField, userField, commandField;
private JPasswordField passwordField;
private JButton connectButton, executeButton, clearButton;
private JTextArea outputArea;
private JLabel connectionStatusLabel;
private Color connectedColor = Color.GREEN;
private Color disconnectedColor = Color.RED;
private Session session;
public SSHConnectorApp() {
super("SSH Connector");
// Create components
hostField = new JTextField("raspberrypi.local", 20);
userField = new JTextField("pi", 20);
passwordField = new JPasswordField(20);
commandField = new JTextField("ls", 30);
connectButton = new JButton("Connect");
connectButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
connectSSH();
}
});
executeButton = new JButton("Execute");
executeButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
executeCommand();
}
});
clearButton = new JButton("Clear Output");
clearButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
outputArea.setText("");
}
});
outputArea = new JTextArea(15, 40);
outputArea.setEditable(false);
JScrollPane scrollPane = new JScrollPane(outputArea);
connectionStatusLabel = new JLabel("Disconnected");
connectionStatusLabel.setForeground(disconnectedColor);
// Set layout
setLayout(new BorderLayout());
// Create panels
JPanel inputPanel = new JPanel(new GridLayout(4, 2));
inputPanel.add(new JLabel("Host:"));
inputPanel.add(hostField);
inputPanel.add(new JLabel("Username:"));
inputPanel.add(userField);
inputPanel.add(new JLabel("Password:"));
inputPanel.add(passwordField);
inputPanel.add(new JLabel("Command:"));
inputPanel.add(commandField);
JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
buttonPanel.add(connectButton);
buttonPanel.add(executeButton);
buttonPanel.add(clearButton);
buttonPanel.add(connectionStatusLabel);
// Add components to the frame
add(inputPanel, BorderLayout.NORTH);
add(scrollPane, BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
// Set frame properties
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null); // Center the frame
setVisible(true);
}
private void connectSSH() {
try {
String host = hostField.getText();
String user = userField.getText();
String password = new String(passwordField.getPassword());
JSch jsch = new JSch();
session = jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no"); // Disable host key checking
// Connect to the SSH server
session.connect();
setStatus("Connected to " + host);
updateConnectionStatus(true); // Update the visual indicator
} catch (JSchException e) {
setStatus("Connection failed. Check credentials and try again.");
updateConnectionStatus(false); // Update the visual indicator
e.printStackTrace();
}
}
private void executeCommand() {
try {
if (session != null && session.isConnected()) {
String customCommand = commandField.getText();
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(customCommand);
channel.setInputStream(null);
((ChannelExec) channel).setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
// Read the command output
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuilder output = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
output.append(line).append("\n");
}
// Display the command output in the JTextArea
outputArea.append("\n" + output.toString());
// Disconnect the channel
channel.disconnect();
setStatus("Command executed successfully.");
} else {
setStatus("Not connected. Please connect first.");
}
} catch (JSchException | IOException e) {
setStatus("Command execution failed.");
e.printStackTrace();
}
}
private void setStatus(String status) {
outputArea.append("\n" + status);
}
private void updateConnectionStatus(boolean isConnected) {
if (isConnected) {
connectionStatusLabel.setText("Connected");
connectionStatusLabel.setForeground(connectedColor);
} else {
connectionStatusLabel.setText("Disconnected");
connectionStatusLabel.setForeground(disconnectedColor);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
new SSHConnectorApp();
}
});
}
}