Solved

Scripting SFTP file transfer

Posted on 2013-05-31
9
1,332 Views
Last Modified: 2013-06-05
Hello experts - I don't have much experience with scripting and have been asked to create one to transfer files over a SFTP connection to a local drive on a windows server, rename the files, then alert me when the task completes.  I've tested the connection which works fine using WinSCP.  There is a scripting function within the application but I don't know if that's the best way to approach this.  If it could be done via the command line perhaps that is a better option.  Please let me know what you have any ideas  about how to complete the task, syntax help greatly appreciated!
0
Comment
Question by:First Last
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39211276
There are a number of ways you could approach this.  If you want a fairly easy solution you might want to invest in RoboFTP.  It's not free, but the scripting language is quite robust and there are some good experts here that know it well.

http://www.robo-ftp.com/

You can accomplish this with WinSCP as well, but you will have to work a little harder I believe.  I think there are a couple of approaches there.  You can drive the process from a BAT script file, and use the older text command script file approach of WinSCP to execute the steps needed for the transfer and rename, etc.  You won't be able to email from WinSCP though, and from a BAT file to send email you need something like the free BLAT tool. There are many examples of using BLAT from a BAT script here on EE that you can search for.

http://winscp.net/eng/docs/scripting#using_scripting
http://www.blat.net/

In addition, WinSCP supports a newer type of scripting which is more powerful, but a bit more complicated.  But it might allow you to do the file transfer from the control of say a VBS script, and then also send an email from the VBS script after the transfer is done.

http://winscp.net/eng/docs/library_com_wsh#vbscript

~bp
0
 
LVL 1

Author Comment

by:First Last
ID: 39211429
Ok, I read through a bunch of that and I think this can be done easily enough with WinSCP for free.  I just need some help with the syntax.  So there are two files I need to move from this SFTP site once a day.  I was thinking using a scheduled task to call winscp.com with something like these parameters might work:

winscp.com /command "option batch abort" "option confirm off" "open SFTPsite.net" "get examplefile.txt /home/user/" "exit"

But I'm missing info there.  I need to pass a username/password and specify a particular directory to copy the files to.  I can worry about renaming them with another script later.  Any ideas on what the proper syntax would be to authenticate and for the directory?
0
 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 39211706
You should be able to pass the user credentials on the OPEN command, try either of these and see if it helps:

open myuser:mypassword@SFTPsite.net

open sftp://myuser:mypassword@SFTPsite.net

~bp
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 54

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
ID: 39211714
If you need to change the directory on the client side, you can use the LCD command.  If you need to change directory on the server side you can use CD command.  Keep in mind that if you do these on the command line as you are now, and you have spaces in the pathnames, you need to double-double quote them, such as below.  If there are no enmbedded spaces then you don't need the extra quotes as in the second example.

winscp.com /command "option batch abort" "option confirm off" "open SFTPsite.net" "cd ""/my dir1/my dir2/my dir3""" "get examplefile.txt /home/user/" "exit"

winscp.com /command "option batch abort" "option confirm off" "open SFTPsite.net" "cd /mydir1/mydir2/mydir3" "get examplefile.txt /home/user/" "exit"

Open in new window

~bp
0
 
LVL 1

Accepted Solution

by:
First Last earned 0 total points
ID: 39211860
I was able to get it working like this:

try
{
    # Load WinSCP .NET assembly
    # Use "winscp.dll" for the releases before the latest beta version.
    [Reflection.Assembly]::LoadFrom("C:\Program Files (x86)\WinSCP\WinSCP.dll") | Out-Null
 
    # Setup session options
    $sessionOptions = New-Object WinSCP.SessionOptions
    $sessionOptions.Protocol = [WinSCP.Protocol]::Sftp
    $sessionOptions.HostName = "SITE.NET"
    $sessionOptions.UserName = "USER"
    $sessionOptions.Password = "PW"
    $sessionOptions.SshHostKeyFingerprint = "ssh-rsa 1024 XX:XX:XX etc"
 
    $session = New-Object WinSCP.Session
 
    try
    {
        # Connect
        $session.Open($sessionOptions)
 
        $localPath = "C:\File\"
        $remotePath = "/Inbox/"
        $file = "file.txt"
 
        # Format timestamp
        $stamp = $(Get-Date -f "yyyyMMddHHmmss")
 
        # Download the file and throw on any error
        $session.GetFiles(
            ($remotePath + $file),
            ($localPath + $stamp + "." + $file)).Check()
    }
    finally
    {
        # Disconnect, clean up
        $session.Dispose()
    }
 
    exit 0
}
catch [Exception]
{
    Write-Host $_.Exception.Message
    exit 1
}
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39211932
Awe, you were holding out Dan, had some .Net skills hiding back there I see :-).

~bp
0
 
LVL 1

Author Comment

by:First Last
ID: 39211935
:)  Wish I could take credit, I grabbed some sample code and filled in the blanks!
0
 
LVL 1

Author Closing Comment

by:First Last
ID: 39221516
Best solution
0
 
LVL 54

Expert Comment

by:Bill Prew
ID: 39221726
Thanks.

~bp
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question