Java uses SSH to connect to Linux and use the passwd command to change the password

Source: Internet
Author: User

Using inputstream and outputstream to obtain command line running results and input content is required first. Then, we need to solve the SSH connection problem. We found it in the opensource project. Ganymed SSH-2 for Java.There are examples after the download, but no examples of interactive commands are found. so I explored how to change the password through a foreign discussion on the Internet. Basically, the problem is that I cannot find the returned prompt, "change the password in Linux", and enter the current password first, enter two new passwords. If the current password is incorrect or the new password does not meet the requirements of the system password rules, the new password is required. if this happens, it is easy for the program to enter a loop that is not well controlled. most people may think that all the responses after the command is input should use session. getstdout (); not session. getstderr (); this is true for method names. One is to get standard output and the other is to get error information. in fact, after entering passwd. changing password for user [username].
The changing password for [username] line must pass the session. getstdout. however, sessssion is required for the following (current) Unix Password: new Unix Password: retype new Unix Password. getstderr ();. If the user input does not pass the system detection, the system returns the specific cause, but it starts with bad password. if the last modification is successful. you will also see successfully. the information is sent through the session. getstdout. now the problem is solved. write a class to modify the password. after using this method, you can now create an account within the company and set up automatic email reply through your own management system.

Import ch. ethz. ssh2.connection; <br/> Import ch. ethz. ssh2.session; <br/> Import ch. ethz. ssh2.streamgobbler; <br/> Import COM. io. debug; <br/> Import Java. io. bufferedreader; <br/> Import Java. io. ioexception; <br/> Import Java. io. inputstream; <br/> Import Java. io. inputstreamreader; <br/> Import Java. io. outputstream; </P> <p> public class changeemailpassword {<br/> private string username = ""; <br/> private string oldpassword = ""; <br/> private connection conn = NULL; <br/> private Boolean haserror = false; <br/> private string errormessage = ""; <br/> private Boolean issuccessfully = false; <br/> private string systemmessage = ""; </P> <p> Public static final string host = "127.0.0.1 "; // server IP <br/> Public Boolean issuccessfully () {<br/> return issuccessfully; <br/>}< br/> Public Boolean ishaserror () {<br/> return haserror; <br/>}< br/> Public String geterrormessage () {<br/> return errormessage; <br/>}< br/> Public void seterrormessage (string MSG) {<br/> haserror = true; <br/> This. errormessage = MSG; <br/>}</P> <p> Public changeemailpassword (string username, string oldpassword) {<br/> This. username = username; <br/> This. oldpassword = oldpassword; <br/> try {<br/> conn = new connection (host); <br/> Conn. connect (); </P> <p> Boolean isauthenticated = Conn. authenticatewithpassword (username, oldpassword); <br/> If (isauthenticated = false) {<br/> seterrormessage ("authentication failed. "); <br/> conn = NULL; <br/>}< br/>} catch (exception e) {<br/> Conn. close (); <br/> conn = NULL; <br/> system. out. println (E); <br/>}< br/> Public void setnewpassword (string newpassword) {<br/> If (haserror) {<br/> return; <br/>}< br/> If (conn = NULL) {<br/> return; <br/>}< br/> try {<br/> session sess = Conn. opensession (); <br/> sess.exe ccommand ("passwd"); <br/> inputstream so = sess. getstdout (); <br/> inputstream err = sess. getstderr (); <br/> outputstream out = sess. getstdin (); </P> <p> byte [] buffer = new byte [500]; // It is not necessary. 130 is almost the same. in case of any prompt. <br/> int length = 0; <br/> length = err. read (buffer); <br/> // If (length> 0) {<br/> // system. out. println ("#1:" + new string (buffer, 0, length); <br/> // (current) UNIX password: <br/>/}< br/> string coldpassword = oldpassword + "/N"; <br/> out. write (coldpassword. getbytes (); <br/> length = err. read (buffer); <br/> // If (length> 0) {<br/> // system. out. println ("#2:" + new string (buffer, 0, length); <br/> // (current) UNIX password: <br/>/}< br/> string cnewpass = newpassword + "/N"; <br/> out. write (cnewpass. getbytes (); <br/> length = err. read (buffer); <br/> If (length> 0) {<br/> string rs = new string (buffer, 0, length ); <br/> // system. out. println ("#3:" + RS); <br/> If (RS. indexof ("bad")>-1) {<br/> sess. close (); <br/> Conn. close (); <br/> seterrormessage (RS); <br/> return; <br/>}< br/> out. write (cnewpass. getbytes (); <br/> length = so. read (buffer); <br/> If (length> 0) {<br/> string rs = new string (buffer, 0, length ); <br/> If (RS. indexof ("successfully")>-1) {<br/> This. issuccessfully = true; <br/> This. systemmessage = RS; <br/>}</P> <p> sess. close (); </P> <p> Conn. close (); <br/>}catch (ioexception e) {<br/> E. printstacktrace (system. err); <br/>}</P> <p> Public static void main (string [] ARGs) {<br/> changeemailpassword CEP = new changeemailpassword ("username", "oldpassword"); <br/> If (CEP. ishaserror () {<br/> system. out. println (CEP. geterrormessage (); <br/> CEP = NULL; <br/> return; <br/>}< br/> CEP. setnewpassword ("newpassword"); <br/> If (CEP. ishaserror () {<br/> system. out. println (CEP. geterrormessage (); <br/> CEP = NULL; <br/> return; <br/>}< br/> If (CEP. issuccessfully) {<br/> system. out. println (CEP. getsystemmessage (); <br/>}</P> <p> Public String getsystemmessage () {<br/> return systemmessage; <br/>}</P> <p>Open the system. Out. println commented out in it and you can see the system prompt information. I only keep the most critical information prompt.

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.