3

I am managing to execute a shell script on a remote server using JSch exec using the helpful examples provided here. I can see the echoes being returned from the script and the exit status at the end is 0 - so all looks good at first glance.

However, the problem is that the script itself calls out to other scripts, and these appear to be completely ignored, just skipped over.

The script calls other scripts directly. i.e. the first line of the script is something like:

script_two.sh

Could anyone advise of any way to overcome this? I did start to look into the "shell" channel instead of "exec", but this may be tricky in my case because before giving the user access to the system, the server presents a form to fill in (name, number, why are you logging in, etc) - I haven't yet been able to to programmatically fill in and submit this form, so I would like to stick with exec if possible.

I am new to all this, so any help/advice would be most welcome!

Code snippet below. As I said, this appears to work, but the sh script represented by "scriptFileName" int he code calls other sh scripts, and these are not executed.

Many thanks in advance for any help, J

JSch jsch = new JSch();
JSch.setConfig(FileTransferConstants.STRICT_HOST_KEY_CHECKING, "no");

Session session = jsch.getSession(username, hostIPAddress, port);
session.setPassword(password);
session.connect();

//create the execution channel over the session
ChannelExec channelExec = (ChannelExec)session.openChannel("exec");

channelExec.setCommand(scriptFileName);
channelExec.connect();
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • *"calls out to other scripts, and these appear to be completely ignored, just skipped over"* - How does the script calls out to other scripts? How are they ignored? Does the "call out" produce any error message? – Martin Prikryl Jun 28 '16 at 10:58
  • Thanks for the reply Martin. The script calls other scripts directly. i.e. the first line of the script is something like: - script_two.sh When I run the script manually as the same user, it works fine, but it seems to be ignored when run remotely using Jsch shell. – Jonathan Head Jun 28 '16 at 11:54

1 Answers1

3

I assume the script looks like:

script_one.sh
script_two.sh

I.e. the script relies on the . (the current path) being in the PATH environment variable, what is not a default.

So for the script to ever work, the . has to be added to the PATH in some startup script. It's quite probable that the addition occurs (probably unintentionally incorrectly) only for interactive sessions. Probably because the addition is done in a startup script that is executed (sourced) for the the interactive sessions only.

The "exec" channel in the JSch (rightfully) does not allocate a pseudo terminal (PTY) for the session. As a consequence a different set of startup scripts is (might be) sourced than, when you login with an SSH client. And/or different branches in the scripts are taken, based on absence/presence of the TERM environment variable. So the environment might differ from the interactive session you use with your SSH client.


Solutions are (in preferred order):


See also a related question Shell ping command with source option is failing when executed using JSch setCommand.

Community
  • 1
  • 1
Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
  • Martin, I can't thank you enough for such a detailed and well thought and explained out response. Please know that it is a massive help to me and has explained things very well. I will have a chat to the team and particularly the guys responsible for the.sh scripts on the remote server to attempt to get this actioned. I will report back here with any updates. You are absolutely right, the scripts are called from the master script WITHOUT the . – Jonathan Head Jun 28 '16 at 12:26
  • 1
    Thanks again! Just accepted the answer and just read the note in the link "Please do not add a comment on your question or on an answer to say "Thank you"". Ooops - sorry! And now I'm doing it again! I will add further technical (non social) notes once this has actually been implemented and tested. – Jonathan Head Jun 28 '16 at 12:34