0
#! /bin/bash
read -p 'Username: ' user
read -p 'IP: ' ip

ssh "$user"@"$ip"

osascript -e ' tell app "System Events" to display "Hello world"'

This file will be ran automatically so it's not like I'm running command after command

It doesn't run the last command and I don't know why

This is terminal on mac

1
  • Pseudo-terminal will not be allocated because stdin is not a terminal. Password:29:38: syntax error: A “"” can’t go after this identifier. (-2740) thats the error I got – Chad McMillin Feb 18 '15 at 3:53
1

I don't know how ssh exactly operates, but your script is written in a way that it will wait until the ssh command finally terminates. Then will it continue and run your last line (osascript...). To avoid waiting for ssh to return, write a & at the end of the ssh line. It will have the interpreter to continue executing the next command in a parallel thread.

1

Your last command,

osascript -e ' tell app "System Events" to display "Hello world"'

will be executed only if you exit the remote ssh terminal. In this way you can not achieve what you are trying to do.

Hopefully there is expect script. You can work with the following script,

#!/bin/sh
expect <<'END'
set timeout 10
set login "your_username" 
set addr "remote_ip"
set pw "your_password"             # skip this line if you have password less login
spawn ssh $login@$addr
expect "$login@$addr's password:"  # skip this line if you have password less login
send -- "$pw\r"                    # skip this line if you have password less login
expect "~"                         # use your prompt here, I have ~, replace ~ with your prompt
send -- "ls\r"                     # replace ls with your command
expect "~"
send -- "exit\r"
expect eof
END

See How to execute sudo commands with Expect & send commands in bash script?

1
  • If you want to continue with the remote terminal, you can use interact command instead of the line send -- "exit\r". – sourav c. Feb 18 '15 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.