简体   繁体   中英

Calling a shell script using SSH exec channel, but it ignores calls to other shell scripts

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. ie 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();

I assume the script looks like:

script_one.sh
script_two.sh

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM