import java.io.BufferedReader;
2 import java.io.IOException;
3 import java.io.InputStreamReader;
4 import java.io.OutputStream;
5
6 import com.sshtools.j2ssh.SshClient;
7 import com.sshtools.j2ssh.authentication.AuthenticationProtocolState;
8 import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient;
9 import com.sshtools.j2ssh.session.SessionChannelClient;
10
11 public class Main {
12
13 public static void main(String[] args) {
14 SshClient ssh = new SshClient();
15 PasswordAuthenticationClient authentication = new PasswordAuthenticationClient();
16 authentication.setUsername("root");
17 authentication.setPassword("123");
18 try {
19 ssh.connect("192.168.94.254", 22, new HostsKeyVerification());
20 if (ssh.authenticate(authentication) == AuthenticationProtocolState.COMPLETE) {
21 SessionChannelClient session = ssh.openSessionChannel();
22 // session.setEnvironmentVariable("TERM", "linux");
23 // if (client.requestPseudoTerminal("vt100", 120, 400, 0, 0,
24 // "")) {
25 if (session.startShell()) {
26 OutputStream writer = session.getOutputStream();
27 writer.write("echo $?\n".getBytes());
28 writer.flush();
29 writer.write("exit\n".getBytes());
30 writer.flush();
31 BufferedReader in = new BufferedReader(
32 new InputStreamReader(session.getInputStream()));
33 BufferedReader err = new BufferedReader(
34 new InputStreamReader(session
35 .getStderrInputStream()));
36 String line;
37 while ((line = in.readLine()) != null) {
38 System.out.println(line);
39 }
40 System.out.println("------------------------");
41 while ((line = err.readLine()) != null) {
42 System.out.println(line);
43 }
44 if (session != null) {
45 session.close();
46 }
47 }
48 // }
49 }
50 } catch (IOException e) {
51 e.printStackTrace();
52 } finally {
53 }
54
55 }
56
57 }