Solved

Scripting SFTP file transfer

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

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 55

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
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.

 
LVL 55

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 55

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 55

Expert Comment

by:Bill Prew
ID: 39221726
Thanks.

~bp
0

Featured Post

Industry Leaders: 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!

Question has a verified solution.

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

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
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 lists in Python. Lists, as their name suggests, are a means for ordering and storing values. : Lists are declared using brackets; for example: t = [1, 2, 3]: Lists may contain a mix of data types; for example: t = ['string', 1, T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

705 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