Solved

Scripting SFTP file transfer

Posted on 2013-05-31
9
1,276 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
  • 5
  • 4
9 Comments
 
LVL 52

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 52

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
 
LVL 52

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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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 52

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 52

Expert Comment

by:Bill Prew
ID: 39221726
Thanks.

~bp
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

912 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now