Use the ganymed tool to call SSH2

Source: Internet
Author: User

The ganymed-ssh2-build210.jar package needs to be introduced.

It is actually very simple. Therefore, you can directly paste the code to speak.

1 package COM. eshore. framework. util; 2 3 4 5 import Java. io. bufferedreader; 6 Import Java. io. file; 7 Import Java. io. fileinputstream; 8 Import Java. io. ioexception; 9 Import Java. io. inputstream; 10 Import Java. io. inputstreamreader; 11 import Java. util. arraylist; 12 Import Java. util. list; 13 14 Import ch. ethz. ssh2.connection; 15 Import ch. ethz. ssh2.session; 16 Import ch. ethz. ssh2.streamgobbler; 17 import ch. ethz. ssh2.log. logger; 18/** 19 * shell script call class 20 * @ author clear 21*22 */23 public class sshbasic {24 25 // connection, log on to 26 public connection login (string hostname, int port, string username, string password) {27 28 // get connection 29 connection conn = new connection (hostname, Port ); 30 try {31 // connect 32 Conn. connect (); 33 // enter the account password to log on to 34 Boolean isauthenticated = Conn. authenticatewithpassword (username, password); 35 // Login Failed, error 36 IF (isauthenticated = false) {37 throw new ioexception ("isauthentication failed. "); 38} 39} catch (ioexception e) {40 41 E. printstacktrace (); 42} 43 44 return conn; 45} 46 // get session 47 public session getsession (connection conn) {48 session sess = NULL; 49 try {50 sess = Conn. opensession (); 51} catch (ioexception e) {52 // todo auto-generated Catch Block 53 E. printstacktrace (); 54} 55 return sess; 56} 57 // obtain the console print information 58 Public String printcmd (string path, connection Conn, session sess, string date, string City) {59 string TXT = ""; 60 try {61 sess.exe ccommand ("chmod 755" + path + "&" + path + "" + date + "" + city ); 62 // print information 63 inputstream stdout = new streamgobbler (sess. getstdout (); 64 // print error 65 inputstream stderr = new streamgobbler (sess. getstderr (); 66 bufferedreader brout = new bufferedreader (New inputstreamreader (stdout, "UTF-8"); 67 bufferedreader brerr = new bufferedreader (New inputstreamreader (stderr, "UTF-8"); 68 while (true) {69 string line = brout. readline (); 70 If (line = NULL) {71 break; 72} 73 TXT + = line + "<br/>"; 74 system. out. println (line); 75} 76 while (true) {77 string line = brerr. readline (); 78 If (line = NULL) {79 break; 80} 81 TXT + = line + "<br/>"; 82 system. out. println (line); 83} 84} catch (ioexception e) {85 // todo auto-generated Catch Block 86 E. printstacktrace (); 87} 88 89 return txt; 90} 91 92 public static void main (string [] ARGs) {93 94 sshbasic M = new sshbasic (); 95 // connect and log on to 96 connection conn = m. login ("132.122.1.51", 22, "srglweb", "srglweb123"); 97 // obtain session 98 session sess = m. getsession (conn); 99 // obtain console information 100 string cmd = m. printcmd ("shelltest/Two. sh ", Conn, sess," 20140905 "," 200 "); 101 system. out. println ("cmd:" + cmd); 102 system. out. println ("--->" + sess); 103 // determines whether the session is successful. 104 int result = sess. getexitstatus (); // if the call succeeds, 0105 system is returned. out. println ("Result:" + result); 106 sess. close (); 107 Conn. close (); 108} 109 110}

What you want to explain is also in the code. It is mainly recorded, so you don't need to find it in the future.

Use the ganymed tool to call SSH2

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.