Skip to content
Advertisement

My JSch session doesn’t execute command

I’m trying to write a Java code that can ssh into a Unix server and reset a user’s password. So I tried to implement some of the code found in SO.

Eg.

Using JSch ChannelExec, I followed this link to get the proper command for resetting user’s password.

When I tried running this code, seems like it doesn’t reset the user’s password. So I tried running the command directly from the Unix’s shell, and the command work perfectly. I assume that the exec didn’t work at all. I changed the exec to run a simple command like mkdir /home/fikrie/testingjsch which then prove my assumption, seeing that directory were not created.

This is my code:

JavaScript

What am I missing actually? The session and channel seems to be alive but it doesn’t execute the command. This is the log for the code:

JavaScript

This is the JSch Log:

JavaScript

Advertisement

Answer

I assume the problem is that you close the connection before the command is even started.

All the examples your pointed to read a command output, until the end (hence until the command finishes). You do not read the output so you fail to wait for the command to finish.

Either consume the command output as the other examples do. Or (if you are not interested in the output) wait until channel.isClosed() is true before exiting.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement