Welcome to the VanDyke Software Forums

Join the discussion today!


Go Back   VanDyke Software Forums > Scripting

Notices

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 09-17-2014, 03:48 PM
secureus secureus is offline
Registered User
 
Join Date: Sep 2014
Posts: 14
Scripting - Basic file transfer local-to-Remote

I took this sample code from JDev Posting : https://forums.vandyke.com/showthread.php?t=10323


Sample code works fine for downloading the file from sftp-linux server to my local computer. I am trying to upload the file.

I am using vralib for 64bit / VS 2012. This is basic, file transfer local-to-remote and remote-to-local. I am using IFileSystemObject.

Is there any code samples which will allow me to send the file from local computer to server.

Code:
static void Main(string[] args)
        {
            // For evaluation of VRALib, this code is needed; it does not cause any problems
            // for license/registered VRALib installations, so it can be left in place even
            // after you license VRALib:


            var objLicense = new vralib.License();
            string strEvalLeft = objLicense.AcceptEvaluationLicense();
            if (strEvalLeft.Contains(@"days remaining"))
                Console.WriteLine(strEvalLeft);

            // Create a VRAlib Connection object we'll use to control
            // the process of connecting via SSH to the remote system
            
            vralib.Connection objConn = new vralib.Connection();

            // Enable debug logging for assisting troubleshooting efforts
            objConn.DebugLogFile = @"C:\##vralib-Download-Dbg-Log.txt";
            objConn.DebugLevel = 5;

            objConn.Hostname = @"myserver.com";
            objConn.Port = 22;
            objConn.Username = @"myusername";


            // For public/private key authentication, use this (obviously, you'll
            // need to create two variables -- PrivateKeyPath, which contains the full
            // path to the private/public key file; and PrivateKeyPassphrase, which
            // contains the passphrase used to unlock/decrypt the private key for use.
            // Consult the VRALib documenation or more information.
            // objConn.SetPrivateKeyFile(PrivateKeyPath, PrivateKeyPassphrase);

            // Otherwise, just use plain old password auth
            objConn.Password = @"mypassword";

            // Now Connect to the remote system.  Wrap this in a try/catch block to
            // handle any connection failures "gracefully":

            Console.WriteLine("Connecting to " + objConn.Hostname + ":" + objConn.Port + "...");
            try
            {
                // When the Connection object has all the necessary connection info
                // already set in the form of properties (e.g. objConn.Hostname,
                // objConn.Username, objConn.Password, etc.), all that is needed is to call
                // Connect with an empty string as the first param, and 'false' as the second
                // param (StartAllForwards is a setting that doesn't apply to file transfer
                // connections).  See the VRALib help documentation .chm file for additional
                // details about the Connection object's Connect() method.
                objConn.Connect("", true);
            }
            catch (System.Runtime.InteropServices.COMException objException)
            {
                Console.WriteLine("Failed to connect:\r\n" + objException.ToString());
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                System.Environment.ExitCode = objException.ErrorCode;
                return;
            }

            Console.WriteLine(
                "Connected and authenticated to " + objConn.Hostname +
                " (" + objConn.RemoteIdentString + ")");

            // Get a reference to a file system object for the remote host
            vralib.IFileSystemObject objRemoteFS = objConn.FileSystemObject;

                       
            // Download remote files (*.txt) to a local folder (C:\Temp\Incoming\)

            string strSrc = @"D:\SecureFX\Sample File Transfer\TestFile.txt";
            string strDst = "/home/admin/newfolder/";

            Console.WriteLine(@"Uploading '" + strSrc + "' To " +
                objConn.Hostname + " to '" + strDst + "'");
            try
            {
                objRemoteFS.Get(strSrc, strDst);
            }


            catch (System.Runtime.InteropServices.COMException objException)
            {
                Console.WriteLine("Transfer failed:\r\n" + objException.ToString());
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                System.Environment.ExitCode = objException.ErrorCode;
                return;
            }
            catch (System.IO.FileNotFoundException objException)
            {
                Console.WriteLine("Transfer failed:\r\n" + objException.ToString());
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                System.Environment.ExitCode = 2;
                return;
            }
            catch (Exception objException)
            {
                Console.WriteLine("Transfer failed:\r\n" + objException.ToString());
                Console.WriteLine("Press any key to continue.");
                Console.ReadKey();
                System.Environment.ExitCode = -9999999;
                return;
            }

            Console.WriteLine("Transfer succeeded.\r\n\r\nPress any key to contine.");
            Console.ReadKey();


        }
Reply With Quote
  #2  
Old 09-17-2014, 04:19 PM
jdev's Avatar
jdev jdev is offline
VanDyke Technical Support
 
Join Date: Nov 2003
Location: Albuquerque, NM
Posts: 1,099
Although the client is the entity that always initiates a connection (to a server), files can be transferred in either direction once a connection is established and authenticated:

Given that you have an example of a download (with Get()) , it should be very similar to upload (with Put()).

Get() = download (from server to client)
Put() = upload (from client to server)

All the methods available to you are documented in the VRALib sections of the ClientPack help file (ClientPack.chm) which is installed with ClientPack (see the attached graphic).

I've attached an <Upload> example that you'll notice is very similar to the existing code you already have, but uses Put() instead of Get() and sets up source/destination to match the direction.

So that you and other readers have both examples in one place (for upload/download), I've attached both upload/download C# examples here.

--Jake
Attached Images
File Type: png VRALibIFSO_Put.png (110.4 KB, 426 views)
Attached Files
File Type: txt VRALibDownloadExample.cs.txt (5.0 KB, 390 views)
File Type: txt VRALibUploadExample.cs.txt (5.0 KB, 363 views)
__________________
Jake Devenport
VanDyke Software
Technical Support
YouTube Channel: https://www.youtube.com/vandykesoftware
Email: support@vandyke.com
Web: https://www.vandyke.com/support
Reply With Quote
  #3  
Old 09-17-2014, 04:36 PM
secureus secureus is offline
Registered User
 
Join Date: Sep 2014
Posts: 14
Jake , Thank you for your help.
That works !!
Reply With Quote
Reply

Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -6. The time now is 03:47 PM.