gpt4 book ai didi

java - 如何使用ssh命令在命令中发送密码

转载 作者:太空狗 更新时间:2023-10-29 14:18:34 24 4
gpt4 key购买 nike

我正在使用 Jsch 库连接我的服务器。连接后我传递的命令需要密码才能继续,因此我只在命令中传递我的密码但没有任何反应。

代码:

JSch jsch = new JSch();
jsch.removeAllIdentity();
Session session = jsch.getSession(user, host, port);
session.setPassword(password);
.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PubkeyAuthentication", "no");
System.out.println("Establishing Connection...");
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");`
Channel shellChannel = session.openChannel("shell");
shellChannel.connect();
((ChannelShell) shellChannel).setPty(true);
shellChannel.setInputStream(System.in);
shellChannel.setOutputStream(System.out);
PrintStream shellStream = new PrintStream(
shellChannel.getOutputStream());

shellChannel.connect();
shellStream
.println("cd /usr/local/apache2/; ls; cd ../www; ls; git fetch origin; <mypasssword>");
shellStream.flush();
System.out.println("SFTP Channel created.");`

当我运行这段代码时,git 询问密码以继续下一步。注意:我无法禁用 git fetch origin 的密码。

最佳答案

我试过你的代码来访问我的 linux box - 它确实登录成功,但随后无法发送任何命令。我不确定这是否是您遇到的问题 - 但我会在此处添加我的解决方案,以防万一。

shellStream.println(); 命令移动到它自己的函数中:

public static void sendCommand(String c) {
shellStream.print(c + "\n");
shellStream.flush();
}

必须在进程中创建 shellChannelshellStream 全局变量。

shellStream.println(); 更改为 shellStream.print("\n");,因为上述方法无法正常工作。

在这行代码之后:

shellStream = new PrintStream(shellChannel.getOutputStream());

添加了我的命令序列:

Thread.sleep(1000); // wait for it to connect    
sendCommand("sudo su"); // the command I tried
Thread.sleep(1000); // not sure how long you need to wait
sendCommand("mypassword");
Thread.sleep(1000);
// etc.

顺便说一句,您在代码中调用了两次 shellChannel.connect(); - 我删除了最后一个。

这是您代码的最终工作版本:

import java.io.IOException;
import java.io.PrintStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelShell;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

public class MyShell {

static String user = "daniel";
static String host = "localhost";
static int port = 22;
static String password = "mypass";
static Session session;
static Channel shellChannel;
static PrintStream shellStream;

public static void main(String[] args) throws JSchException, IOException,
InterruptedException {

JSch jsch = new JSch();
jsch.removeAllIdentity();
session = jsch.getSession(user, host, port);
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("PubkeyAuthentication", "no");
System.out.println("Establishing Connection...");
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.connect();
System.out.println("Connection established.");
System.out.println("Crating SFTP Channel.");

shellChannel = session.openChannel("shell");
shellChannel.connect();
((ChannelShell) shellChannel).setPty(true);
shellChannel.setInputStream(System.in);
shellChannel.setOutputStream(System.out);
shellStream = new PrintStream(shellChannel.getOutputStream());

Thread.sleep(1000);
sendCommand("sudo su");
Thread.sleep(1000);
sendCommand("mypass");
Thread.sleep(1000);
sendCommand("ls");

}

public static void sendCommand(String c) {
shellStream.print(c + "\n");
shellStream.flush();
}
}

关于java - 如何使用ssh命令在命令中发送密码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31555048/

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