-2

I have written a Java program which when executed would open an upload wizard through which a user can select a file. After doing this, the selected file would be transferred/uploaded to a Unix box to a directory which the user specifies and processed further. Here are the steps/actions:

  1. Browse the file through the upload wizard
  2. After this the program would prompt for a new directory to be created, the name of which the user can give as an input
  3. The program creates a new directory (source) and places the selected file in the created directory of the server
  4. Show the contents of the transferred file in the source (using cat command)
  5. Prompt the user for a new directory to be created (target), copy the file from the source to target and subsequently show the contents of the file in the target, all with Linux commands.

I'm able to upload the file to the source (step 3). However, the 4th and the 5th steps don't work. Here's the code:

String cmd1 = "cat" + " " + path + s1 + "/" + file;
System.out.println(cmd1);
((ChannelExec)channel).setCommand(cmd1);
Scanner in2 = new Scanner(System.in);
System.out.println("Enter d target directory");
String s = in2.nextLine();
String path2 = "/e/f/";
String d = path2 + s;
String cmd2 = "mkdir" + " " + path2 + s;
((ChannelExec)channel).setCommand(cmd2);
String src = p + "/" + file;
String cmd3 = "cp" + " " + path + s1 + "/" + file + " " + path2 + s;
((ChannelExec)channel).setCommand(cmd3);
String destpath = d + "/" + file;
String cmd4 = "cat" + " " + path2 + s + "/" + file;

I'm not able to make the program work with variables (for user inputs) in the command. However, hardcoded strings like, for eg. cat /a/b/file seems to be able to work.

Could any one please help me in this regard?

Raffaele
  • 20,627
  • 6
  • 47
  • 86
anand k
  • 1
  • 2

2 Answers2

0

I think the problem is in some mess with variables values (variable d is created but almost not used) and final / character. And I don't know the p value.

I would start adding a println for cmd2 and cmd3 before executing them because I think there will be a missing / in some place, or perhaps two / where should be only one depending on what the user types, and doing some normalization of directories so they always have a final /, or they never have, but I would like to be sure that whatever the user inputs or the parameters have, all directory names are consistent.

Directory names, user input and final / are always a pain...

Ivan Aracki
  • 4,861
  • 11
  • 59
  • 73
Nil
  • 127
  • 3
  • When I print the commands on the console, they seem to be fine. For eg., printing cmd1 gives cat /a/b/src/file.txt. cmd2 gives mkdir /e/f/tgt, whhich would otherwise work on a terminal. The strings src and tgt are user inputs for the directory names without the / characters. – anand k Feb 23 '18 at 11:18
0

From your snippet i guess channel is a ChannelExec, which is a channel intended for just issuing commands and does not work like a shell (that use-case is handled by the shell channel, which allocates a pseudo-tty).

In your case I'd simplify the code and not using variables at all. Otherwise, if you absolutely need them, you could:

  • if the variable is defined your Java program call ChannelExec.setEnv(name, value)
  • if the variable is defined on the target host, ensure it's in the right place, for example a file sourced when a non-interactive SSH session is spawned. A good place could be .bashrc but you should check your system documentation (and maybe this other answer)
Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • In this case, what would be the value of the here? I'm not sure I follow this concept. :( – anand k Feb 23 '18 at 12:36
  • Maybe I misunderstood your question. When you say the program *only works with hardcoded paths and not with variables* you mean that your path is in the form `/home/user/$TARGET/file.txt` or rather `"/home/user/" + target + "/file.txt"`? – Raffaele Feb 23 '18 at 13:31
  • Yes, it'd be "/home/user/" + target + "/file.txt" in this manner. The value of the string target is taken from the user. Glad that you asked. :) – anand k Feb 23 '18 at 13:42
  • Ok, so this answer is not relevant. If the path works when hardcoded and does not work when assembled from user input, then either the two strings end up being different from each other or even another code path is executed altogether. Unfortunately, nobody can tell without having a breakpoint in your IDE, but luckily you can :-) Simply set a breakpoint at the offending line and analyze every difference you can find between the two scenarios (hardcoded and from user input). I mean, also look at other variables and at the server state – Raffaele Feb 23 '18 at 13:51
  • Yes. I've done that as well. But there seems to be no difference between the two scenarios. I've printed the commands to the console which gives the exact same command which can be executed directly on the server. I've begun to suspect that ((ChannelExec)channel).setCommand(cmd3) doesn't support/wok with variables. – anand k Feb 23 '18 at 14:03
  • It can't be :-) Maybe there's some other difference. Check the state on the server. Maybe some previous command has unexpected effects – Raffaele Feb 23 '18 at 14:06
  • channel = session.openChannel("exec"); channel.connect(); String cmd1 = "cat" + " " + path + s1 + "/" + file; path: "/a/b/"; s1:"src". As you can see, cmd1 is the first command to be executed after opening/connecting to the "exec" session. When cmd1 is written as "cat /a/ab/src/file", it works fine. Hence, I was led to the above conclusion. – anand k Feb 23 '18 at 17:12
  • String cmd1 = "cat" + " " + path + s1 + "/" + file; Execution of this command returns an exit status, channel.getExitStatus() = -1. But I'm able to execute the command in cmd1 directly on the Unix box. Also, cmd2, cmd3 and cmd4 return the exit status 0, i.e. channel.getExitStatus() = 0 for these commands but still the corresponding actions for these commands aren't reflected on the server. Please help! – anand k Feb 26 '18 at 11:27
  • Sorry, we can't really help further. Can you switch to some other library/tool like Python fabric/Paramiko? – Raffaele Feb 26 '18 at 13:36
  • :D :D Can't! Obsessed with this now...jst like the Joker is with Batman! BTW I received the following error string: com.jcraft.jsch.Channel$MyPipedInputStream@622948 for cmd1. – anand k Feb 26 '18 at 14:41