Differences
This shows you the differences between the selected revisions of the page.
| library_session_executecommand 2012-04-03 | library_session_executecommand 2022-06-16 (current) | ||
| Line 1: | Line 1: | ||
| ====== Session.ExecuteCommand Method ====== | ====== Session.ExecuteCommand Method ====== | ||
| Executes command on the remote server. | Executes command on the remote server. | ||
| - | |||
| - | &beta_feature | ||
| ===== Syntax ===== | ===== Syntax ===== | ||
| Line 10: | Line 8: | ||
| <code vbnet *> | <code vbnet *> | ||
| - | Public Function ExecuteCommand(ByVal command As String) As CommandExecutionResult | + | Public Function ExecuteCommand(command As String) As CommandExecutionResult |
| </code> | </code> | ||
| Line 18: | Line 16: | ||
| ==== Return Value ==== | ==== Return Value ==== | ||
| - | ''[[library_commandexecutionresult|CommandExecutionResult]]'' with command output. | + | ''[[library_commandexecutionresult|CommandExecutionResult]]'' with command output. See also [[library_session#results|Capturing results of operations]]. |
| ===== Exceptions ===== | ===== Exceptions ===== | ||
| Line 24: | Line 22: | ||
| | InvalidOperationException | Session is not opened. | | | InvalidOperationException | Session is not opened. | | ||
| | [[library_sessionlocalexception|SessionLocalException]] | Error communicating with ''[[executables#winscp.com|winscp.com]]''. \\ See the exception documentation for details. | | | [[library_sessionlocalexception|SessionLocalException]] | Error communicating with ''[[executables#winscp.com|winscp.com]]''. \\ See the exception documentation for details. | | ||
| - | | [[library_sessionremoteexception|SessionRemoteException]] | Session has failed. \\ Command has failed. \\ See the exception documentation for details. | | + | | [[library_sessionremoteexception|SessionRemoteException]] | Session has failed. \\ See the exception documentation for details. | |
| | TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | | TimeoutException | Timeout waiting for ''winscp.com'' to respond. | | ||
| - | ===== Remarks ===== | + | ===== [[remarks]] Remarks ===== |
| With [[protocols|SFTP and SCP protocols]], executes arbitrary [[remote_command|remote shell command]]. | With [[protocols|SFTP and SCP protocols]], executes arbitrary [[remote_command|remote shell command]]. | ||
| With FTP protocol, executes a protocol command. | With FTP protocol, executes a protocol command. | ||
| With SFTP protocol, that does not allow execution of arbitrary remote command, separate [[shell session]] will be automatically opened. | With SFTP protocol, that does not allow execution of arbitrary remote command, separate [[shell session]] will be automatically opened. | ||
| + | |||
| + | Not supported with WebDAV and S3 protocols. | ||
| The command must not require user input. | The command must not require user input. | ||
| - | ===== Example ===== | + | ===== [[example]] Example ===== |
| ==== [[csharp]] C# Example ==== | ==== [[csharp]] C# Example ==== | ||
| <code csharp> | <code csharp> | ||
| Line 48: | Line 49: | ||
| { | { | ||
| // Setup session options | // Setup session options | ||
| - | SessionOptions sessionOptions = new SessionOptions { | + | SessionOptions sessionOptions = new SessionOptions |
| + | ···········{ | ||
| Protocol = Protocol.Sftp, | Protocol = Protocol.Sftp, | ||
| HostName = "example.com", | HostName = "example.com", | ||
| UserName = "user", | UserName = "user", | ||
| Password = "mypassword", | Password = "mypassword", | ||
| - | SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| }; | }; | ||
| Line 61: | Line 63: | ||
| session.Open(sessionOptions); | session.Open(sessionOptions); | ||
| - | // Execute mysqldump on the server to dump all MySQL databases and compress the results | + | // Execute mysqldump on the server to dump all MySQL databases and |
| + | // compress the results | ||
| const string dbUsername = "USERNAME"; | const string dbUsername = "USERNAME"; | ||
| const string dbPassword = "PASSWORD"; | const string dbPassword = "PASSWORD"; | ||
| Line 67: | Line 70: | ||
| string dumpCommand = | string dumpCommand = | ||
| - | string.Format("mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}", | + | string.Format( |
| - | ·································dbUsername, dbPassword, tempFilePath); | + | ························"mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}", |
| - | session.ExecuteCommand(dumpCommand); | + | ·······················dbUsername, dbPassword, tempFilePath); |
| + | session.ExecuteCommand(dumpCommand).Check(); | ||
| // Download the database dump | // Download the database dump | ||
| - | session.GetFiles(tempFilePath, "D:\\dbbackup\\").Check(); | + | session.GetFiles(tempFilePath, @"D:\dbbackup\").Check(); |
| } | } | ||
| Line 88: | Line 92: | ||
| ==== [[vbnet]] VB.NET Example ==== | ==== [[vbnet]] VB.NET Example ==== | ||
| <code vbnet> | <code vbnet> | ||
| - | Imports System | ||
| Imports WinSCP | Imports WinSCP | ||
| Line 97: | Line 100: | ||
| Try | Try | ||
| ' Setup session options | ' Setup session options | ||
| - | Dim sessionOptions As New SessionOptions; | + | Dim sessionOptions As New SessionOptions |
| With sessionOptions | With sessionOptions | ||
| .Protocol = Protocol.Sftp | .Protocol = Protocol.Sftp | ||
| Line 103: | Line 106: | ||
| .UserName = "user" | .UserName = "user" | ||
| .Password = "mypassword" | .Password = "mypassword" | ||
| - | .SshHostKey = "ssh-rsa 1024 xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" | + | .SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." |
| End With | End With | ||
| - | Using session As Session = New Session | + | Using session As New Session |
| ' Connect | ' Connect | ||
| session.Open(sessionOptions) | session.Open(sessionOptions) | ||
| - | ' Execute mysqldump on the server to dump all MySQL databases and compress the results | + | ' Execute mysqldump on the server to dump all MySQL databases and |
| + | ' compress the results | ||
| Const dbUsername As String = "USERNAME" | Const dbUsername As String = "USERNAME" | ||
| Const dbPassword As String = "PASSWORD" | Const dbPassword As String = "PASSWORD" | ||
| Const tempFilePath As String = "/tmp/all_databases.gz" | Const tempFilePath As String = "/tmp/all_databases.gz" | ||
| - | ' Execute mysqldump on the server to dump all MySQL databases and compress the results | + | Dim dumpCommand As String = |
| - | ················Dim dumpCommand As String = _ | + | String.Format( |
| - | String.Format("mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}", _ | + | ························"mysqldump --opt -u {0} --password={1} --all-databases | gzip > {2}", |
| dbUsername, dbPassword, tempFilePath) | dbUsername, dbPassword, tempFilePath) | ||
| - | session.ExecuteCommand(dumpCommand) | + | session.ExecuteCommand(dumpCommand).Check() |
| ' Download the database dump | ' Download the database dump | ||
| - | session.GetFiles(tempFilePath, "D:\dbbackup\").Check | + | session.GetFiles(tempFilePath, "D:\dbbackup\").Check() |
| End Using | End Using | ||
| Line 134: | Line 138: | ||
| End Class | End Class | ||
| + | </code> | ||
| + | |||
| + | ==== [[powershell]] PowerShell Example ==== | ||
| + | Learn more about [[library_powershell|using WinSCP .NET assembly from PowerShell]]. | ||
| + | |||
| + | <code powershell> | ||
| + | try | ||
| + | { | ||
| + | # Load WinSCP .NET assembly | ||
| + | Add-Type -Path "WinSCPnet.dll" | ||
| + | |||
| + | # Setup session options | ||
| + | $sessionOptions = New-Object WinSCP.SessionOptions -Property @{ | ||
| + | Protocol = [WinSCP.Protocol]::Sftp | ||
| + | HostName = "example.com" | ||
| + | UserName = "user" | ||
| + | Password = "mypassword" | ||
| + | SshHostKeyFingerprint = "ssh-rsa 2048 xxxxxxxxxxx..." | ||
| + | } | ||
| + | |||
| + | $session = New-Object WinSCP.Session | ||
| + | |||
| + | try | ||
| + | { | ||
| + | # Connect | ||
| + | $session.Open($sessionOptions) | ||
| + | |||
| + | # Execute mysqldump on the server to dump all MySQL databases and compress the results | ||
| + | $dbUsername = "USERNAME" | ||
| + | $dbPassword = "PASSWORD" | ||
| + | $tempFilePath = "/tmp/all_databases.gz" | ||
| + | |||
| + | $dumpCommand = | ||
| + | "mysqldump --opt -u $dbUsername --password=$dbPassword --all-databases | " + | ||
| + | "gzip > $tempFilePath" | ||
| + | $session.ExecuteCommand($dumpCommand).Check() | ||
| + | |||
| + | # Download the database dump | ||
| + | $session.GetFiles($tempFilePath, "D:\dbbackup\").Check() | ||
| + | |||
| + | finally | ||
| + | { | ||
| + | # Disconnect, clean up | ||
| + | $session.Dispose() | ||
| + | } | ||
| + | |||
| + | exit 0 | ||
| + | } | ||
| + | catch | ||
| + | { | ||
| + | Write-Host "Error: $($_.Exception.Message)" | ||
| + | exit 1 | ||
| + | } | ||
| </code> | </code> | ||