gpt4 book ai didi

java - 我的 JSch session 不执行命令

转载 作者:行者123 更新时间:2023-11-30 03:23:34 25 4
gpt4 key购买 nike

我正在尝试编写一个Java代码,可以通过ssh连接到Unix服务器并重置用户的密码。所以我尝试实现一些在SO中找到的代码。

例如。

使用 JSch ChannelExec,我关注了 this link获取重置用户密码的正确命令。

当我尝试运行此代码时,似乎它没有重置用户的密码。所以我尝试直接从 Unix 的 shell 运行该命令,该命令运行得很好。我认为执行人员根本没有工作。我更改了 exec 来运行一个简单的命令,例如 mkdir/home/fikrie/testingjsch ,然后证明了我的假设,看到该目录没有创建。

这是我的代码:

public void executeSetPassword(final String userName, final GuardedString password)  {
JSch jsch = new JSch();
String host = configuration.getHost();
String remoteUser = configuration.getRemoteUser();
GuardedString passwd = configuration.getPassword();
String command = "echo " + userName + ":" + password + " | chpasswd";
Boolean sessionStatus, channelStatus;

logger.info("userName is " + userName);
logger.info("password is " + password);
logger.info("command is " + command);

final Session session;
try {
session = jsch.getSession(remoteUser, host, 22);

passwd.access(new Accessor(){
@Override
public void access(char[] clearChars) {
session.setPassword(new String(clearChars));
}});
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();

sessionStatus = session.isConnected();
logger.info("sessionStatus is " + sessionStatus);
ChannelExec channel=(ChannelExec) session.openChannel("exec");
channel.setCommand(command);

channel.connect();
channelStatus = channel.isConnected();

logger.info("channelStatus is " + channelStatus);
logger.info("Exit status = " + channel.getExitStatus());

channel.disconnect();
session.disconnect();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

我实际上错过了什么? session 和 channel 似乎处于 Activity 状态,但它不执行命令。这是代码的日志:

Method: executeSetPassword(UnixConnector.java:230)  Level: INFO Message: userName is anne
Method: executeSetPassword(UnixConnector.java:231) Level: INFO Message: password is org.identityconnectors.common.security.GuardedString@e492109
Method: executeSetPassword(UnixConnector.java:232) Level: INFO Message: command is echo anne:org.identityconnectors.common.security.GuardedString@e492109 | chpasswd
Method: executeSetPassword(UnixConnector.java:249) Level: INFO Message: sessionStatus is true
Method: executeSetPassword(UnixConnector.java:257) Level: INFO Message: channelStatus is true
Method: executeSetPassword(UnixConnector.java:258) Level: INFO Message: Exit status = -1

这是 JSch 日志:

INFO: Connecting to 192.168.1.62 port 22
INFO: Connection established
INFO: Remote version string: SSH-2.0-OpenSSH_5.3
INFO: Local version string: SSH-2.0-JSCH-0.1.53
INFO: CheckCiphers: aes256-ctr,aes192-ctr,aes128-ctr,aes256-cbc,aes192-cbc,aes128-cbc,3des-ctr,arcfour,arcfour128,arcfour256
INFO: aes256-ctr is not available.
INFO: aes192-ctr is not available.
INFO: aes256-cbc is not available.
INFO: aes192-cbc is not available.
INFO: CheckKexes: diffie-hellman-group14-sha1,ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521
INFO: CheckSignatures: ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: SSH_MSG_KEXINIT sent
INFO: SSH_MSG_KEXINIT received
INFO: kex: server: diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group14-sha1,diffie-hellman-group1-sha1
INFO: kex: server: ssh-rsa,ssh-dss
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: aes128-ctr,aes192-ctr,aes256-ctr,arcfour256,arcfour128,aes128-cbc,3des-cbc,blowfish-cbc,cast128-cbc,aes192-cbc,aes256-cbc,arcfour,rijndael-cbc@lysator.liu.se
INFO: kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: hmac-md5,hmac-sha1,umac-64@openssh.com,hmac-ripemd160,hmac-ripemd160@openssh.com,hmac-sha1-96,hmac-md5-96
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server: none,zlib@openssh.com
INFO: kex: server:
INFO: kex: server:
INFO: kex: client: ecdh-sha2-nistp256,ecdh-sha2-nistp384,ecdh-sha2-nistp521,diffie-hellman-group14-sha1,diffie-hellman-group-exchange-sha256,diffie-hellman-group-exchange-sha1,diffie-hellman-group1-sha1
INFO: kex: client: ssh-rsa,ssh-dss,ecdsa-sha2-nistp256,ecdsa-sha2-nistp384,ecdsa-sha2-nistp521
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO: kex: client: aes128-ctr,aes128-cbc,3des-ctr,3des-cbc,blowfish-cbc
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: hmac-md5,hmac-sha1,hmac-sha2-256,hmac-sha1-96,hmac-md5-96
INFO: kex: client: none
INFO: kex: client: none
INFO: kex: client:
INFO: kex: client:
INFO: kex: server->client aes128-ctr hmac-md5 none
INFO: kex: client->server aes128-ctr hmac-md5 none
INFO: SSH_MSG_KEXDH_INIT sent
INFO: expecting SSH_MSG_KEXDH_REPLY
INFO: ssh_rsa_verify: signature true
WARN: Permanently added '192.168.1.62' (RSA) to the list of known hosts.
INFO: SSH_MSG_NEWKEYS sent
INFO: SSH_MSG_NEWKEYS received
INFO: SSH_MSG_SERVICE_REQUEST sent
INFO: SSH_MSG_SERVICE_ACCEPT received
INFO: Authentications that can continue: gssapi-with-mic,publickey,keyboard-interactive,password
INFO: Next authentication method: gssapi-with-mic
INFO: Authentications that can continue: publickey,keyboard-interactive,password
INFO: Next authentication method: publickey
INFO: Authentications that can continue: password
INFO: Next authentication method: password
INFO: Authentication succeeded (password).
INFO: Disconnecting from 192.168.1.62 port 22
INFO: Caught an exception, leaving main loop due to Socket closed

最佳答案

我认为问题是您在命令启动之前关闭了连接。

您指出的所有示例都读取命令输出,直到结束(因此直到命令完成)。您没有读取输出,因此无法等待命令完成。

要么像其他示例一样使用命令输出。或者(如果您对输出不感兴趣)请等到 channel.isClosed() 为 true 后再退出。

关于java - 我的 JSch session 不执行命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30774046/

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