LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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


Reply
  Search this Thread
Old 07-12-2019, 02:27 AM   #1
vishinfy
LQ Newbie
 
Registered: Jul 2019
Posts: 1

Rep: Reputation: Disabled
set variable in expect to copy file through sftp


My task is =
copy a file from sftp and put on local machine.
But am not able to pass variable in sftp mode.

Here is the script

#!/usr/bin/expect -f
source /opt/infa_shared/icm/Infa_Adm/ParamFiles/ICM_param.param

set DATE="%Y%m%d"
expect -c 'spawn sftp (username)@Servername
expect "*password:"
--- Password is given here---
expect "sftp>"
send "cd /Home/nni_mktbi_synergistix/Samples/Synergistix/ \r"
expect "sftp>"
send "get $DATE \r"
expect "sftp>"
send "lcd /opt/infa_shared/icm/Infa_Adm/TgtFiles/ \r\n"
expect "sftp> "
send "quit \r"'

Error thrown here is
sftp> can't read "DATE": no such variable
while executing
"send "get $DATE \r""

Please resolve. Tried other forums for solution but its not working!!

Thanks
Vishinfy
9962588575
vishinfy@gmail.com

Last edited by vishinfy; 07-12-2019 at 02:35 PM.
 
Old 07-12-2019, 02:38 AM   #2
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Welcome. Please review the guidelines on how to ask a question.

With that out of the way, if you are automating SFTP transfers then you need to be using batch mode, and that has key-based authentication as a prerequisite. Certificates would work as well. You can specify the key (or a certificate instead) using the -i option and the batch script using the -b option. The script may be read either from stdin or from a file. Here is an example using stdin and a Here Document:

Code:
date=$(date +"%Y%m%d")

cat <<EOS | sftp -b - \
        -i /path/to/your/sshkeys/synerg_ed25519_key \
        (username)@server.example.com
cd /Home/nni_mktbi_synergistix/Samples/Synergistix/
lcd /opt/infa_shared/icm/Infa_Adm/TgtFiles/
get $date
EOS
Or you can put the SFTP commands in a file and read that instead of "-"

Last edited by Turbocapitalist; 07-12-2019 at 02:44 AM. Reason: sanitized example
 
1 members found this post helpful.
Old 07-12-2019, 05:10 AM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
My tcl is a bit rusty, this is untested.

set now [ clock seconds ]
set date [ clock format $now -format %Y%m%d ]

Please do not post personal information.
 
Old 07-12-2019, 07:15 AM   #4
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by vishinfy View Post
My task is =
copy a file from sftp and put on local machine. But am not able to pass variable in sftp mode. Here is the script
Code:
#!/usr/bin/expect -f
source /opt/infa_shared/icm/Infa_Adm/ParamFiles/ICM_param.param

set DATE="%Y%m%d"
expect -c 'spawn sftp (username)@conduit.novonordisk-us.com
expect "*password:"
--- Password is given here---
expect "sftp>"
send "cd /Home/nni_mktbi_synergistix/Samples/Synergistix/ \r"
expect "sftp>"
send "get $DATE \r"
expect "sftp>"
send "lcd /opt/infa_shared/icm/Infa_Adm/TgtFiles/ \r\n"
expect "sftp> "
send "quit \r"'
Error thrown here is
Code:
sftp> can't read "DATE": no such variable
    while executing
"send "get $DATE \r""
Please resolve. Tried other forums for solution but its not working!!
Since this is YOUR task, it's fairly rude of you to tell us to resolve it for you, especially since you say you've posted this on other forums too. And we certainly aren't going to call or email you personally with solutions...if you'd like one-on-one tech support, I'm sure there are lots of people who would be willing to provide that. Just tell us how much you're going to pay to have someone else perform your tasks for you.

Past that, if you're using SFTP, consider NOT using expect, since there's no need. Perform an SSH keyswap between the two systems, and eliminate the need to hard code a user ID and password into a script (which is VERY BAD from a security standpoint). Leaving you with something like:
Code:
sftp -o PasswordAuthentication=user@host <<< $'get /path/to/file/$DATE'
And where there's SFTP, chances are you also have SCP, which sidesteps this entire issue, since you can just issue:
Code:
scp /path/to/file/DATE /some/local/directory
 
Old 07-12-2019, 12:11 PM   #5
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
As others have said you can use sftp batch mode to accomplish this. Use of expect is good for interactive sessions such as telnet where you have to login, wait for a prompt, enter something, wait for another prompt, etc... Most file transfer protocols don't need it. Even with old fashioned ftp you could use "here document" coding and avoid expect.

Ideally for sftp you'd set up a trust wherein you send the public key from originating user/system to be imported into the authorized_keys of the destination user/system so wouldn't need the password.

If you absolutely have to use the password you could use sshpass. It may not already be installed so you might have to install it.
 
1 members found this post helpful.
Old 07-12-2019, 02:32 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
Novo Nordisk is a pharmaceutical company and conduit.novonordisk-us.com appears to be a portal to upload and download files. While suggesting using keys is a good idea it has nothing to do with the question. In addition we do not know anything about the website or what software is running so keys or scp may not be applicable.

Last edited by michaelk; 07-12-2019 at 03:17 PM.
 
Old 07-12-2019, 03:03 PM   #7
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
Quote:
Originally Posted by michaelk View Post
Novo Nordisk is a pharmaceutical company and conduit.novonordisk-us.com appears to be a portal to upload and download files. While suggesting using keys or sshpass is a good idea it has nothing to do with the question. In addition we do not know anything about the website or what software is running so keys or scp may not be applicable.
This post mystifies me. The OP clearly shows the expect script is doing sftp:
Code:
spawn sftp (username)@conduit.novonordisk-us.com
Keys are definitely germane to the discussion and sshpass is a viable tool to read in passwords with sftp (which lives on the ssh ecosystem) if the partner doesn't agree to use keys.

My organization does quite a bit of B2B here using various protocols and our preference is always sftp with keys. If we can't get the partner to use keys we use sshpass. If we absolutely have to we use lftp to do ftps (not sftp) or even "here documents" for the few remaining partners that still do ftp (not ftps or sftp) even though it is a security risk. We could talk about http/https get and post or AS2 as well but as noted above the OP is using sftp.

Last edited by MensaWater; 07-12-2019 at 03:06 PM.
 
Old 07-12-2019, 03:17 PM   #8
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,702

Rep: Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895Reputation: 5895
I did not say that anything was wrong with keys except that we do not know if the OP can use them. Sorry, I messed up my post and did not mean to suggest that sshpass was not a good solution and using it with batch mode in post #2 might be the OP easiest solution.

Last edited by michaelk; 07-12-2019 at 03:18 PM.
 
1 members found this post helpful.
  


Reply



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
[root@fugo trace]# sh expect.sh expect.sh: line 9: expect: command not found sivaloga Linux - Kernel 1 08-22-2013 04:29 AM
set variable in expect to copy file through sftp hbchok Linux - Newbie 10 11-03-2012 04:23 AM
SFTP throwing "End of file" when doing SFTP transfer in perl exceed1 Programming 5 03-06-2010 01:34 PM
how to set a variable in a script and have the variable still set after script termin john test Linux - Newbie 9 10-21-2008 05:21 PM
transfer file using expect and sftp tanveer Linux - General 7 05-16-2008 10:29 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 07:24 PM.

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
Open Source Consulting | Domain Registration