LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Closed Thread
  Search this Thread
Old 11-01-2012, 12:34 PM   #1
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Rep: Reputation: Disabled
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.

Last edited by hbchok; 11-01-2012 at 12:35 PM.
 
Old 11-02-2012, 05:44 AM   #2
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
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"

Last edited by cbtshare; 11-02-2012 at 06:00 AM.
 
Old 11-02-2012, 05:49 AM   #3
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.8, Centos 5.10
Posts: 17,240

Rep: Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324Reputation: 2324
Does it have to be sftp?
Using scp with ssh auth-keys would be so much simpler.
 
Old 11-02-2012, 06:14 AM   #4
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
it is hard to understand for me.

Quote:
Originally Posted by cbtshare View Post
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.
 
Old 11-02-2012, 06:17 AM   #5
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
no, keys will not allowed.

Quote:
Originally Posted by chrism01 View Post
Does it have to be sftp?
Using scp with ssh auth-keys would be so much simpler.
Yes have to be sftp.

Thanks.
 
Old 11-02-2012, 06:45 AM   #6
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
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)
#==========================
 
Old 11-02-2012, 09:29 AM   #7
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
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"
 
Old 11-02-2012, 09:56 AM   #8
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Error ; not working

Quote:
Originally Posted by cbtshare View Post
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"
============

Last edited by hbchok; 11-02-2012 at 10:23 AM.
 
Old 11-02-2012, 06:35 PM   #9
cbtshare
Member
 
Registered: Jul 2009
Posts: 610

Rep: Reputation: 42
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

Last edited by cbtshare; 11-02-2012 at 06:40 PM.
 
Old 11-03-2012, 05:06 AM   #10
hbchok
LQ Newbie
 
Registered: Nov 2012
Posts: 14

Original Poster
Rep: Reputation: Disabled
Thumbs up

Quote:
Originally Posted by cbtshare View Post
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####

Last edited by hbchok; 11-03-2012 at 05:09 AM.
 
Old 11-03-2012, 05:23 AM   #11
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976Reputation: 1976
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.
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
expect script using gpg decrypted file as variable mike909 Linux - Newbie 0 11-01-2011 07:42 PM
How to set file permissions using SFTP client thesignchef Linux - Server 3 04-27-2009 01:00 PM
Check for Success of SFTP transfer using expect kasthana Programming 4 06-03-2008 11:55 AM
transfer file using expect and sftp tanveer Linux - General 7 05-16-2008 11:29 AM
set a line in a file as a variable tpreitano Linux - General 5 08-24-2005 12:53 PM


All times are GMT -5. The time now is 10:49 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration