Getting error while using date as a variable in expect

Hi
getting error when i am saving date command output in a variable and then using the same variable in expect

#!/usr/bin/expect
set DATE [exec date -d "1 day ago" +%Y_%m_%d]
spawn sftp AUT#XX.XX.XX.XX
spawn echo $DATE
expect "password:"
send "AT123\n"
expect "sftp>"
send "cd /home/ \r"
expect "sftp>"
send "LOGS_$DATE.tar.gz \r"
expect "sftp>"
send "quit \r"

below is the error I am getting

spawn sftp AUT@XX.XX.XX.XX
spawn echo 2018_08_02
2018_08_02
send: spawn id exp5 not open
    while executing
"send "AT123\n""
    (file "./ns.sh" line 6)

pls let me know where I am doing wronge

Does it work without echo ing the $DATE variable?

Does it work when echo ing the $DATE variable before the sftp command?

1 Like

script is not working whether I do

echo

or not.
I have using echo in my script to just check if I m using the date command

set DATE [exec date -d "1 day ago" +%Y_%m_%d]

correct or not.

although it is working and can be seen in error o/p

2018_08_02

------ Post updated at 03:31 PM ------

Hi

also I have noticed if I hash out below command and then try to sftp the another file it is working successfully

set DATE [exec date -d "1 day ago" +%Y_%m_%d]

------ Post updated at 03:44 PM ------

Hi Rudic,

your suggestion works.

 Does it work without echoing the $DATE variable?
 Does it work when echoing the $DATE variable before the sftp command?

when I hash out the

 spawn echo $DATE

script start working.
can you tell me why this was causing the problem
initially I misunderstood your suggestion.

Sorry, no, I can't. Just guessing I'd say that the second spawn closes the first ( sftp ) one, and echo doesn't expect an answer / input.

HI RUDIC,
thx for help.

I have noticed another issue while running the script.
transferring of file getting closed in between. see below

LOGS_2018-08-02.tar.gz                23%   10MB   1.7MB/s   00:18 ETA

why this happening so I am not sure .. still there is a problem.
as I can transfer the file 100% when do manually . see below

LOGS_2018-08-02.tar.gz                100%   42MB 909.5KB/s   00:47

Network error / glitch? Is that reproducible? Any error messages?

Hi

I have resolved the above error by setting timeout time.
now the script is running fine but when I tried to run through it is showing error and state file not found

script mentioned below

#!/usr/bin/expect
set DATE [exec date -d "1 day ago" +%Y-%m-%d]
spawn sftp AUT#XX.XX.XX.XX
#spawn echo $DATE
expect "password:"
send "AT123\n"
expect "sftp>"
send "cd /home/ \r"
expect "sftp>"
send "LOGS_$DATE.tar.gz\r"
set timeout 2000
expect "sftp>"
send "quit \r

getting below error when script run through cron

# log
+ log
spawn sftp AUT@xx.xx.xx.xx
Connecting to xx.xx.xx.xx...
AUT@xx.xx.xx.xx's password: 
sftp> cd /home 
sftp> put LOGS_2018-08-05.tar.gz
stat LOGS_2018-08-05.tar.gz: No such file or directory

although the file is present on the path

can anyone help me

Can you sftp this from the command line without using expect?

Personally I think you are making the operation more complicated than it needs to be.

Are you able to use an SSH key pair to make the connection? This would get rid of the need to send passwords.

Are you only sending the one file? The script you have shown in previous posts could have been stripped down to show the problems you are experiencing. If it has not, and you are only sending one file, you could use SCP instead of SFTP, provided you can make use of an SSH key pair. Your script could then be something like:

#!/bin/bash
remuser=xxx
remhost=yyy
remdir=zzz
logfile=$(date -d "1 day ago" +"LOGS_%Y_%m_%d.tar.gz")
if [[ -e ${logfile} ]]
then scp ${logfile} ${remuser}@${remhost}:${remdir}
else echo "Cannot find log file ${logfile}"
fi

Andrew

Hi Andrew,

My current problem is that when I am trying to run my script by scheduling it in cron it did not work.
however when the same script I run manually it works.

this issue I am not able to figure out

Are you running the script manually from the same directory that it will be run by cron?

What happens when the full pathname of the log file is used in the script?

Andrew

Hi Andrew,
yes the path is same when I run manually or when it runs from cron.

below error I get when script run from cron

# log
+ log
spawn sftp AUT@xx.xx.xx.xx
Connecting to xx.xx.xx.xx...
AUT@xx.xx.xx.xx's password: 
sftp> cd /home 
sftp> put LOGS_2018-08-08.tar.gz
stat LOGS_2018-08-08.tar.gz: No such file or directory