Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use expect inside bash script [duplicate]

Tags:

bash

expect

Could anybody please tell me why this is not working?

#!/bin/bash
cd /home
touch somefile
/usr/bin/expect<<FILETRANSFER
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "assword:"
send "MyPassWord\r"
interact
FILETRANSFER
echo "It's done"

It doesn't give any error but file is not transferred to remote server.I have tried many ways still couldn't find any solution.

like image 909
Hasan Rumman Avatar asked Jul 17 '17 13:07

Hasan Rumman


People also ask

How do you use loop in Expect script?

Expect For Loop Examples: for {initialization} {conditions} {incrementation or decrementation} { ... } Expect for loop example : for {set i 1} {$i < $no} {incr i 1} { set $total [expr $total * $i ] } puts "$total"; Note: You should place the loop open brace in the same line as it contains “for” keyword.

What is bash expect?

Bash scripts provide many programs and features to carry out system automation tasks. The expect command offers a way to control interactive applications which require user input to continue.

How do I copy a bash script?

Copy a File ( cp ) You can also copy a specific file to a new directory using the command cp followed by the name of the file you want to copy and the name of the directory to where you want to copy the file (e.g. cp filename directory-name ). For example, you can copy grades. txt from the home directory to documents .


1 Answers

The bash script you have defined is passing the expect commands on the standard input of expect. However, the expect command requires its arguments on a file or as an argument using the -c option.

You have several options but to add the less modifications on your script you just need to use the process substitution to create a here-document (temporary) for the expect command.

#!/bin/bash 

  echo "[DEBUG] INIT BASH"

  cd /home
  touch somefile

  /usr/bin/expect <(cat << EOF
spawn scp -r -P remoteServerPort somefile remoteServerIP:/home
expect "Password:"
send "MyPassWord\r"
interact
EOF
)

  echo "[DEBUG] END BASH"
like image 107
Cristian Ramon-Cortes Avatar answered Oct 05 '22 04:10

Cristian Ramon-Cortes