title: How to return the output of scp command using expect script?
body: <p>I am trying to write an expect script which will automate the scp command and returns the status of scp command. Since I am new to ecpect script, I am not able to find any way by which this can be done. What I need is to automate SCP command using expect script, which exit with code 0 if scp is successful else return with the error like connection time out or link goes down while transferring file etc.</p>
  
  <p>Please help me on this.</p>
  
  <p>Following is my script snippet:</p>
  
  <pre><code># connect via scp
  spawn scp -o ConnectTimeout=30 /home/user/file.tar.gz root@192.168.1.146:/home/user
  
      #######################
      expect {
            -re ".*es.*o.*" {
                    exp_send "yes\r"
                            exp_continue
                              }
              -re ".*sword.*" {
                      exp_send "abc\r"
                            }
      }
      interact
  </code></pre>