Chilkat HOME .NET Core C# Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi ActiveX Delphi DLL Go Java Lianja Mono C# Node.js Objective-C PHP ActiveX PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift 2 Swift 3,4,5... Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(PowerBuilder) SSH Parallel Remote Commands on Multiple ServersSee more SSH ExamplesShows how to execute a command in parallel on multiple servers.
integer li_rc oleobject loo_Ssh1 oleobject loo_Ssh2 oleobject loo_Ssh3 integer li_Port integer li_Success string ls_Cmd integer li_Ssh1Channel integer li_Ssh2Channel integer li_Ssh3Channel integer li_PollTimeoutMs integer li_NumFinished integer li_Channel integer li_Ssh1Finished integer li_Ssh2Finished integer li_Ssh3Finished // This example requires the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. // Executing a command on multiple servers simultaneously is straightforward. // It's just a matter of using one SSH object per server.. loo_Ssh1 = create oleobject // Use "Chilkat_9_5_0.Ssh" for versions of Chilkat < 10.0.0 li_rc = loo_Ssh1.ConnectToNewObject("Chilkat.Ssh") if li_rc < 0 then destroy loo_Ssh1 MessageBox("Error","Connecting to COM object failed") return end if loo_Ssh2 = create oleobject // Use "Chilkat_9_5_0.Ssh" for versions of Chilkat < 10.0.0 li_rc = loo_Ssh2.ConnectToNewObject("Chilkat.Ssh") loo_Ssh3 = create oleobject // Use "Chilkat_9_5_0.Ssh" for versions of Chilkat < 10.0.0 li_rc = loo_Ssh3.ConnectToNewObject("Chilkat.Ssh") li_Port = 22 li_Success = loo_Ssh1.Connect("ssh-server1.com",li_Port) if li_Success <> 1 then Write-Debug loo_Ssh1.LastErrorText destroy loo_Ssh1 destroy loo_Ssh2 destroy loo_Ssh3 return end if // Authenticate using login/password: li_Success = loo_Ssh1.AuthenticatePw("sshLogin1","sshPassword1") if li_Success <> 1 then Write-Debug loo_Ssh1.LastErrorText destroy loo_Ssh1 destroy loo_Ssh2 destroy loo_Ssh3 return end if // Connect and authenticate with 2 more servers. // For brevity, the success/failure won't be checked... li_Success = loo_Ssh2.Connect("ssh-server2.com",li_Port) li_Success = loo_Ssh2.AuthenticatePw("sshLogin2","sshPassword2") li_Success = loo_Ssh3.Connect("ssh-server3.com",li_Port) li_Success = loo_Ssh3.AuthenticatePw("sshLogin3","sshPassword3") // Note: If we wanted, we could've used ConnectAsync and AuthenticatePwAsync // to do the connecting and authenticating in parallel... // The command to be run on each SSH server will sleep for 5 seconds, // and then show the current system date/time. ls_Cmd = "sleep 5; date" // Start each command li_Ssh1Channel = loo_Ssh1.QuickCmdSend(ls_Cmd) if li_Ssh1Channel < 0 then Write-Debug loo_Ssh1.LastErrorText destroy loo_Ssh1 destroy loo_Ssh2 destroy loo_Ssh3 return end if // For brevity, we're not checking the return values here: li_Ssh2Channel = loo_Ssh2.QuickCmdSend(ls_Cmd) li_Ssh3Channel = loo_Ssh3.QuickCmdSend(ls_Cmd) // OK, at this point the command is running simultaneously on each server. // Now collect the results of each command. li_PollTimeoutMs = 50 li_NumFinished = 0 // Note: You would rewrite this code to use arrays. li_Ssh1Finished = 0 li_Ssh2Finished = 0 li_Ssh3Finished = 0 do while li_NumFinished < 3 // Check to see if anything has finished. // QuickCmdCheck returns -1 if there are no errors and nothing else finished // QuickCmdCheck returns -2 if there was an error (such as a lost connection) // QuickCmdCheck returns a channel number if a channel finished. if li_Ssh1Finished <> 1 then li_Channel = loo_Ssh1.QuickCmdCheck(li_PollTimeoutMs) if li_Channel = -2 then Write-Debug loo_Ssh1.LastErrorText destroy loo_Ssh1 destroy loo_Ssh2 destroy loo_Ssh3 return end if if li_Channel = li_Ssh1Channel then Write-Debug "---- ssh1 channel " + string(li_Channel) + " finished ----" Write-Debug loo_Ssh1.GetReceivedText(li_Channel,"ansi") li_NumFinished = li_NumFinished + 1 li_Ssh1Finished = 1 end if end if if li_Ssh2Finished <> 1 then li_Channel = loo_Ssh2.QuickCmdCheck(li_PollTimeoutMs) if li_Channel = -2 then Write-Debug loo_Ssh2.LastErrorText destroy loo_Ssh1 destroy loo_Ssh2 destroy loo_Ssh3 return end if if li_Channel = li_Ssh2Channel then Write-Debug "---- ssh2 channel " + string(li_Channel) + " finished ----" Write-Debug loo_Ssh2.GetReceivedText(li_Channel,"ansi") li_NumFinished = li_NumFinished + 1 li_Ssh2Finished = 1 end if end if if li_Ssh3Finished <> 1 then li_Channel = loo_Ssh3.QuickCmdCheck(li_PollTimeoutMs) if li_Channel = -2 then Write-Debug loo_Ssh3.LastErrorText destroy loo_Ssh1 destroy loo_Ssh2 destroy loo_Ssh3 return end if if li_Channel = li_Ssh3Channel then Write-Debug "---- ssh3 channel " + string(li_Channel) + " finished ----" Write-Debug loo_Ssh3.GetReceivedText(li_Channel,"ansi") li_NumFinished = li_NumFinished + 1 li_Ssh3Finished = 1 end if end if loop // -------------- // Sample output: // ---- ssh2 channel 101 finished ---- // Fri Dec 23 00:25:48 UTC 2016 // // ---- ssh3 channel 102 finished ---- // Thu Dec 22 18:25:12 CST 2016 // // ---- ssh1 channel 100 finished ---- // Thu Dec 22 18:25:48 CST 2016 destroy loo_Ssh1 destroy loo_Ssh2 destroy loo_Ssh3 |
© 2000-2024 Chilkat Software, Inc. All Rights Reserved.