Java Telnet SSH implementation

Source: Internet
Author: User
Tags ssh port telnet program dns spoofing

Let's take a look at the definitions of Telnet and SSH:

The telnet protocol is a member of the TCP/IP protocol and is the standard protocol and main method of the Internet remote login service. It provides users with the ability to complete remote host work on local computers. Use the telnet program on the terminal user's computer to connect to the server. End users can enter commands in the telnet program. These commands will run on the server, just as they are directly entered on the server console. You can control the server locally. To start a telnet session, you must enter the user name and password to log on to the server. Telnet is a common method to remotely control web servers.

SSH is a toolkit used to replace telnet, FTP, and r commands. It mainly aims to solve the problem of plaintext transmission of passwords on the Internet. To ensure system security and users' own rights and interests, it is necessary to promote ssh. SSH is short for the English Secure Shell. By using SSH, You can encrypt all transmitted data, so that the "man-in-the-middle" attack method is impossible, and it can also prevent DNS Spoofing and IP spoofing.

In fact, the Telnet and SSH functions are basically the same, and they are all network protocols for remote login.

Like most other protocols, the implementation of Telnet SSH in Java also goes through the trilogy: establishing connections, using connections, and releasing connections. SSH is a protocol used to replace Telnet. in Java programming, ssh and telnet differ only in establishing and releasing connections.

The connection process is actually the process of sending commands to interact with the remote server. The principle is also very simple: every time a command is sent to the server, it will wait for the response stream returned after the server executes this command, for the moment, it is called an interaction. We receive these streams within the specified time. If a "command prompt" appears in these streams, it means that the server is in the waiting status and we think the interaction is complete. Otherwise, the interaction fails, an exception occurs.

There are not many command prompts on the Linux server, and the logon command prompt is limited. Generally, there are three types: User Name prompt, password prompt, and logon success prompt (also called command prompt, different linux user command and password prompts are slightly different.

Below is a simple program for telnet testing. This program is only used for testing, so many of the statements are not standard.

(1) required package: commons-net-2.2.jar

(2) The Code is as follows:

 

Package Telnet; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. outputstream; <br/> Import java.net. socketexception; <br/> Import Java. util. regEx. pattern; <br/> Import org.apache.commons.net. telnet. telnetclient; <br/> public class Telnet {<br/> private telnetclient; <br/> private inputstream; <br/> private outputstream; <br/> priv Ate long timeouts = 30000; // The default timeout value is 30 seconds <br/>/** <br/> * establish a telnet connection <br/> * @ Param hostip Server IP address <br/> * @ Param Port port: Telnet, default port: 23 <br/> * @ Param username Logon account <br/> * @ Param Password Logon password <br/> * @ throws ioexception <br/> *@ throws socketexception <br/> */<br/> Public void connect (string hostip, int port, string username, string password) throws socketexception, ioexception {<br/> telnetclient = new Tel Netclient (); <br/> telnetclient. connect (hostip, Port); <br/> inputstream = telnetclient. getinputstream (); // input stream for receiving server messages <br/> outputstream = telnetclient. getoutputstream (); // The output stream from which a message is sent to the server <br/> // after the connection is completed, the server waits for the user name to be sent by the client, then the password <br/> If (sendlogininfo (username ,". * password: ") {// If the prompt obtained after the user name is sent is a password prompt <br/> sendlogininfo (password," //] // $ "); // If the prompt obtained after sending the password information is a command prompt <br/>}< br/> private Boolean sendlogininfo (St Ring info, string regext) {<br/> pattern = pattern. compile (regext); <br/> try {<br/> outputstream. write (Info + "/N "). getbytes (); <br/> outputstream. flush (); <br/> int I =-1; <br/> stringbuilder sb = new stringbuilder (); <br/> long starttime = system. currenttimemillis (); <br/> while (system. currenttimemillis ()-starttime <timeout) {<br/> while (I = (char) inputstream. read ()>-1) {<br/> if (I =-1) {<br/> throw new Illegalargumentexception ("messages not received"); <br/>}< br/> char CH = (char) I; <br/> If (CH = '/N' | CH ='/R') {// the command prompt is last in a row <br/> Sb. delete (0, sb. length (); <br/> continue; <br/>}< br/> Sb. append (char) CH); </P> <p> If (pattern. matcher (sb. tostring ()). find () {<br/> return true; <br/>}< br/> throw new illegalargumentexception ("prompt not received upon timeout "); <br/>} catch (ioexception e) {<br/> close (); <br/> return false; <br/>} <Br/>}</P> <p> Public String send (string cmd) {<br/> If (null = telnetclient | null = inputstream | null = outputstream) {<br/> throw new illegalargumentexception ("please establish a connection first or fail to establish a connection"); <br/>}< br/> pattern = pattern. compile ("//] // $"); // If the command prompt is/$ <br/> stringbuilder text = new stringbuilder (); <br/> try {<br/> outputstream. write (CMD + "/N "). getbytes (); <br/> outputstream. flush (); <br/> stringbuilder sb = new Stringbuilder (); <br/> long starttime = system. currenttimemillis (); <br/> int I =-1; <br/> while (system. currenttimemillis ()-starttime <timeout) {<br/> while (I = inputstream. read ()>-1) {<br/> if (I =-1) {<br/> throw new illegalargumentexception ("messages not received "); <br/>}< br/> char CH = (char) I; <br/> text. append (CH); <br/> If (CH = '/N' | CH ='/R') {<br/> Sb. delete (0, sb. length (); <br/> continue; <br/>}< br/> Sb. append (CH); <B R/> If (pattern. matcher (sb. tostring ()). find () {// return to the response stream and find the command prompt <br/> return text. tostring (); <br/>}< br/> throw new illegalargumentexception ("the prompt cannot be received upon timeout "); <br/>} catch (ioexception e) {<br/> close (); <br/> return NULL; <br/>}< br/> Public void close () {<br/> If (null! = NULL) {<br/> try {<br/> telnetclient. disconnect (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (); <br/>}< br/> Public static void main (string [] ARGs) throws socketexception, ioexception {<br/> Telnet = new telnet (); <br/> Telnet. connect ("ip", 23, "username", "passowrd"); <br/> system. out. println (Telnet. send ("ls"); <br/> Telnet. close (); <br/>}< br/> 

The implementation of SSH is similar to that of Telnet. the difference lies in establishing a connection and releasing a connection:

(1) need package: jsch-0.1.42.jar

(2) the key code is as follows:

/** <Br/> * establish a connection <br/> * @ Param hostip Server IP address <br/> * @ Param port ssh port 22 by default <br/> * @ Param username <br/> * @ Param password <br/> */<br/> Public void connect (string hostip, int port, string username, string password) {<br/> try {<br/> session = jsch. getsession (username, hostip, Port); <br/> session. setpassword (password); <br/> session. setuserinfo (defaultuserinfo); <br/> session. connect (); <br/> channel = ses Sion. openchannel ("shell"); <br/> inputstream = channel. getinputstream (); <br/> outputstream = channel. getoutputstream (); <br/>} catch (jschexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>} catch (ioexception e) {<br/> // todo auto-generated Catch Block <br/> E. printstacktrace (); <br/>}< br/> // close the connection <br/> Public void close () {<br/> If (null! = Channel) {<br/> channel. Disconnect (); <br/>}< br/> If (null! = Session) {<br/> session. Disconnect (); <br/>}< br/>} 

Defaultuserinfo is a class object that implements userinfo:

Userinfo defaultuserinfo = new userinfo () {<br/> Public String getpassphrase () {<br/> return NULL; <br/>}< br/> Public String GetPassword () {<br/> return NULL; <br/>}< br/> Public Boolean promptpassword (string arg0) {<br/> return false; <br/>}< br/> Public Boolean promptpassphrase (string arg0) {<br/> return false; <br/>}< br/> Public Boolean promptyesno (string arg0) {<br/> return true; <br/>}< br/> Public void showmessage (string arg0) {<br/>}< br/>} 

 

The introduction is complete. Thank you for your comments!

 

 

 

 


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.