LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   set variable in expect to copy file through sftp (https://www.linuxquestions.org/questions/linux-newbie-8/set-variable-in-expect-to-copy-file-through-sftp-4175435098/)

hbchok 11-01-2012 11:34 AM

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.

cbtshare 11-02-2012 04:44 AM

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:

#!/usr/bin/expect

spawn "echo "get $WHEN.log " | /usr/bin/sftp user@11.22.33.44
expect "11.22.33.44's password:"
send "abcdefg"
expect "Fetching /path to the file/110212.log to 110212.log"
send "bye \r"

chrism01 11-02-2012 04:49 AM

Does it have to be sftp?
Using scp with ssh auth-keys would be so much simpler.

hbchok 11-02-2012 05:14 AM

it is hard to understand for me.
 
Quote:

Originally Posted by cbtshare (Post 4820308)
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

Thanks cbtshare for your update

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.

hbchok 11-02-2012 05:17 AM

no, keys will not allowed.
 
Quote:

Originally Posted by chrism01 (Post 4820314)
Does it have to be sftp?
Using scp with ssh auth-keys would be so much simpler.

Yes have to be sftp.

Thanks.

hbchok 11-02-2012 05:45 AM

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)
#==========================

cbtshare 11-02-2012 08:29 AM

I already gave you the script:

make the change on the local server:
Quote:

export WHEN=`date +%m%d%y`
on the local server


then the script is


Quote:

#!/usr/bin/expect

spawn "echo "get $WHEN.log " | /usr/bin/sftp user@11.22.33.44
expect "11.22.33.44's password:"
send "abcdefg"
expect "Fetching /path to the file/110212.log to 110212.log"
send "bye \r"

hbchok 11-02-2012 08:56 AM

Error ; not working
 
Quote:

Originally Posted by cbtshare (Post 4820482)
I already gave you the script:

make the change on the local server:
on the local server


then the script is

It is not working see the error and updated script below.
===========
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"
============

cbtshare 11-02-2012 05:35 PM

when you do
Quote:

echo $WHEN
on the local server, what do you get?

then if when you run

Quote:

echo "get $WHEN.log" | /usr/bin/sftp user@11.22.33.44
does it get the file?(Create a mock file on the server with today's date.log)

It works on my end, so your doing something wrong

hbchok 11-03-2012 04:06 AM

Quote:

Originally Posted by cbtshare (Post 4821051)
when you do on the local server, what do you get?

then if when you run



does it get the file?(Create a mock file on the server with today's date.log)

It works on my end, so your doing something wrong

Okay Thanks for your efforts,

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####

colucix 11-03-2012 04:23 AM

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.