Java remote to Windows machine execution DOS command via Telnet

Source: Internet
Author: User

Ready to work, Telnet service on remote Windows machine, add Telnet user to telnetclients user group

Core code:

Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.PrintStream;
Import java.io.UnsupportedEncodingException;
Import org.apache.commons.net.telnet.TelnetClient;
Import Org.testng.annotations.Test;

/**
*
* @author Zhuwei
*
*/
public class Telnetutils {

/**
* End the identity string, which is in >,linux in Windows #
*/
Private String prompt = ">";

/**
* End Identification character
*/
Private char PromptChar = ' > ';

/**
* Telnetclient Object
*/
Private Telnetclient Telnet;

/**
* InputStream input stream, receive return information
*/
Private InputStream in;

/**
* PrintStream write command to server
*/
Private PrintStream out;

/**
* Constructor function
* @param termtype
* @param prompt
*/
Public Telnetutils (String termtype,string prompt) {
This.telnet = new Telnetclient (TermType);
Setprompt (prompt);
}

/**
* Constructor function
* @param termtype
*/
Public telnetutils (String termtype) {
This.telnet = new Telnetclient (TermType);
}

/**
* Constructor function
*/
Public Telnetutils () {
telnet = new Telnetclient ();
}

/**
* Log on to the target host
* @param IP IP Address
* @param Port Port number
* @param username User name
* @param password Password
*/
public void Login (string ip, int port, string Username, string password) {
try {
Telnet.connect (IP, port);
in = Telnet.getinputstream ();
out = new PrintStream (Telnet.getoutputstream ());
Readuntil ("Login:");
Write (username);
Readuntil ("Password:");
Write (password);
String rs = readuntil (null);
if (Rs!=null&&rs.contains ("Login Failed")) {
throw new RuntimeException ("Login Failed");
}
} catch (Exception e) {
throw new RuntimeException (e);
}
}

/**
* Read Analysis results
* Returns the result when @param pattern matches to the string
* @return String
*/
public string Readuntil (string pattern) {
StringBuffer sb = new StringBuffer ();
try {
Char Lastchar = (char)-1;
Boolean flag = Pattern!=null&&pattern.length () >0;
if (flag)
Lastchar = Pattern.charat (Pattern.length ()-1);
Char ch;
int code =-1;
while ((Code = In.read ())! =-1) {
ch = (char) code;
Sb.append (CH);
Return results when matching to end identity
if (flag) {
if (ch = = Lastchar && sb.tostring (). EndsWith (pattern)) {
return sb.tostring ();
}
}else{
If no end identification is specified, the result is returned when matching to the default end identifier character
if (ch = = PromptChar)
return sb.tostring ();
}
Return results when login fails
if (Sb.tostring (). Contains ("Login Failed")) {
return sb.tostring ();
}
}
} catch (Exception e) {
E.printstacktrace ();
}
return sb.tostring ();
}

/**
* Send command
* @param value
*/
public void Write (String value) {
try {
Out.println (value);
Out.flush ();
} catch (Exception e) {
E.printstacktrace ();
}
}

/**
*
* Function Description: Send command, return execution result
*
* Input Parameters: command Telnet commands
*
* Output parameters: String Command Execution result
* @throws unsupportedencodingexception
*
*
*/
public string SendCommand (String command) throws Unsupportedencodingexception {
try {
Write (command);
return Readuntil (Prompt);
} catch (Exception e) {
E.printstacktrace ();
}
Transcode The returned result to GBK, otherwise it will be garbled
String serverresult = new String (Readuntil (Prompt). GetBytes ("Iso-8859-1"), "GBK");
return serverresult;
}

/**
* Function Description: Turn off Telnet connection
*
* Input Parameters:
*
* Output Parameters:
*/
public void distinct () {
try {
if (telnet!=null&&!telnet.isconnected ())
Telnet.disconnect ();
} catch (IOException e) {
E.printstacktrace ();
}
}

/**
* Function Description: Set End identifier
*
* Input parameters: Prompt End identifier
*
* Output Parameters:
*
*/
public void Setprompt (String prompt) {
if (prompt!=null) {
This.prompt = prompt;
This.promptchar = Prompt.charat (Prompt.length ()-1);
}
}

/**
* Function Description: Test function
*
* Input parameters: args
*
* Output Parameters:
* @throws unsupportedencodingexception
*/

public string Excutewincmd (string remotehost,string port,string username,string password,string wincmd) throws unsupportedencodingexception{
Login (RemoteHost, integer.valueof (Port), UserName, PassWord);
String Serverresult = SendCommand (wincmd);
Distinct ();
return serverresult;
}

@Test
public void Test1 () throws unsupportedencodingexception{

Windows, use VT220, otherwise it will garbled
Telnetutils telnetutils = new Telnetutils ("VT220", ">");
String result = Telnetutils.excutewincmd ("172.19.21.216", "23°c", "xxxx", "xxxx", "taskkill/f/t/im chrome.exe");
SYSTEM.OUT.PRINTLN (result);
}
}

Java remote to Windows machine execution DOS command via Telnet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.