gpt4 book ai didi

java - 如何使用 Java 从一台机器到另一台机器进行 ssh

转载 作者:太空宇宙 更新时间:2023-11-04 15:02:08 24 4
gpt4 key购买 nike

使用putty,我可以从一台机器到另一台机器,然后从另一台机器进行ssh。就像一个链过程。我想在我的程序中复制相同的内容。到目前为止,我能够ssh到第一台机器。从那台机器我如何 ssh 到后续机器。我还想执行一些命令,如“pwd”或“ls”。

import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import java.io.*;

public class SSH {

public static void main(String args[]) {

String user = "******";
String password = "******";
String host = "******";
int port=22;

String remoteFile="something.txt";

try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelSftp sftpChannel = (ChannelSftp) session.openChannel("sftp");
sftpChannel.connect();
System.out.println("connected to first server....");
InputStream out= null;
out= sftpChannel.get(remoteFile);
BufferedReader br = new BufferedReader(new InputStreamReader(out));
String line;
while ((line = br.readLine()) != null)
System.out.println(line);
br.close();

/********* Here i want to ssh to another machine from the already connected one ******/

String command = "pwd" ; //executing correctly with o/p
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
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));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel.getExitStatus());
break;
}

}
channel.disconnect();

Channel channel1 = session.openChannel("exec");
((ChannelExec) channel1).setCommand("ssh username@hostname" + "&&" + "password");//not executing
channel1.setInputStream(null);
in = channel1.getInputStream();
channel1.connect();
channel1.disconnect();



Channel channel2 = session.openChannel("exec");

((ChannelExec) channel2).setCommand("ls"); //to verify if ssh to 2nd machine has happened.not working
channel2.setInputStream(null);
in = channel2.getInputStream();
channel2.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));
}
if(channel.isClosed()){
System.out.println("exit-status: "+channel2.getExitStatus());
break;
}

}
channel2.disconnect();
session.disconnect();
} catch(Exception e) { System.err.print(e); }
}

}

最佳答案

在session.connect()之后;使用代码在您的 Linux 服务器中执行命令

                            session.connect();
command = "pwd;" + "ssh username@hostname" + "&&" + "password" ; // u can give your own commands

Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.setInputStream(null);
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) {

output = new String(tmp, 0, i); // name a variable of your own

} else {

break;

}

System.out.println(output);

}}
channel.disconnect();
session.disconnect();

确保断开 channel 。这样您就可以创建一个新 channel 并执行下一个命令。

关于java - 如何使用 Java 从一台机器到另一台机器进行 ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22453683/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com