Not able to execute Unix commands containing variables using JSch library

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-14 03:28:21

问题


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?


回答1:


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)



回答2:


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...



来源:https://stackoverflow.com/questions/48945949/not-able-to-execute-unix-commands-containing-variables-using-jsch-library

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!