Java Remote Execute Linux command __linux

Source: Internet
Author: User

Using GANYMED-SSH2 to invoke commands to execute remote Linux, look up on Maven and find that the most used package is Ganymed-ssh2-build210.jar, so download it

	private static final string hostname = "115.182.62.225";//linux IP Address private static final string username = "root";//lin
	UX username private static final String password = "J\\^pr$&zslxcqhif";//linux password private static Connection conn;
	
	private static session SSH = NULL; /** * Determine if you can connect the host, account password is correct (default configured server) * @return/public static Boolean hasconnection () {return hasconnection (host
	Name,username,password,false); /** * Execute command (default configured server) * @param cmd * @return/public static string Excutecmd (string cmd) {return Excutecmd (
	Hostname,username,password,cmd); /** * Determine whether the host can be connected, the account password is correct * @param hostname * @param username * @param password * @param flag whether the query is only connected to the service Device * @return/public static Boolean hasconnection (String hostname,string username,string Password,boolean flag) {b
		Oolean Isconn = false;
		conn = new Connection (hostname);  
            try {//Connect to host Conn.connect (); Use user name and password checksum isconn = Conn.authentiCatewithpassword (username, password);
		catch (IOException e) {e.printstacktrace ();
		} if (flag) {closeconnection ();
	return isconn; /** * Execute command * @param hostname * @param username * @param password * @param cmd * @return/Public stat IC string Excutecmd (string hostname,string username,string password,string cmd) {StringBuilder result = new Stringbuilde
		R ();
		Boolean isconn = Hasconnection (Hostname,username,password,false);
		if (!isconn) {//Connection failed Result.append ("Connection Server failed");
				}else{try {ssh = conn.opensession ();  
	            Use multiple commands to separate Ssh.execcommand (CMD) with semicolons; Only one line of commands is allowed, that is, the SSH object can only be used once execcommand this method, multiple use will appear an exception//print all the text on the screen inputstream is = new STR  
	            Eamgobbler (Ssh.getstdout ());  
	            BufferedReader BRS = new BufferedReader (new InputStreamReader (IS));  
	                while (true) {String line = Brs.readline ();  
	                  if (line==null) {  Break
	            } result.append (line+ "\ n");
				The catch (IOException e) {result.append ("Connection Server failed");
			E.printstacktrace ();
	} return result.tostring ();  
        /** * Close connected object/private static void CloseConnection () {//connected session and connection object all need to close ssh.close ();
	Conn.close ();
	public static void Main (string[] args) {System.out.println (Excutecmd ("Cd/home/;ls")); }

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.