Quando a instância interativa ssh
é gerada, ela assume a instância atual de bash
e os seguintes comandos não são executados até que ela tenha terminado sua execução.
user@user-X550CL ~ % ssh user@localhost; echo string
user@localhost's password:
Welcome to Ubuntu 15.04 (GNU/Linux 3.19.0-15-generic x86_64)
* Documentation: https://help.ubuntu.com/
Last login: Tue Sep 15 09:10:01 2015 from localhost
user@user-X550CL ~ % exit
Connection to localhost closed.
string
Existem várias maneiras de resolver isso:
- Passar o (s) comando (s) como um argumento:
spawn ssh root@hostname /sbin/service heartbeat status
user@user-X550CL ~/tmp % ssh user@localhost echo command1
user@localhost's password:
command1
user@user-X550CL ~/tmp % ssh user@localhost 'echo command1; echo command2; echo command3'
user@localhost's password:
command1
command2
command3
user@user-X550CL ~/tmp %
- Agrupe os comandos em outro script e passe o script usando
stdin
redirection:
user@user-X550CL ~/tmp % ssh user@localhost <script.sh
Pseudo-terminal will not be allocated because stdin is not a terminal.
user@localhost's password:
Welcome to Ubuntu 15.04 (GNU/Linux 3.19.0-15-generic x86_64)
* Documentation: https://help.ubuntu.com/
command1
command2
command3
user@user-X550CL ~/tmp %