Solved

Scripting SFTP file transfer

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

Expert Comment

by:Bill Prew
Comment Utility
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
Comment Utility
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 51

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
Comment Utility
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 51

Assisted Solution

by:Bill Prew
Bill Prew earned 500 total points
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Accepted Solution

by:
First Last earned 0 total points
Comment Utility
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 51

Expert Comment

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

~bp
0
 
LVL 1

Author Comment

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

Author Closing Comment

by:First Last
Comment Utility
Best solution
0
 
LVL 51

Expert Comment

by:Bill Prew
Comment Utility
Thanks.

~bp
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
The viewer will learn how to count occurrences of each item in an array.

763 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

13 Experts available now in Live!

Get 1:1 Help Now