Execute commands through Telnet in Java

Source: Internet
Author: User

 

/*** The commons-net-2.0.jar is the project dependency package */import Java. io. inputstream; import Java. io. printstream; import org.apache.commons.net. telnet. telnetclient; public class nettelnet {private telnetclient Telnet = new telnetclient (); Private inputstream in; private printstream out; private char prompt = '$ '; // normal user ends public nettelnet (string IP, int port, string user, string password) {try {Telnet. connect (IP, Port); In = teln Et. getinputstream (); out = new printstream (Telnet. getoutputstream (); // Based on the root user, set the terminator this. Prompt = user. Equals ("root ")? '#': '$'; Login (user, password);} catch (exception e) {e. printstacktrace () ;}/ ***** log on ** @ Param user * @ Param password */Public void login (string user, string password) {readuntil ("login: "); write (User); readuntil (" Password: "); write (password); readuntil (prompt + "");} /*** read the analysis result ** @ Param pattern * @ return */Public String readuntil (string pattern) {try {char lastchar = pattern. charat (pattern. length ()-1); stringbuffer sb = new stringbuffer (); char CH = (char) in. read (); While (true) {sb. append (CH); If (CH = lastchar) {If (sb. tostring (). endswith (pattern) {return sb. tostring () ;}} CH = (char) in. read () ;}} catch (exception e) {e. printstacktrace ();} return NULL;}/*** write operation ** @ Param value */Public void write (string value) {try {out. println (value); out. flush ();} catch (exception e) {e. printstacktrace () ;}}/*** send the command string ** @ Param command * @ return */Public String sendcommand (string command) {try {write (command ); return readuntil (prompt + "");} catch (exception e) {e. printstacktrace ();} return NULL;}/*** close connection */Public void disconnect () {try {Telnet. disconnect ();} catch (exception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {try {system. out. println ("START Telnet... "); string IP =" 192.168.0.11 "; int Port = 23; string user =" user "; string Password =" 111111 "; nettelnet Telnet = new nettelnet (IP, port, user, password); Telnet. sendcommand ("Export lang = en"); string R1 = Telnet. sendcommand ("CD/home/project/"); string r2 = Telnet. sendcommand ("PWD"); string R3 = Telnet. sendcommand ("Sh. sh "); system. out. println ("display result"); system. out. println (R1); system. out. println (R2); system. out. println (r3); Telnet. disconnect ();} catch (exception e) {e. printstacktrace ();}}}

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.