Reputation: 1129
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...
Upvotes: 0
Views: 3424
Reputation: 74760
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.
Upvotes: 3