释放双眼,带上耳机,听听看~!
需要下载一个包:http://101.110.118.74/www.ganymed.ethz.ch/ssh2/ganymed-ssh2-build210.zip
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 1import java.io.IOException;
2import java.io.InputStream;
3import java.io.UnsupportedEncodingException;
4import java.nio.charset.Charset;
5
6import ch.ethz.ssh2.ChannelCondition;
7import ch.ethz.ssh2.Connection;
8import ch.ethz.ssh2.Session;
9import ch.ethz.ssh2.StreamGobbler;
10
11public class RemoteShellExecutor {
12
13 private Connection conn;
14 /** 远程机器IP */
15 private String ip;
16 /** 用户名 */
17 private String osUsername;
18 /** 密码 */
19 private String password;
20 private String charset = Charset.defaultCharset().toString();
21
22 private static final int TIME_OUT = 1000 * 5 * 60;
23
24 public RemoteShellExecutor(String ip, String usr, String pasword) {
25 this.ip = ip;
26 this.osUsername = usr;
27 this.password = pasword;
28 }
29
30 /**
31 * 登录
32 */
33 private boolean login() throws IOException {
34 conn = new Connection(ip);
35 conn.connect();
36 return conn.authenticateWithPassword(osUsername, password);
37 }
38
39 /**
40 * 执行脚本
41 *
42 * @param cmds
43 * @return
44 * @throws Exception
45 */
46 public int exec(String cmds) throws Exception {
47 InputStream stdOut = null;
48 InputStream stdErr = null;
49 String outStr = "";
50 String outErr = "";
51 int ret = -1;
52 try {
53 if (login()) {
54 // Open a new {@link Session} on this connection
55 Session session = conn.openSession();
56 // Execute a command on the remote machine.
57 session.execCommand(cmds);
58
59 stdOut = new StreamGobbler(session.getStdout());
60 outStr = processStream(stdOut, charset);
61
62 stdErr = new StreamGobbler(session.getStderr());
63 outErr = processStream(stdErr, charset);
64
65 session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
66
67 System.out.println("正确输出:" + outStr);
68 if(!"".equals(outErr)) {
69 System.out.println("错误输出:" + outErr);
70 }
71 ret = session.getExitStatus();
72 } else {
73 throw new Exception("登录远程机器失败" + ip); // 自定义异常类 实现略
74 }
75 } finally {
76 if (conn != null) {
77 conn.close();
78 }
79 stdOut.close();
80 stdErr.close();
81 }
82 return ret;
83 }
84
85 /**
86 * @param in
87 * @param charset
88 * @return
89 * @throws IOException
90 * @throws UnsupportedEncodingException
91 */
92 private String processStream(InputStream in, String charset) throws Exception {
93 byte[] buf = new byte[1024];
94 StringBuilder sb = new StringBuilder();
95 while (in.read(buf) != -1) {
96 sb.append(new String(buf, charset));
97 }
98 return sb.toString();
99 }
100
101 public static void main(String args[]) throws Exception {
102 RemoteShellExecutor executor = new RemoteShellExecutor("10.10.15.208", "root", "123456");
103 executor.exec("export LANG=en_US && ls -lh /root");
104 }
105}
106