JAVA执行shell命令小工具

在我们的项目中,大部分环境的服务器用的都是Linux,从而使用JAVA与Linux shell通讯,就成了一项很常见的事情。

Ganymed SSH是一个Java实现SSH的项目
Java 实现SSH协议的项目有很多,如JFTP,trilead SSH,JSCH,ganymed SSH等
下面我们主要说的是关于ganymed SSH的一些小使用。
Ganymed SSH-2 for Java是用纯Java实现SSH-2协议的一个项目。可以通过它直接在Java程序中连接SSH服务器,实现基于SSH协议的服务访问。 如远程命令执行和shell访问,本地和远程端口转发,本地数据流转发,X11转发,SCP,SFTP等功能。



散仙测试的demo如下:

package com.qin.shell;

 
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.mortbay.log.Log;
 

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler;

public class ExecShell{
	private String IP;//要远程登录的IP地址
	private String username;//用户名
	private String password;//密码
	
	public ExecShell(String IP, String username,String password){
		this.IP=IP;
		this.username=username;
		this.password=password;
	}
	
	//命令执行
	public boolean exec( String command ) throws InterruptedException{
		Log.info("command: "+command);
		boolean rtn = false;
		try {
			Connection conn = new Connection(IP);
			conn.connect();
			boolean isAuthenticated = conn.authenticateWithPassword(username, password);
			if (isAuthenticated == false){
				throw new IOException("Authentication failed.");
			}
			Session sess = conn.openSession();
			sess.execCommand(command);
			
			InputStream stdout = new StreamGobbler(sess.getStdout());
			BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
			
			InputStream stderr = new StreamGobbler(sess.getStderr());
			BufferedReader stderrReader = new BufferedReader(new InputStreamReader(stderr));
			
			String line = null;
			while ( (line = br.readLine())!=null )
			{
				Log.info("GanyMedUtil out> "+line);
			}
			
			while (true)
			{
				line = stderrReader.readLine();
				if (line == null)
					break;
				Log.info("GanyMedUtil out> "+line);
			}
			
			/* Show exit status, if available (otherwise "null") */  
			Log.info("ExitCode: " + sess.getExitStatus()+" "+IP+":"+command);
			sess.close();
			conn.close();
			rtn = new Integer(0).equals(sess.getExitStatus());
			return rtn;
		}
		catch (IOException e)
		{
			Log.warn("Error ......................",e);
			e.printStackTrace();
			System.exit(2);
			return rtn;
		}
	}

 
	
	
	
	public static void main(String[] args) throws InterruptedException {
	
		ExecShell es = new ExecShell("192.168.75.130","root","dongliang");
		System.out.println("==========================================单个命令测试执行==========================================");
		es.exec("ls");//执行单行命令
		System.out.println("==========================================多个命令测试执行==========================================");
		es.exec("cd /root/apache-nutch-1.8/ && ls && date");//多个命令之间使用&&隔开
		//ganyMedUtil.execMoreShellCommand("");
		//ganyMedUtil.exec("ls");
	
	}

}

输出的内容如下所示:
2014-05-28 15:27:19.076:INFO::Logging to STDERR via org.mortbay.log.StdErrLog
2014-05-28 15:27:19.076:INFO::command: ls
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> 2
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> abc.txt
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> anaconda-ks.cfg
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-ant-1.9.2
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-ant-1.9.2-bin.tar.gz
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-nutch-1.8
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> apache-nutch-1.8-src.zip
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> a.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> bb.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> b.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> case.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> cip.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> c.sh
2014-05-28 15:27:19.454:INFO::GanyMedUtil out> date
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> d.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> e.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> f.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia_20140327155418.zip
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-devel-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-gmetad-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-gmond-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-gmond-python-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> ganglia-web-3.1.7-3.el6.rf.x86_64.rpm
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> gg.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> hadoop1.2
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> hadoop-1.2.0.tar.gz
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> installganglia.sh
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> install.log
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> install.log.syslog
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> jdk1.7
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> jdk-7u51-linux-x64.tar.gz
2014-05-28 15:27:19.455:INFO::GanyMedUtil out> jmxtrans-20121016.145842.6a28c97fbb-0.noarch.rpm
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> libconfuse-2.7-6.2.x86_64.rpm
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> libevent-2.0.21-stable
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> libevent-2.0.21-stable.tar.gz
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> memcached-1.4.15
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> memcached-1.4.15.tar.gz
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> my.sh
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> pp
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> solr-4.3.1.tgz
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> test.jar
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> test.py
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> zookeeper
2014-05-28 15:27:19.456:INFO::GanyMedUtil out> zookeeper.zip
2014-05-28 15:27:19.456:INFO::ExitCode: 0 192.168.75.130:ls
==========================================多个命令测试执行==========================================
2014-05-28 15:27:19.458:INFO::command: cd /root/apache-nutch-1.8/ && ls && date
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> bb.sh
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> build
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> build.xml
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> CHANGES.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> conf
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> default.properties
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> docs
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> ivy
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> lib
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> LICENSE.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> NOTICE.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> README.txt
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> runtime
2014-05-28 15:27:19.789:INFO::GanyMedUtil out> src
2014-05-28 15:27:19.790:INFO::GanyMedUtil out> 2014年 05月 28日 星期三 23:27:19 CST
2014-05-28 15:27:19.790:INFO::ExitCode: 0 192.168.75.130:cd /root/apache-nutch-1.8/ && ls && date


能够在在应用程序中,执行shell脚本是非常有用的,我们可以自己写一个shell脚本,把多个shell命令,封装起来,然后执行一次shell即可。

你可能感兴趣的:(java,linux,ssh,脚本)