Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exit from ssh session created from bash script?

Tags:

linux

bash

ssh

I have the following bash script test.sh

#!/bin/bash
ssh -o "StrictHostKeyChecking no" user@$1
#do some stuff
exit

However if I run this script ./test.sh I need to enter ctrl+d to get out of the ssh session and it appears as if the exit command is not working the way I intended.

Is there any way I can exit this ssh session that was created from this bash script?

like image 256
user784637 Avatar asked Mar 17 '13 08:03

user784637


1 Answers

You may want to code a here document

#!/bin/bash
ssh -o "StrictHostKeyChecking no" user@$1 << ENDHERE
   here do some remote stuff
   also here
   exit
ENDHERE

If you code <<-ENDHERE the initial spaces or tabs in each line of the here document are suppressed. Parameters (like $foo) are substituted unless you code <<\ENDHERE, and the ENDHERE can actually be any word, conventionally in capitals.

Read the Advanced Bash Scripting guide (which does have some imperfections)

like image 95
Basile Starynkevitch Avatar answered Sep 20 '22 01:09

Basile Starynkevitch