我试图建立一个类,这样我可以ssh到远程服务器(我有IP,用户名和密码),然后像“回声‘试验’,”送一个命令,然后接收返回的输出(例如, “测试”)。 我使用JSch要做到这一点,但我不知道如何做到这一点。
import com.jcraft.jsch.*;
public class ConnectSSH {
public int execute (String command) {
JSch jsch = new JSch();
String ip = "00.00.00.00;
String user = "root";
String pass = "password";
int port = 22;
try {
Session session = jsch.getSession(user, ip, port);
session.setPassword(pass);
session.connect();
...
我不知道该怎么办,我是连接后卡住。
任何意见是极大的赞赏。
试试这个:
JSch jsch=new JSch();
Session session=jsch.getSession(remoteHostUserName, RemoteHostName, 22);
session.setPassword(remoteHostpassword);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelExec channel=(ChannelExec) session.openChannel("exec");
BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.setCommand("pwd;");
channel.connect();
String msg=null;
while((msg=in.readLine())!=null){
System.out.println(msg);
}
channel.disconnect();
session.disconnect();
上述shamnu的回答是正确的。 我不能添加评论,所以这里有几个例子来加深对他的回答。 其一是如何做的“ls -l命令”一个远程执行,另一个“MKDIR”,另有一个本地的远程复制。 所有jsch的0.1.51版本做( http://www.jcraft.com/jsch/ )。
public void remoteLs() throws JSchException, IOException {
JSch js = new JSch();
Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
s.setPassword("mypassword");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();
Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("ls -l");
ce.setErrStream(System.err);
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
ce.disconnect();
s.disconnect();
System.out.println("Exit code: " + ce.getExitStatus());
}
public void remoteMkdir() throws JSchException, IOException {
JSch js = new JSch();
Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
s.setPassword("mypassword");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();
Channel c = s.openChannel("exec");
ChannelExec ce = (ChannelExec) c;
ce.setCommand("mkdir remotetestdir");
ce.setErrStream(System.err);
ce.connect();
BufferedReader reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
ce.disconnect();
s.disconnect();
System.out.println("Exit code: " + ce.getExitStatus());
}
public void remoteCopy() throws JSchException, IOException, SftpException {
JSch js = new JSch();
Session s = js.getSession("myusername", "myremotemachine.mycompany.com", 22);
s.setPassword("mypassword");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();
Channel c = s.openChannel("sftp");
ChannelSftp ce = (ChannelSftp) c;
ce.connect();
ce.put("/home/myuser/test.txt","test.txt");
ce.disconnect();
s.disconnect();
}
我建议看JCraft网站上隐藏的例子: http://www.jcraft.com/jsch/examples/UserAuthKI.java
他们的榜样提示输入用户名,主机名,密码,所以准备测试开箱。 我跑了它在我的网络上,并能够连接到AIX服务器,而无需更改任何代码。
请注意,它们的例子有一个问题(这可能是为什么它是隐藏的)..它不会永远关闭通道。 如果您发送“退出”的服务器将断开你,但通道对象保持开放和Java程序不退出。 我在这里提供的是一个修复: 使用jSch阅读服务器响应上房
1. List item
import java.io.InputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
public class SSHCommandExecutor {
/**
* @param args
*/
public static void main(String[] args) {
String host="10.75.81.21";
String user="root";
String password="Avaya_123";
String command1="ls -l";
try{
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
JSch jsch = new JSch();
Session session=jsch.getSession(user, host, 22);
session.setPassword(password);
session.setConfig(config);
session.connect();
System.out.println("Connected");
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(command1);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
System.out.print(new String(tmp, 0, i));
}`enter code here`
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
}
1. List item
}