/*** 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 ();}}}