Command using ChannelExec not executed - Jsch - java

I am using Jsch to create a file in the server and execute some commands. For the file creation, it's working fine, however, for the command execution, doesn't. It keeps the status -1 (still working on it) and keep there forever. This happen for shell execution or when I try to became root. Follow the method used below:
public void upload(String localPath) throws IOException {
Session session = connectToServer();
System.out.println("In upload");
ChannelSftp channelSftp = getChannelToSftpServer(session);
//Creating file in temporary location
File f = new File(localPath);
FileInputStream fi = new FileInputStream(f);
// Creating file on server and setting the permissions to the user (chmod 777)
if (channelSftp != null) {
try {
System.out.println("Change working in temp directory");
changeWorkingDirectory(channelSftp, TEMP_PATH);
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
//THE PROBLEM ALSO HAPPENS WHEN EXECUTING A SHELL WITH THIS COMMAND INSIDE
channelExec.setCommand(
"root command (using pbrun) <command is here, confidential> ");
InputStream commandOutput = channelExec.getInputStream();
channelExec.connect();
StringBuilder outputBuffer = new StringBuilder();
int readByte = commandOutput.read();
while(readByte != 0xffffffff)
{
outputBuffer.append((char)readByte);
readByte = commandOutput.read();
System.out.println(outputBuffer);
}
System.out.println("Root connected.");
channelExec.disconnect();
channelSftp.put(fi, f.getName());
channelSftp.chmod(0777, localPath);
channelSftp.chown(123, localPath);
channelSftp.chgrp(123, localPath);
System.out.println("File configurations changed.");
//Copying to the official path
channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("mv /tmp/"+f.getName()+" "+path);
channelExec.connect();
System.out.println("File is completed and ready!");
while (channelExec.getExitStatus() == -1) {
Thread.sleep(1000);
}
channelExec.disconnect();
} catch (SftpException e) {
e.printStackTrace();
throw new IOException(e.getStackTrace() + "");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
disconnectChanneltoSftpServer(channelSftp);
session.disconnect();
fi.close();
// Deletes the local File.
f.delete();
}
}
}
What am I doing wrong? Thank you in advance.

You have to call getInputStream() before calling connect().
And you actually better read both stderr and stdout to get the errors.
For that, see my answer to How to read JSch command output?

Related

Unzipping of file is not working from ChannelExec- Java [duplicate]

I am using Jsch to create a file in the server and execute some commands. For the file creation, it's working fine, however, for the command execution, doesn't. It keeps the status -1 (still working on it) and keep there forever. This happen for shell execution or when I try to became root. Follow the method used below:
public void upload(String localPath) throws IOException {
Session session = connectToServer();
System.out.println("In upload");
ChannelSftp channelSftp = getChannelToSftpServer(session);
//Creating file in temporary location
File f = new File(localPath);
FileInputStream fi = new FileInputStream(f);
// Creating file on server and setting the permissions to the user (chmod 777)
if (channelSftp != null) {
try {
System.out.println("Change working in temp directory");
changeWorkingDirectory(channelSftp, TEMP_PATH);
ChannelExec channelExec = (ChannelExec) session.openChannel("exec");
//THE PROBLEM ALSO HAPPENS WHEN EXECUTING A SHELL WITH THIS COMMAND INSIDE
channelExec.setCommand(
"root command (using pbrun) <command is here, confidential> ");
InputStream commandOutput = channelExec.getInputStream();
channelExec.connect();
StringBuilder outputBuffer = new StringBuilder();
int readByte = commandOutput.read();
while(readByte != 0xffffffff)
{
outputBuffer.append((char)readByte);
readByte = commandOutput.read();
System.out.println(outputBuffer);
}
System.out.println("Root connected.");
channelExec.disconnect();
channelSftp.put(fi, f.getName());
channelSftp.chmod(0777, localPath);
channelSftp.chown(123, localPath);
channelSftp.chgrp(123, localPath);
System.out.println("File configurations changed.");
//Copying to the official path
channelExec = (ChannelExec) session.openChannel("exec");
channelExec.setCommand("mv /tmp/"+f.getName()+" "+path);
channelExec.connect();
System.out.println("File is completed and ready!");
while (channelExec.getExitStatus() == -1) {
Thread.sleep(1000);
}
channelExec.disconnect();
} catch (SftpException e) {
e.printStackTrace();
throw new IOException(e.getStackTrace() + "");
} catch (JSchException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
disconnectChanneltoSftpServer(channelSftp);
session.disconnect();
fi.close();
// Deletes the local File.
f.delete();
}
}
}
What am I doing wrong? Thank you in advance.
You have to call getInputStream() before calling connect().
And you actually better read both stderr and stdout to get the errors.
For that, see my answer to How to read JSch command output?

Execute multiple ssh commands and get result for each command

I have opened session with jsch on Android, this way:
SshObjects Connect(String username, String password, String hostname, int port)
{
JSch jsch=new JSch();
try
{
sshObjects._session = jsch.getSession(username, hostname, port);
}
catch (JSchException e)
{
e.printStackTrace();
return null;
}
sshObjects._session.setPassword(password);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
sshObjects._session.setConfig(config);
try
{
sshObjects._session.connect();
}
catch (JSchException e)
{
e.printStackTrace();
return null;
}
try
{
sshObjects._channel = (ChannelExec) sshObjects._session.openChannel("exec");
}
catch (JSchException e)
{
e.printStackTrace();
return null;
}
connected = true;
return sshObjects;
}
And then, to execute some command on opened session and get result, I did this:
private String ExecuteCommand(SshCommandsEnum cmdType)
{
String result = "";
switch (cmdType)
{
case SERVER_INFO:
sshObjects._channel.setCommand("uname --all");
break;
.......
}
try
{
BufferedReader in=new BufferedReader(new InputStreamReader(sshObjects._channel.getInputStream()));
//sshObjects._channel.disconnect();
try
{
sshObjects._channel.connect();
}
catch (JSchException e)
{
e.printStackTrace();
}
String msg=null;
try
{
while((msg=in.readLine())!=null)
{
System.out.println(msg);
result += msg;
}
sshObjects._channel.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
catch (IOException e)
{
return "";
}
return result;
}
So I want to open my session only once. And then execute commands as "exec" on it. It works for first command executed after connect - everything seems to be ok and I can get result succesfully. But when I call "Execute Command" again, it doesn't work anymore. My thread hangs on sshObjects._channel.connect(); and nothing works. When I try to disconnect (close channel and session) and connect again - the same. I can connect and disconnect without any problems only if I don't even try to execute command.
However, I don't experience this issue without this:
BufferedReader in=new BufferedReader(new InputStreamReader(sshObjects._channel.getInputStream()));
But, obviously I need it to get my command output. So what's the problem? Do you have any idea what am I doing wrong?
The ChannelExec is not re-usable, so you need to instantiate it for each command.

Changing ftp upload location

So I'm uploading a file to my VPS (Linux Centos 5 64 bit) via FTP using Java. The code I'm using to upload to my VPS is
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
Now the code is working but what I want is to change where on the VPS it uploads the file to. Right now it's
serverip/HERE
I have some files so want to change it to
serverip/file/HERE
How can I go about doing so?
You can use the changeCurrentWorkingDirectory() method to change to the desired directory. Once you're in there, you can write the file using storeFile just like before.
changeCurrentWorkingDirectory returns true if the directory change was successful, otherwise it returns false. It takes a string which interpreted as the directory path. If the path starts with a slash, it's interpreted as absolute path starting at the ftproot directory. Otherwise it's interpreted as relative path.
Revised code could look something like this:
FTPClient client = new FTPClient();
FileInputStream fis = null;
try
{
client.connect(serverip);
client.login("user, pass);
client.setFileType(FTPClient.BINARY_FILE_TYPE);
// change directory to serverip/file/
if (client.changeWorkingDirectory("/file"))
{
// Create an InputStream of the file to be uploaded
String filename = Shared.saveLocation + Shared.saveAs;
fis = new FileInputStream(filename);
// Store file to server
client.storeFile(Shared.saveAs, fis);
}
client.logout();
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (fis != null)
{
fis.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}

apache.commons.net.ftp.FTPClient not uploading the file to the required folder

I'm using the following code to upload an xml file to the directory /home/domainname/public_html/guest in the server. However, the file is uploaded only to the location /home/domainname. It is not uploading to the child directories. Please advise.
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(Util.getProductsXMLFTPServer());
client.login(Util.getProductsXMLFTPUser(), Util.getProductsXMLFTPPassword());
//
// Create an InputStream of the file to be uploaded
//
fis = new FileInputStream(new File(Util.getProductsXMLFTPInputFilePath(), Util.getProductsXMLFTPOutputFileName()));
client.changeWorkingDirectory(Util.getProductsXMLFTPUploadPath());
client.storeFile(Util.getProductsXMLFTPOutputFileName(), fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
I checked your code, it works. I've only changed file type declaration to binary, which may be not needed for XML files.
Here's my complete code for reference:
package apachenet.ftp;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class App {
public static void main( String[] args ) {
FTPClient client = new FTPClient();
FileInputStream fis = null;
try {
client.connect(/*Util.getProductsXMLFTPServer()*/"127.0.0.1");
client.login(/*Util.getProductsXMLFTPUser()*/"pwyrwinski",
/*Util.getProductsXMLFTPPassword()*/"secret");
client.setFileType(FTP.BINARY_FILE_TYPE); // optional
fis = new FileInputStream(
new File(/* Util.getProductsXMLFTPInputFilePath() */"/home/pwyrwinski",
/* Util.getProductsXMLFTPOutputFileName() */"img.png"));
client.changeWorkingDirectory(/*Util.getProductsXMLFTPUploadPath()*/ "someDir");
client.storeFile(/*Util.getProductsXMLFTPOutputFileName()*/"img_bis.png", fis);
client.logout();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
client.disconnect();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
As you can see it's roughly the same as yours.
Util class calls are replaced with raw data.
When I ran it, file /home/pwyrwinski/img.png was uploaded to {FTP_USER_ROOT}/someDir directory on ftp server with name changed to img_bis.png. I assume this is exactly what you wanted to achieve.
Let's go back to your problem.
Try to check what is returned from
Util.getProductsXMLFTPUploadPath() call. My guess is it's not what
you're expecting - so debug it in your IDE or print it to the console.
Check if path returned from Util.getProductsXMLFTPUploadPath()
call starts with slash, it shouldn't.
UPDATE 1.
Does direcory /home/domainname/public_html/guest exist on server?
Add following method to your class:
private static void showServerReply(FTPClient ftpClient) {
String[] replies = ftpClient.getReplyStrings();
if (replies != null && replies.length > 0) {
for (String aReply : replies) {
System.out.println("SERVER: " + aReply);
}
}
}
and call it after every ftp-client's method call. This will give you codes and descriptions of every command result. I suspect client.changeWorkingDirectory(...) ends with error, probably: 550 Permission Denied (or No such file or folder).
Next modification will be:
client.login(Util.getProductsXMLFTPUser(), Util.getProductsXMLFTPPassword());
System.out.println(client.printWorkingDirectory()); // added this line!
this will tell us what is current working directory after login in.
Please post your results.
FTPClient ftpClient = new FTPClient();
try {
System.out.println("before server connection");
ftpClient.connect(server, port);
System.out.println("before user name and passwod");
ftpClient.login(user, pass);
ftpClient.enterLocalActiveMode();
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println("connection sucess");
// windows working fine
File secondLocalFile = new File("/home/aims/archived_reports/tes_S_000000123/test.pdf");
// String secondRemoteFile = "/archived_reports/PermanentRecord.pdf";
//linux
// File secondLocalFile = new File("/archived_reports/tes_S_000009123/test.pdf");
String secondRemoteFile = "remotefilename.pdf";
InputStream inputStream = new FileInputStream(secondLocalFile);
System.out.println("Start uploading second file");
ftpClient.changeWorkingDirectory("/reports");// home/ftp.test/reports folder
System.out.println("Prasent Working Directory :"+ftpClient.printWorkingDirectory());
OutputStream outputStream = ftpClient.storeFileStream(secondRemoteFile);
int returnCode = ftpClient.getReplyCode();
System.out.println(returnCode);
byte[] bytesIn = new byte[4096];
int read = 1;
while ((read = inputStream.read(bytesIn)) != -1) {
outputStream.write(bytesIn, 0, read);
}
System.out.println();
inputStream.close();
outputStream.close();
boolean completed = ftpClient.completePendingCommand();
if (completed) {
System.out.println("The second file is uploaded successfully.");
}

Scp file with jsch gives 'unexpected filename'

I'm using Jsch 0.1.44 to scp a file from one host to another. The relevant code is the following:
public boolean transferFileToHost(File fileToTransfer, String destDirectory, String destFilename) {
Channel channel = null;
try {
String command = "scp -t "+ destDirectory + destFilename;
channel = session.openChannel("exec");
((ChannelExec)channel).setCommand(command);
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
if(!connectToChannel(channel, in)) {
return false;
}
if(!sendScpCommand(fileToTransfer, command, out, in)) {
return false;
}
if(!sendFileContent(out, fileToTransfer, in)) {
return false;
}
return true;
} catch (IOException e) {
logger.error("Error while reading file. Error was: ",e);
} catch (JSchException e) {
logger.error("Error while sending ssh commands. Error was: ",e);
}
finally {
if(channel != null) {
channel.disconnect();
}
}
private boolean sendScpCommand(File file, String command, OutputStream out, InputStream in) throws IOException {
long filesize=file.length();
command="C0644 "+filesize+" ";
command+=file;
command+="\n";
out.write(command.getBytes());
out.flush();
if (checkAck(in) != 0) {
return false;
}
return true;
}
The command in this line
((ChannelExec)channel).setCommand(command);
looks like this: scp -t /tmp/config.xml and the command in this line
out.write(command.getBytes());
looks like this: C0644 5878 /home/myuser/config.xml
The problem is, that I get the following error from scp: scp: error: unexpected filename: /path/to/config.xml
What is the reason for this error? How can I avoid it?
Any help is highly appreciated.
I've found a solution. It seems that the source filename in the command must not contain any slashes. To solve this problem you simple have to change this line:
command+=file;
into this:
command+=file.getName();
Thats it.

Categories