软件包下载地址:http://www.ganymed.ethz.ch/ssh2/
在线文档:http://www.boyunjian.com/javadoc/org.jvnet.hudson/ganymed-ssh2/build210-hudson-1/_/ch/ethz/ssh2/Session.html
package com.pasier.xxx.util;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.nio.charset.Charset;
6
7 import org.slf4j.Logger;
8 import org.slf4j.LoggerFactory;
9
10 import ch.ethz.ssh2.ChannelCondition;
11 import ch.ethz.ssh2.Connection;
12 import ch.ethz.ssh2.Session;
13 import ch.ethz.ssh2.StreamGobbler;
14
15 public class RmtShellExecutor {
16
17 private static final Logger LOG = LoggerFactory.getLogger(RmtShellExecutor.class);
18
19 private Connection conn;
20 private String ip;
21 private String usr;
22 private String psword;
23 private String charset = Charset.defaultCharset().toString();
24
25 private static final int TIME_OUT = 1000 * 5 * 60;
26
27 public RmtShellExecutor(String ip, String usr, String ps) {
28 this.ip = ip;
29 this.usr = usr;
30 this.psword = ps;
31 }
32
33 private boolean login() throws IOException {
34 conn = new Connection(ip);
35 conn.connect();
36 return conn.authenticateWithPassword(usr, psword);
37 }
38
39 public String exec(String cmds) throws IOException {
40 InputStream stdOut = null;
41 InputStream stdErr = null;
42 String outStr = "";
43 String outErr = "";
44 int ret = -1;
45
46 try {
47 if (login()) {
48 Session session = conn.openSession();
49 session.execCommand(cmds);
50 stdOut = new StreamGobbler(session.getStdout());
51 outStr = processStream(stdOut, charset);
52 LOG.info("caijl:[INFO] outStr=" + outStr);
53 stdErr = new StreamGobbler(session.getStderr());
54 outErr = processStream(stdErr, charset);
55 LOG.info("caijl:[INFO] outErr=" + outErr);
56 session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT);
57 ret = session.getExitStatus();
58
59 } else {
60 LOG.error("caijl:[INFO] ssh2 login failure:" + ip);
61 throw new IOException("SSH2_ERR");
62 }
63
64 } finally {
65 if (conn != null) {
66 conn.close();
67 }
68 if (stdOut != null)
69 stdOut.close();
70 if (stdErr != null)
71 stdErr.close();
72 }
73
74 return outStr;
75 }
76
77 private String processStream(InputStream in, String charset) throws IOException {
78 byte[] buf = new byte[1024];
79 StringBuilder sb = new StringBuilder();
80 while (in.read(buf) != -1) {
81 sb.append(new String(buf, charset));
82 }
83 return sb.toString();
84 }
85
86 public static void main(String[] args) {
87
88 String usr = "root";
89 String password = "12345";
90 String serverIP = "11.22.33.xx";
91 String shPath = "/root/ab.sh";
92
93 RmtShellExecutor exe = new RmtShellExecutor(serverIP, usr, password);
94
95 String outInf;
96
97 try {
98 outInf = exe.exec("sh " + shPath + " xn");
99 System.out.println("outInf= " + outInf);
100 } catch (IOException e) {
101 e.printStackTrace();
102 }
103 }
104
105 }
-------------------------------------------------------------------------------------------------------
import java.io.IOException;
import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class TestRemoteConnect {
public static void main(String[] args) {
String hostname = "192.168.100.50";
String username = "root";
String password = "highgo"; // 指明连接主机的IP地址
Connection conn = new Connection(hostname);
Session ssh = null;
try {
// 连接到主机
conn.connect();
// 使用用户名和密码校验
boolean isconn = conn.authenticateWithPassword(username, password);
if (!isconn) {
System.out.println("用户名称或者是密码不正确");
} else {
System.out.println("已经连接OK");
ssh = conn.openSession();
ssh.execCommand("sh /root/hello.sh");
// ssh.execCommand("perl /root/hello.pl");
// 只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,
// 多次使用则会出现异常
// 使用多个命令用分号隔开
// ssh.execCommand("cd /root; sh hello.sh");
// 将Terminal屏幕上的文字全部打印出来
InputStream is = new StreamGobbler(ssh.getStdout());
BufferedReader brs = new BufferedReader(new InputStreamReader(
is));
while (true) {
String line = brs.readLine();
if (line == null) {
break;
}
System.out.println(line);
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 连接的Session和Connection对象都需要关闭
ssh.close();
conn.close();
}
}
}
--------------------------------------------------------------------------------------------------------------
<pre name="code" class="html">
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
public class LinuxUploadFile {
/**
* Description: 向FTP服务器上传文件
*
* @Version1.0 Jul 27, 2008 4:31:09 PM by 崔红保([email protected])创建
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param path
* FTP服务器保存目录
* @param filename
* 上传到FTP服务器上的文件名
* @param input
* 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url, int port, String username,
String password, String path, String filename, InputStream input) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);// 连接FTP服务器
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
/**
* Description: 从FTP服务器下载文件
*
* @Version1.0 Jul 27, 2008 5:32:36 PM by 崔红保([email protected])创建
* @param url
* FTP服务器hostname
* @param port
* FTP服务器端口
* @param username
* FTP登录账号
* @param password
* FTP登录密码
* @param remotePath
* FTP服务器上的相对路径
* @param fileName
* 要下载的文件名
* @param localPath
* 下载后保存到本地的路径
* @return
*/
public static boolean downFile(String url, int port, String username,
String password, String remotePath, String fileName,
String localPath) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(url, port);
// 如果采用默认端口,可以使用ftp.connect(url)的方式直接连接FTP服务器
ftp.login(username, password);// 登录
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(remotePath);// 转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
File localFile = new File(localPath + "/" + ff.getName());
OutputStream is = new FileOutputStream(localFile);
ftp.retrieveFile(ff.getName(), is);
is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}
}
原网址:
http://blog.csdn.net/hbcui1984/article/details/2720204