【发布时间】:2014-09-26 12:34:02
【问题描述】:
我正在使用 java 自动化 ssh 会话,使用此代码我能够登录并在屏幕上获得输出,但是在此之后,在我们进入应用程序以在 ssh 会话中运行后,它不需要用户名和密码,因为它需要像 TAB 和 ENTER 和功能键等键盘交互,所以有没有办法发送键盘事件来代替命令,以便自动化成功?
public class jschputty {
public static void main(String args[])
{
String host="x.x.x.x";
String user="username";
String password="password";
String command1="ls -ltr\t";
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");
ChannelExec channel=(ChannelExec) session.openChannel("exec");
BufferedReader in=new BufferedReader(new InputStreamReader(channel.getInputStream()));
channel.setCommand(command);
channel.connect();
String msg=null;
while((msg=in.readLine())!=null){
System.out.println(msg);
}
channel.disconnect();
session.disconnect();
System.out.println("DONE");
}catch(Exception e){
e.printStackTrace();
}
}
}
【问题讨论】:
-
您正在做的是与 SFTP 服务器建立 SFTP 连接。就像您可以使用名为“putty”的客户端应用程序一样。您不会使用此代码以任何方式与 putty 交互。
-
@Gimby - 从技术上讲,是 ssh,而不是 sftp,但你说得对,腻子根本不涉及。
标签: java automation