Search code examples
bashexpect

Expect - Spawn exiting after sometime


I written a script for sftp. Its working fine for few seconds. But exiting after few seconds. I tried in so many ways but cant able to figure out the problem. Here is my code.

#!/usr/bin/expect

set now [clock seconds]
set now [expr {$now - 3600}]
set date [clock format $now -format {%Y%m%d%H}]

set SOURCE "/home/sms/SMSC-Sec-2.0.7.0_MT/cdrs/"
spawn sftp root@10.167.250.75
  expect "password:"
     send "teledna\r"
     sleep 1
     expect "sftp>"
        send "cd /home/sms/CDRS-Noida \r"
        expect "sftp>"
           send "pwd \r"
           expect "sftp>"
               send "mput $SOURCE/DNA-AegisGw-CDR_PREPAID_$date*.log  \r"
               expect -exact "sftp>"
               send "quit \r"
send "I am here"

Output:

spawn sftp root@10.167.xx.xx
root@10.167.xx.xx's password:
Connected to 10.167.xx.xx.
sftp> cd /home/sms/CDRS-Noida
sftp> pwd
Remote working directory: /home/sms/CDRS-Noida
sftp> mput /home/sms/SMSC-Sec-2.0.7.0_MT/cdrs//DNA-AegisGw-CDR_PREPAID_2017031618*.log
Uploading /home/sms/SMSC-Sec-2.0.7.0_MT/cdrs//DNA-AegisGw-CDR_PREPAID_201703161800_499_10.170.xx.xx_AS.log to /home/sms/CDRS-Noida/DNA-AegisGw-CDR_PREPAID_201703161800_499_10.170.xx.xx_AS.log
/home/sms/SMSC-Sec-2.0.7.0_MT/cdrs//DNA-AegisGw-CDR_PREPAID_201703161800_499_10.170.xx.xx_AS.log   2% 1088KB 104.3KB/s   06:39 ETA[root@smsc admin]#

Exited after few seconds say 5 like that


Solution

  • Since you're mputing many files it's expected to take quite a while. So try using a longer timeout (default: 10) after mput:

    send "mput $SOURCE/DNA-AegisGw-CDR_PREPAID_$date*.log \r" 
    expect -timeout 600 -exact "sftp>"
    

    or you can also set the global timeout:

    set timeout 600
    spawn ...
    

    And an infinite timeout may be specified with the value −1.