admin管理员组文章数量:1122846
The Linux server has the SSH setting which are the host key (Ed25519) and the cipher (ChaCha20; SSH-2 only) as highlighted in below snapshot. Manually we are using putty to connect the server. In automation framework, we are using Jsch Maven dependency. How can we add these settings in Java Selenium?
Below is my code,
package Demo;
import static org.testng.Assert.assertTrue;
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 JSchExampleSSHConnection {
public static String getPlayerTestData(String command1) {
String host =”HostIP”;
String user = "User";
String password = "Password";
StringBuilder strBuilder = new StringBuilder();
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));
strBuilder.append(new String(tmp, 0, i));
}
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();
}
return strBuilder.toString();
}
}
本文标签:
版权声明:本文标题:How to add host key (Ed25519) and the cipher (ChaCha20; SSH-2 only) settings of putty to Java-Selenium code using JSCH maven dep 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736281107a1926215.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论