set variable in expect to copy file through sftp
Hi,
My task is = copy a file from sftp and put on local machine. Need to do this every day using crontab. Now file name is variable like yyyymmdd.log I have write a script using expect but it is not working. ERROR = sftp> can't read "date": no such variable while executing "send "get $date --date='-1 days' +%Y%m%d.log \r"" (file "./sftp.exp" line 9) Requesting you to please update my below script in right format. #/usr/bin/expect spawn /usr/bin/sftp user@11.22.33.44 expect "11.22.33.44's password:" send "abcdefg" send "\r" expect "sftp>" send "ls \r" expect "sftp>" send "get $date --date='-1 days' +%Y%m%d.log \r" expect "sftp>" send "bye \r" Thanks a lot. |
You need to Establish a local variable to store the new component of the file name (in
this case, a modification of Date) export WHEN=`date +%m%d%y` on the local server then all you need in the script is Quote:
|
Does it have to be sftp?
Using scp with ssh auth-keys would be so much simpler. |
it is hard to understand for me.
Quote:
I am very new for linux and as well for programming. I am unable to put your text in the script. Requesting you to please update my script. Thanks a lot. |
no, keys will not allowed.
Quote:
Thanks. |
I have updated script like below but still error is coming.
UPDATED SCRIPT= #========================== #/usr/bin/expect set file "$(date --date='-1 days' +%Y%m%d.log)" spawn /usr/bin/sftp user@11.22.33.44 expect "11.22.33.44's password:" send "abcdefg" send "\r" expect "sftp>" send "ls \r" expect "sftp>" send "get file \r" expect "sftp>" send "bye \r" #========================== ERROR #========================== expect ./sftp.exp can't read "(date --date='-1 days' +%Y%m%d.log)": no such variable while executing "set file "$(date --date='-1 days' +%Y%m%d.log)"" (file "./sftp.exp" line 2) #========================== |
I already gave you the script:
make the change on the local server: Quote:
then the script is Quote:
|
Error ; not working
Quote:
=========== ERROR = =========== can't read "WHEN": no such variable while executing "spawn "echo get $WHEN.log" | /usr/bin/sftp Averda@gateway.rmmel.com" (file "/home/mydesk/Desktop/1/sftp.exp" line 3) =========== UPDATED SCRIPT = =========== [as you explain I have given the command in local server first (export WHEN=`date +%m%d%y`) then after I am running the script] #!/usr/bin/expect spawn "echo get $WHEN.log" | /usr/bin/sftp user@11.22.33.44 expect "user@11.22.33.44's password:" send "abcdefg" send "\r" expect "sftp>" send "ls \r" expect "sftp>" spawn "get $WHEN \r" (File name will change every day so I will have to give here variable name) expect "sftp>" send "bye \r" ============ |
when you do
Quote:
then if when you run Quote:
It works on my end, so your doing something wrong |
Quote:
BTY I got solution, is as below. I will have to say Thanks to Rakesh for this support. ####start#### #!/usr/bin/expect -f set DATE [exec date --date=yesterday +"%Y%m%d".csv] spawn sftp user@11.22.33.44 match_max 100000 expect -exact "user@11.22.33.44's password:" send -- "abcdedfh" expect "sftp>" send "get $DATE \r" expect "sftp>" send "bye \r" interact ####end#### |
Please post your thread in only one forum. Posting a single thread in the most relevant forum will make it easier for members to help you and will keep the discussion in one place. This thread is being closed because it is a duplicate. The original thread was http://www.linuxquestions.org/questi...te-4175435310/ and discussion took place there.
|
All times are GMT -5. The time now is 03:41 PM. |