Use JSCH in Java to connect to a remote server and execute linux commands

Source: Internet
Author: User

Use JSCH in Java to connect to a remote server and execute linux commands

Sometimes you may need to use code to control the execution of linux commands to implement some functions.

JSCH can be used to solve such problems. The specific code is as follows:

Public class CogradientImgFileManager {private static final Logger log = LoggerFactory. getLogger (CogradientImgFileManager. class); private static ChannelExec channelExec; private static Session session = null; private static int timeout = 60000; // test code public static void main (String [] args) {try {versouSshUtil ("10.8.12.189", "jmuser", "root1234", 22); runCmd ("java-version", "UTF-8");} catch (Exception E) {// TODO Auto-generated catch block e. printStackTrace ();}} /*** connect to the remote server * @ param host IP Address * @ param userName * @ param password * @ param port * @ throws Exception */public static void versouSshUtil (String host, string userName, String password, int port) throws Exception {log.info ("try to connect .... host: "+ host +", username: "+ userName +", password: "+ password +", port: "+ port); JSch jsch = New JSch (); // create JSch object session = jsch. getSession (userName, host, port); // obtain a Session object based on the user name, host ip address, and port. setPassword (password); // set the password Properties config = new Properties (); config. put ("StrictHostKeyChecking", "no"); session. setConfig (config); // set properties Session for the session object. setTimeout (timeout); // sets the timeout time session. connect (); // establish a link through Session}/*** execute the command on the remote server * @ param cmd the command character to be executed String * @ param charset encoding * @ throws Exception */public static void runCmd (String cmd, String charset) throws Exception {channelExec = (ChannelExec) session. openChannel ("exec"); channelExec. setCommand (cmd); channelExec. setInputStream (null); channelExec. setErrStream (System. err); channelExec. connect (); InputStream in = channelExec. getInputStream (); BufferedReader reader = new BufferedReader (new InputStre AmReader (in, Charset. forName (charset); String buf = null; while (buf = reader. readLine ())! = Null) {System. out. println (buf);} reader. close (); channelExec. disconnect ();}}

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.