Solved

Scripting SFTP file transfer

Posted on 2013-05-31
9
1,313 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 53

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 53

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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 
LVL 53

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 53

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 53

Expert Comment

by:Bill Prew
ID: 39221726
Thanks.

~bp
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

It is a general practice to get rid of old user profiles on a computer  in a LAN environment. As I have been working with a company in a LAN environment where users move from one place to some other place at times. This will make many user profil…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

860 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