我正在使用Jsch库来连接我的服务器。连接后,我传递命令,需要密码进一步进行,因此我只是在命令中传递我的密码,但没有任何反应。如何使用ssh命令在命令中发送密码
代码:
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原点的密码。
谢谢丹尼尔!有效。 :d –