![]() |
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
(Java) 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".
import com.chilkatsoft.*; public class ChilkatExample { static { try { System.loadLibrary("chilkat"); } catch (UnsatisfiedLinkError e) { System.err.println("Native code library failed to load.\n" + e); System.exit(1); } } public static void main(String argv[]) { // This example assumes the Chilkat API to have been previously unlocked. // See Global Unlock Sample for sample code. CkSsh ssh = new CkSsh(); // Hostname may be an IP address or hostname: String hostname = "172.16.16.46"; int port = 22; boolean success = ssh.Connect(hostname,port); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // If receiving output from a command stalls for more than 5 seconds, then stop waiting. ssh.put_IdleTimeoutMs(5000); success = ssh.AuthenticatePw("myLogin","myPassword"); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Open a session channel. int channelNum = ssh.OpenSessionChannel(); if (channelNum < 0) { System.out.println(ssh.lastErrorText()); return; } // Request a pseudo-terminal String termType = "dumb"; int widthInChars = 120; int heightInChars = 40; int pixWidth = 0; int pixHeight = 0; success = ssh.SendReqPty(channelNum,termType,widthInChars,heightInChars,pixWidth,pixHeight); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Start a shell on the channel: success = ssh.SendReqShell(channelNum); if (success != true) { System.out.println(ssh.lastErrorText()); 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.) String cmd = "su\n"; success = ssh.ChannelSendString(channelNum,cmd,"ansi"); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Read until we get the prompt for the password: success = ssh.ChannelReceiveUntilMatch(channelNum,"Password:","ansi",true); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Display what we've received so far: System.out.println(ssh.getReceivedText(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. String password; password = "myPassword\n"; success = ssh.ChannelSendString(channelNum,password,"ansi"); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Read the response until we get the shell prompt (assuming it's successful) // In my case, the shell prompt is: "chilkat@vivoMini64:~$ " String myShellPrompt = "chilkat@vivoMini64:~$"; // It will be different in your case. success = ssh.ChannelReceiveUntilMatch(channelNum,myShellPrompt,"ansi",true); if (success != true) { // Check the last-error information and the session log... System.out.println(ssh.lastErrorText()); // Check to see what was received. System.out.println(ssh.getReceivedText(channelNum,"ansi")); 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... System.out.println(ssh.getReceivedText(channelNum,"ansi")); // Send a command. In this case, we are sending the "ls" command: cmd = "ls\n"; success = ssh.ChannelSendString(channelNum,cmd,"ansi"); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Read until the next command prompt: success = ssh.ChannelReceiveUntilMatch(channelNum,myShellPrompt,"ansi",true); if (success != true) { // Check the last-error information and the session log... System.out.println(ssh.lastErrorText()); // Check to see what was received. System.out.println(ssh.getReceivedText(channelNum,"ansi")); return; } // Display the command output: System.out.println(ssh.getReceivedText(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 = ssh.ChannelSendEof(channelNum); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Close the channel: success = ssh.ChannelSendClose(channelNum); if (success != true) { System.out.println(ssh.lastErrorText()); return; } // Disconnect ssh.Disconnect(); } } |
||||
© 2000-2026 Chilkat Software, Inc. All Rights Reserved.