我正在尝试编写一个Java代码,它可以ssh到Unix服务器并重置用户密码。所以我尝试实现SO中的一些代码。
如。
使用JSchChannelExec
,我按照此链接获取重置用户密码的正确命令。
当我试着运行这段代码时,它似乎没有重置用户的密码。因此,我尝试直接从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);
}
}
我到底错过了什么?会话和通道似乎处于活动状态,但它不执行命令。这是代码的日志:
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
我假设问题是您在命令启动之前就关闭了连接。
指向的所有示例读取命令输出,直到结束(因此直到命令完成)。您没有读取输出,因此无法等待命令完成。
或者像其他示例一样使用命令输出。或者(如果您对输出不感兴趣)等到频道。isClosed()
在退出前为true。
我正在从事一个项目,其中我打算制作一个JavaGUI应用程序,该应用程序连接到ssh服务器并在服务器上执行远程命令。我愿意使用JSch库。我的目标是制作按钮和文本字段,让用户能够轻松发送命令和获得回复。我的意思是,用户不会打开xShell并提示“grep”hi“/usr/file.txt”,而是从列表中选择路径,在文本字段中输入“hi”,然后按下grep按钮。 问题是,我找不到一个在一个会话中执行
为什么/我的目标: 我有一个由pi组成的小型网状网络,每天大部分时间都在运行脚本。我想取消停机时间,但是代码有时会在连续循环3-4天后停止工作,(有时长达一周,代码才会出现错误并停止)。 每个节点上运行的脚本用“最后签入”字段更新mySQL数据库。 我希望用Java编写一个小型后台程序,它将在我的服务器上无限期运行,时不时地检查每个站点的“最后签入”,如果它注意到一个节点宕机,远程ssh进入该节点
我正在寻找一个解决方案,采取SSH连接,然后是Telnet连接到远程系统通过Java。由于只有使用telnet连接,我才能在远程计算机上执行该特定命令。 在大量浏览之后,我找到了这个答案“https://stackoverflow.com/questions/27146991/running-telnet-command-on-remote-ssh-session-using-jsch”,但是在执
嗨,我正试图通过ssh隧道连接到服务器上的数据库,但我甚至无法连接Jsch会话。我试图在GWT中作为我的后端代码的一部分来做这件事。这是服务器端代码的实现部分。我对服务器一般不是那么熟悉。通常在端子I类型中: ssh-x username@xxxx.xxx.edu 然后系统提示我输入密码,我输入密码,然后我就进入了。 所以在java中我的代码如下: 我已经双重检查了用户名、密码和主机字符串,它们都
问题内容: 我正在SSH shell会话上运行命令,为了获得该命令,我在官方示例后面使用了JSch 。 我也在 StackOverflow* 上也遵循此示例编写了自己的代码 * 这是 我的代码: 看起来不错,但奇怪的是 它只能在调试模式下工作 。如果我运行它,程序将无法执行。我认为它锁定 了while循环, 但是我不明白为什么。没有while循环,代码将到达终点,但是不执行任何shell命令。 我
我使用Netbeans作为IDE 你能帮我找出问题吗?!