3

I want to write a bash script which sends a password to authenticate with a remote server.

For example, I want to write a bash script which would pull the code from a github repository to a server, but I have to specify a password or passpharse to do it.

How am I supposed to code that? I tried couple of things but nothing worked.

Tim Post
  • 33,371
  • 15
  • 110
  • 174
Jeevan Dongre
  • 4,627
  • 13
  • 67
  • 129

1 Answers1

7

I think it was discussed a lot but you could use expect. Here is an example of using it to connect to host using ssh:

#!/usr/bin/expect

set password "YOURPASSWORD"
set user "john"
set host "192.168.1.100"

spawn ssh $user@$host
expect "Password:"
send "$password\n";
interact

Replace "ssh user@host" with your command you want to execute, e. g. "git pull":

spawn git pull

The next line tells which prompt to expect ("Password:"). Make sure this string is set to exactly what appears after you manually do git pull. Case sensitivity and spaces are important.

EDIT: installing expect is easy, on Debian-based systems:

apt -y install expect

on RedHat-based:

yum -y install expect
dimir
  • 693
  • 6
  • 23
  • Expect is a really nice tool to use, especially if you have commit / pull / etc hooks that prompt for additional input. – Tim Post Dec 28 '11 at 14:14
  • 1
    `autoexpect` is another good tool. I like to use that to create the original script, then go in and edit out the parts I think need fixing. – SiegeX Dec 28 '11 at 18:09