You generally should not use "shell" channel to automate a command execution.
The "shell" channel is intended to implement an interactive shell session.
Hence it requests PTY (pseudo-terminal), which has human-friendly, but machine-unfriendly, side-effects that break your code.
Use "exec" channel instead.
See Execute a list of commands from an ArrayList using JSch exec in Java
If you need or want to use the "shell" channel for some reason (but do not, it will bite you anyway), make sure you call .setPty(false)
before .connect
:
channel = (ChannelShell)getSession().openChannel("shell");
channel.setPty(false);
channel.connect();
Side notes:
- The
"#!/bin/bash"
is a pure nonsense. The shell ignores all commands starting with #
(it's comment). There's no point sending it.
- Do not use
StrictHostKeyChecking=no
. See JSch SFTP security with session.setConfig(“StrictHostKeyChecking”, “no”);.