0

I'm trying to pass a script on a remotehost without getting the command outputs on the jenkins console. an example script:

ssh -o StrictHostKeyChecking=no -t -t update@dispotest  bash -s 'hello' <<'END'
printf '%s world!\n' "$1"
exit
END

produces following output:

+ sh start_stop_jboss_remote.sh
printf '%s world!\n' "$1"

exit
[update@dispotest ~]$ printf '%s world!\n' "$1"
hello world!
[update@dispotest ~]$ 
[update@dispotest ~]$ exit
exit
Connection to dispotest closed.
Finished: SUCCESS

My main issue is that the whole part between the two endssh will be postet in the console output of jenkins even twice. The first time i guess is for starting the ssh session and the second time is inside the ssh session. This makes finding the right place to look quiet hard. So i was hoping someone has a idea how to hide the script itself from the console. But i don't wan't to mute it completely. I'm still interested in the output of the commands running in the ssh session.

I have experimented with > /dev/null alot. but often also the output of the echo or the command executed in the shell where gone which i d like to preserve.

some examples i tried

ssh -o StrictHostKeyChecking=no -t -t update@dispotest  bash -s 'hello' <<'END'
{
printf '%s world!\n' "$1"
exit
} 2> /dev/null
END

outputs at least the commands bundled

+ sh start_stop_jboss_remote.sh
{

printf '%s world!\n' "$1"
exit

} 2> /dev/null
[update@dispotest ~]$ {
> 
> printf '%s world!\n' "$1"
> exit
> 
> } 2> /dev/null
hello world!
Connection to dispotest closed.
Finished: SUCCESS

example 3

ssh -o StrictHostKeyChecking=no -t -t update@dispotest  bash -s 'hello' 2> /dev/null <<'END'
printf '%s world!\n' "$1"
exit
END

outputs

+ sh start_stop_jboss_remote.sh
printf '%s world!\n' "$1"
exit
[update@dispotest ~]$ printf '%s world!\n' "$1"
hello world!
[update@dispotest ~]$ exit
exit
Finished: SUCCESS

example 4

ssh -o StrictHostKeyChecking=no -t -t update@dispotest  bash -s 'hello' 2> /dev/null <<'END'
{
printf '%s world!\n' "$1"
exit
} 2> /dev/null
END

outputs

+ sh start_stop_jboss_remote.sh
{
printf '%s world!\n' "$1"
exit
} 2> /dev/null
[update@dispotest ~]$ {
> printf '%s world!\n' "$1"
> exit
> } 2> /dev/null
hello world!
Finished: SUCCESS

1 Answer 1

0

You invoke an interactive login shell with -t -t. Remove these from your command line.

The correct way to pass arguments to a bash -s script is

bash -s "$COMMAND" "$SERVICENAME" <<'END_SCRIPT'
here goes
script that uses
"$1" and "$2"
END_SCRIPT

The script is passed the values of $COMMAND and $SERVICENAME and these are available as $1 and $2 in the script. The script itself should furthermore be quoted, which is done by quoting the here-document tag at the start.

In your case:

ssh -o StrictHostKeyChecking=no "user@$SERVER"  bash -s "$COMMAND" "$SERVICENAME" <<-'ENDSSH'
        echo "I'd like to see this"
        if [ "$1" = "stop" ]; then
            ...
        fi
ENDSSH

Note that the ENDSSH tag at the end has to be either in the first column (first on the line), or indented using a tab character.

Example that works for me:

ssh myhost bash -s 'hello' <<'END'
printf '%s world!\n' "$1"
END

which produces the string hello world! in the terminal.

7
  • Hi, and thanks for the quick response. I' have edited my question so you can see the output your example generates. Actually i'm only intereseted in line 1 & 6
    – Severin N
    Commented Jun 12, 2018 at 13:23
  • @SeverinN See top of updated answer.
    – Kusalananda
    Commented Jun 12, 2018 at 13:59
  • @ Kusalananda thanks for that hint, but i need to be able to run the sudo command :-( and the only way i know how to do that is with the -t parameter...
    – Severin N
    Commented Jun 12, 2018 at 14:10
  • @SeverinN Put the script on the remote server and execute it with ssh directly rather than trying to pass it on standard input.
    – Kusalananda
    Commented Jun 12, 2018 at 14:18
  • @ Kusalananda i'm not so happy with this solution because i run it on multiple servers and i would need to copy the scripts on the remote server just to ensure it's the newest state... but if there is no other way i will do it as soon the current solution (which works) annoys me to much.
    – Severin N
    Commented Jun 12, 2018 at 14:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .