1

I'm working on a project in which I intend to make a Java GUI application that connects to a ssh server and executes remote commands on the server. I'm willing to use JSch Library. My aim is to make buttons and textfields those will provide the user the ability of sending commands and getting replies easily. I mean, instead of opening xShell and prompting "grep "hi" /usr/file.txt", the user will choose the path from the list and will enter "hi" into the textfield and will press the button for grep.

Problem is, I couldn't find a solution to execute multiple linux commands in one session (I don't want shell if i cannot redirect its input and output streams) (also I don't want the solution "cd.. \n dir \n ls -l" which works fine but not solving my problem) send the arguments those shall be taken from related GUI components.

Since I have not made so much modifications on the JSch's example code, yet, you can see the code here: http://www.jcraft.com/jsch/examples/Exec.java.html

Thanks from now on.

orhan7
  • 33
  • 7
  • `shell` is the way to go. – Scary Wombat Apr 07 '15 at 08:06
  • @ScaryWombat hi, can i redirect shell's input and output stream to my GUI components, then? if yes, how? thanks from now on. – orhan7 Apr 07 '15 at 08:09
  • I am not sure if you can do directly, but to redirect to a String and then update your GUI from the String is easy enough – Scary Wombat Apr 07 '15 at 08:13
  • The sample program which you link to is perfectly capable of running a sequence of multiple commands. In fact, the program's default is a two-command pipeline. You say the "cd.. \n dir \n ls -l" solution works fine but doesn't solve your problem. What _is_ your problem? – Kenster Apr 07 '15 at 16:10
  • @Kenster for example, when the user wants to change to a directory from the directory list, selects a file and make a grep search in that file , --("cd ..") --("grep hi smp.txt"), in first step his path changes but in second step the path is reset(because exec channel does so). so how can I solve it? – orhan7 Apr 08 '15 at 06:11
  • I suggest you to consider using an Expect-like library. Take a look at my answer on a similar question: http://stackoverflow.com/a/24604951/3537858 – Alexey Gavrilov Apr 13 '15 at 10:39
  • @AlexeyGavrilov I will check it immediately, thanks from now on. – orhan7 Apr 13 '15 at 11:38
  • This limitation is RIDICULOUS! – Magno C Jul 17 '15 at 00:14

1 Answers1

0

If using exec type of channel you can combine commands with && :

channel.setCommand(". ./.profile && env");
Dmitry
  • 330
  • 1
  • 14