1

I'm writing "fake deployment" bash script I want to run to pull latest master on server. I have a following script:

#!/usr/bin/expect -f
spawn ssh -t user@host.domain 'cd path/to/dir'
expect "continue"
sleep 1
send "yes\r"
expect "assword:"
sleep 1
send "password\r"

# Commands for remote
git pull origin master
expect "sername"
sleep 1
send "username\r"
expect "assword"
sleep 1
send "password\r"
interact

Connection is made successfully, however, when the git pull origin master is ran, the error is triggered: invalid command name "git".

I'm probably should run the commands differently, unfortunately, I'm not sure how. How will I be able to run the "Commands for remote" on remote? Thanks in advance.

Dawid Zbiński
  • 5,521
  • 8
  • 43
  • 70
  • is git installed on the machine? this error mean it doesn't.... – shahaf Apr 04 '18 at 16:53
  • Thanks for comment. Git is surely installed there, as when I do it manually I'm fully able to use git commands. I guess the problem might be that after a connection is established commands are ran in some weird environment (not inside the terminal directly). But I'm writing the bash script for the third time in my life so I'm not sure. – Dawid Zbiński Apr 04 '18 at 16:58
  • 1
    Regardless of your issue with the git command itself, why are you using **expect**? You can use either ssh keys : https://help.github.com/articles/connecting-to-github-with-ssh/ or personal access tokens : https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/ and you avoid having to use expect at all. – Matias Barrios Apr 04 '18 at 18:28

1 Answers1

0

ssh will leave you on the remote host side with a standard limited PATH.

If git is not installed in /usr/bin, you might have to modify your script in order to write the full path of the git executable.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250
  • `which git` outputs that it is installed in `/usr/bin`. I guess there might be some other issue. The weird thing is the message `invalid command name`. Normally, when you try to execute the command which doesn't exist it says `-bash: "command" : command not found` – Dawid Zbiński Apr 04 '18 at 20:20
  • @DawidZbiński `which git` when done after the `exec \$SHEL`? – VonC Apr 04 '18 at 20:21
  • I'm sorry, I didn't update the code. Here it is, the updated version. I tried running `which git` after manually logging in. It gives the right path `/usr/bin/git/`. I'm still wondering why is the error message that weird. Should probably respond with `command not found` if the command is not found, but it responds with `invalid command name`. – Dawid Zbiński Apr 04 '18 at 20:30
  • I added `which git` right before the `git pull origin master` command. It response with `invalid command name 'which'` as well. – Dawid Zbiński Apr 04 '18 at 20:32
  • @DawidZbiński Is this similar to https://stackoverflow.com/q/16212628/6309? And https://askubuntu.com/a/959662/5470. – VonC Apr 04 '18 at 20:33
  • It's similar to the first one, but it does not give me an error because of "what" I'm expecting. it just doesn't want to run the commands once logged in. Also, I'm wondering... until know I though that the only requirement for me to use expect is to have it installed on my computer (from which I run the script), but doesn't it need to be installed on the remote host too? – Dawid Zbiński Apr 04 '18 at 20:42
  • @DawidZbiński you run the script on the remote side though, through ssh. – VonC Apr 04 '18 at 20:43
  • I have no clue how does the expect work exactly, but I thought that it's using some sort of listener to wait until terminal wants some data and then sends the response. E.g. in my example, I thought the ssh session will be opened, `git pull origin master` will be ran as an ordinary command, the `expect "sername"` would wait until terminal asks for username, etc. To simplify... I thought expect is just an "extension" in my terminal to input the data when terminal asks for them. So I guess I have it all wrong then, right? – Dawid Zbiński Apr 04 '18 at 20:50
  • @DawidZbiński Yes, that seems off, especially when I compare it to https://stackoverflow.com/q/16212628/6309. – VonC Apr 04 '18 at 20:53