![]() |
Chilkat HOME Android™ AutoIt C C# C++ Chilkat2-Python CkPython Classic ASP DataFlex Delphi DLL Go Java JavaScript Node.js Objective-C PHP Extension Perl PowerBuilder PowerShell PureBasic Ruby SQL Server Swift Tcl Unicode C Unicode C++ VB.NET VBScript Visual Basic 6.0 Visual FoxPro Xojo Plugin
(C) SSH -- Running Commands that Prompt for Additional Input, such as "su"Demonstrates how to run a shell command via SSH where the shell command prompts for additional input from the client. This example demonstrates "su".
#include <C_CkSsh.h> void ChilkatSample(void) { HCkSsh ssh; const char *hostname; int port; BOOL success; int channelNum; const char *termType; int widthInChars; int heightInChars; int pixWidth; int pixHeight; const char *cmd; const char *password; const char *myShellPrompt; // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. ssh = CkSsh_Create(); // Hostname may be an IP address or hostname: hostname = "172.16.16.46"; port = 22; success = CkSsh_Connect(ssh,hostname,port); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // If receiving output from a command stalls for more than 5 seconds, then stop waiting. CkSsh_putIdleTimeoutMs(ssh,5000); success = CkSsh_AuthenticatePw(ssh,"myLogin","myPassword"); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Open a session channel. channelNum = CkSsh_OpenSessionChannel(ssh); if (channelNum < 0) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Request a pseudo-terminal termType = "dumb"; widthInChars = 120; heightInChars = 40; pixWidth = 0; pixHeight = 0; success = CkSsh_SendReqPty(ssh,channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Start a shell on the channel: success = CkSsh_SendReqShell(ssh,channelNum); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Send the su command. // (The SSH server I'm using for testing is a Linux Ubuntu // system running OpenSSH. It is important in this case to send a bare-LF // and not a CRLF.) cmd = "su\n"; success = CkSsh_ChannelSendString(ssh,channelNum,cmd,"ansi"); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Read until we get the prompt for the password: success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,"Password:","ansi",TRUE); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Display what we've received so far: printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi")); // Send the password. (This is the equivalent of typing at the shell prompt.) // Again, make sure it uses a bare-LF and not a CRLF. password = "myPassword\n"; success = CkSsh_ChannelSendString(ssh,channelNum,password,"ansi"); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Read the response until we get the shell prompt (assuming it's successful) // In my case, the shell prompt is: "chilkat@vivoMini64:~$ " myShellPrompt = "chilkat@vivoMini64:~$"; // It will be different in your case. success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,myShellPrompt,"ansi",TRUE); if (success != TRUE) { // Check the last-error information and the session log... printf("%s\n",CkSsh_lastErrorText(ssh)); // Check to see what was received. printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi")); CkSsh_Dispose(ssh); return; } // Display what we've received so far. This clears // the internal receive buffer, which is important. // After we send the command, we'll be reading until // the next command prompt. If the command prompt // is already in the internal receive buffer, we'll think we're // already finished... printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi")); // Send a command. In this case, we are sending the "ls" command: cmd = "ls\n"; success = CkSsh_ChannelSendString(ssh,channelNum,cmd,"ansi"); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Read until the next command prompt: success = CkSsh_ChannelReceiveUntilMatch(ssh,channelNum,myShellPrompt,"ansi",TRUE); if (success != TRUE) { // Check the last-error information and the session log... printf("%s\n",CkSsh_lastErrorText(ssh)); // Check to see what was received. printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi")); CkSsh_Dispose(ssh); return; } // Display the command output: printf("%s\n",CkSsh_getReceivedText(ssh,channelNum,"ansi")); // You may continue sending additional commands. // The technique is: send the command, read until the next command prompt, // and then fetch/clear the internal receive buffer. // We're done, so shut it down.. // Send an EOF. This tells the server that no more data will // be sent on this channel. The channel remains open, and // the SSH client may still receive output on this channel. success = CkSsh_ChannelSendEof(ssh,channelNum); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Close the channel: success = CkSsh_ChannelSendClose(ssh,channelNum); if (success != TRUE) { printf("%s\n",CkSsh_lastErrorText(ssh)); CkSsh_Dispose(ssh); return; } // Disconnect CkSsh_Disconnect(ssh); CkSsh_Dispose(ssh); } |
||||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.