Reputation: 91
I am looking for a way to fire passwd onto a remote unix server via a java code. I am using Jsch, I can successfully execute commands like find, zip, get. But when I pass 'passwd' command nothing happens. The motive is to create an application which can change the password of unix server in every 10 days.
This is the code I am trying to use (find command works fine)
JSch sftp=new JSch();
Session session=sftp.getSession("Test", "150.236.9.75");
session.setConfig("StrictHostKeyChecking", "no");
session.setConfig("compression.s2c", "[email protected],zlib,none");
session.setConfig("compression.c2s", "[email protected],zlib,none");
session.setConfig("compression_level", "9");
session.setPassword("Test@123");
session.connect();
if(session.isConnected())
{
String line="";
System.out.println("connected");
ChannelExec ch=(ChannelExec) session.openChannel("exec");
String Command="passwd -s; Test@123; Pass@123; Pass@123";
//String Command="find *";
InputStream in = ch.getInputStream();
ch.setCommand(Command);
ch.connect();
byte[] buffer = new byte[1024];
int bytes;
do {
while (in.available() > 0) {
bytes = in.read(buffer, 0, 1024);
String file=new String(buffer, 0, bytes);
System.out.println(file);
line=line+file;
}
} while (!ch.isClosed());
ch.disconnect();
}
session.disconnect();
Upvotes: 0
Views: 1083
Reputation: 1
For me running the next command without using Thread.sleep works:
echo -e \"new-password\nnew-password\" | passwd user-linux
Upvotes: 0
Reputation: 2485
The passwd
command requires interative input (i.e. read from stdin), see How to write in Java to stdin of ssh?
Upvotes: 2