Scp file with jsch gives 'unexpected filename' - java

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.

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?

Command using ChannelExec not executed - Jsch

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?

Unable to download files from FTP server

I've created Java function that downloads files from FTP server. It works fine from my local machine. But I need to run it under linux server (means another host and port). And the function gives an error
The collection, array, map, iterator, or enumeration portion of a for statement cannot be null
Caused in a line with the code:
for(String f : ftpNames) {
ftpclient.retrieveFile(f, os); // os is OutputStream
}
So it doesn't see the files...
I added
ftpclient.enterRemotePassiveMode();
And ftpclient.getPassiveHost() returns 227 Entering Passive Mode (x,x,x,x,204,15)
Tried to list and download them via shell - it works.
How should I modify my code to solve the problem? Thanks.
UPD. I got log from FTP server I'm trying to get files from, and there is such string:
425 Cannot open data connection
Full code:
static boolean ftpFilesDownload(String ip, int port, String login, String passwd, String ftpdir, String localdir) throws IOException {
Boolean result = false;
FTPClient client = new FTPClient();
String separator = File.separator;
try {
client.connect(ip, port);
System.out.println(client.getReplyString());
client.login(login, passwd);
System.out.println(client.getReplyString());
client.setControlKeepAliveTimeout(1000*60*5);
client.setControlKeepAliveReplyTimeout(1000*60*5);
client.setFileType(FTP.BINARY_FILE_TYPE);
System.out.println("client setFileType success");
client.changeWorkingDirectory(ftpdir);
System.out.println(client.getReplyString());
client.printWorkingDirectory();
System.out.println("directory changed");
FTPFile[] ftpFiles = client.listFiles();
System.out.println(ftpFiles);
String[] ftpNames = client.listNames();
System.out.println("the files are " + Arrays.toString(ftpNames)); // so null here...
for(String f : ftpNames) {
String localfile = localdir + f;
OutputStream os = new FileOutputStream(localfile);
try {
result = client.retrieveFile(f, os);
System.out.println("DOWNLOADING STARTED);
System.out.println(client.getReplyString());
client.noop();
}
catch(Exception e) {
System.out.println(e);
result = false;
}
finally {
if(os != null)
os.close();
}
}
client.logout();
System.out.println(client.getReplyString());
}
catch(Exception e)
{
System.out.println(e);
result = false;
}
finally
{
try
{
client.disconnect();
}
catch(Exception e)
{
System.out.println(e);
result = false;
}
}
return result;
}
As the error message explains, you're trying to iterate over a null object. You should check for this (or make sure an empty Iterable is used perhaps)
If this is an execptional (error) state, I'd check for this explicitly and throw some kind of runtime exception, e.g.:
if (ftpNames == null) {
throw new IllegalArgumentException("Cannot use a null set of FTP servers");
}
for (String f : ftpNames) {
ftpclient.retrieveFile(f, os); // os is OutputStream
}
Alternatively you could try to continue with no FTP servers, but seems a bit pointless.
Try to use ftpclient.enterLocalActiveMode();

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.

executing **kill -9 <PID>** command through java is not working

I am executing the below command to kill a process, but its not killing any process.
Command I tried: #kill -9 "2319"
But at the same time when I execute the same command in command prompt its working fine.
private void killProcess()
{
InputStream errorStream = null;
final String taskKill = isWindows() ? InstallerConstants.WIN_TASKKILL : InstallerConstants.LIN_TASKKILL;
try
{
Process process = null;
final String killCMD = taskKill+"\""+getGuiProcess()+"\"";\\kill -9 "2319"
process = runTime.exec(killCMD);
errorStream = process.getErrorStream();
bufferedReader = new BufferedReader(new InputStreamReader(errorStream));
String error = "";
while(bufferedReader.readLine() != null)
{
error = bufferedReader.readLine();
}
if(!(error==null || "".equals(error)))
{
logger.error(error);
}
}
catch (IOException ioException) {
logger.error(ioException.getMessage(), ioException);
}
finally
{
try
{
if(bufferedReader!=null)
{
bufferedReader.close();
}
if(errorStream!=null)
{
errorStream.close();
}
}
catch (IOException ioException) {
logger.error(ioException.getMessage(), ioException);
}
}
}
Please help to overcome this problem.
You need to specify the absolute path to the kill command
/bin/kill may be?
$ whereis kill //output will show you where kill command is.
do you get any errors btw?

Categories