如何使用 Jsch 在 2 个远程 unix 机器之间执行 scp?
我正在使用 Jsch 编写 Java 代码,其中我需要在 2 个远程 UNIX 机器之间执行 scp。
我尝试以与从 java 代码执行普通命令相同的方式执行 scp 命令,如下所示:
JSch jsch=new JSch();
Session session=jsch.getSession("user", "host", 22);
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.setPassword("pwd");
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("scp [email protected]:/home/user/demo.csv /home/user/demo.csv");
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
//Printing operations
channel.disconnect();
session.disconnect();
而不是提示 [电子邮件受保护] 正如我所料。我只得到错误:
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
exit-status: 1
通过直接在 UNIX 机器上执行命令,我发现:
scp 要求输入密码才能连接到远程计算机。但是使用此代码,它不会提示输入任何密码。所以它试图在没有密码的情况下执行 scp 并尝试三次(NumberoOFAttempts 标志)并给出错误消息...
我想知道如何使 scp 至少提示输入某些内容而不是仅仅不输入密码...我已经使用过我的代码的 jsch 示例中给出的 MyUserInfo 类的默认实现...
或者
有什么方法可以提供密码以及 scp 命令?我试图尽可能避免使用私钥/公钥组合。
编辑: 从保罗的帖子..现在我添加了channel.setPty(true)来获取命令的伪终端,它要求我输入密码而不是自动失败..
我现在应该如何指定密码...我尝试输入它在控制台中,但它不接受并继续等待用户输入......我尝试使用 channel.setInputStream(pwdbytes); 设置它但它一直等待命令而不接受输入..
感谢有关此问题的任何帮助...
I am writing a Java code using Jsch in which I need to perform scp between 2 remote UNIX boxes.
I tried to execute the scp command in the same way as to execute a normal command from my java code like this :
JSch jsch=new JSch();
Session session=jsch.getSession("user", "host", 22);
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.setPassword("pwd");
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand("scp [email protected]:/home/user/demo.csv /home/user/demo.csv");
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
//Printing operations
channel.disconnect();
session.disconnect();
Instead of prompting password for [email protected] as I expected. I get only errors :
Permission denied, please try again.
Permission denied, please try again.
Permission denied (publickey,gssapi-with-mic,password).
exit-status: 1
From executing the command on the UNIX box directly, I have found this :
scp asks for password to connect to the remote machine.. however using this code, it is not prompting for any password. So it is trying to perform scp without the password and trying thrice(NumberoOFAttempts flag) and gives the error messages...
I want to know how to make scp atleast prompt to enter something instead of just not taking password ... I have used the default implementation of MyUserInfo class given in jsch examples for my code...
Or
Is there any way to provide the password along with the scp command ?? I am trying to avoid using private/public key combinations as much as possible..
EDIT :
From Paulo's post.. Now I have added channel.setPty(true) to get a pseudo terminal for the command and it is asking for me to enter Password than failing automatically..
How should I specify the password now... I tried typing it in the console but it is not accepting and keeps on waiting for user input.... I tried setting it using channel.setInputStream(pwdbytes); but it keeps waiting at the command and not taking the input ..
Thanks for any help regarding this...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个 Unix 机器上的
scp
命令尝试从控制台/终端读取密码。但这仅在存在与进程关联的终端时才有效 - 默认情况下并非如此。解决此问题的最简单方法是使用
channel.setPty(true)
在连接通道之前,然后通过输入/输出流提供密码。The
scp
command on your first Unix box tries to read the password from the console/terminal. But this will only work when there is a terminal associated with the process - and it is not, by default.The easiest way to solve this would be to use
channel.setPty(true)
before connecting the channel, and then provide the password via the input/output streams.