浏览 5309 次
锁定老帖子 主题:JAVA执行shell命令小工具
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2014-05-28
在我们的项目中,大部分环境的服务器用的都是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如下: <pre name="code" class="java">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&gt; "+line); } while (true) { line = stderrReader.readLine(); if (line == null) break; Log.info("GanyMedUtil out&gt; "+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/ &amp;&amp; ls &amp;&amp; date");//多个命令之间使用&amp;&amp;隔开 //ganyMedUtil.execMoreShellCommand(""); //ganyMedUtil.exec("ls"); } }</pre> 输出的内容如下所示: <pre name="code" class="java">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&gt; 2 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; abc.txt 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; anaconda-ks.cfg 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; apache-ant-1.9.2 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; apache-ant-1.9.2-bin.tar.gz 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; apache-nutch-1.8 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; apache-nutch-1.8-src.zip 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; a.sh 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; bb.sh 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; b.sh 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; case.sh 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; cip.sh 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; c.sh 2014-05-28 15:27:19.454:INFO::GanyMedUtil out&gt; date 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; d.sh 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; e.sh 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; f.sh 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; ganglia_20140327155418.zip 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; ganglia-3.1.7-3.el6.rf.x86_64.rpm 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; ganglia-devel-3.1.7-3.el6.rf.x86_64.rpm 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; ganglia-gmetad-3.1.7-3.el6.rf.x86_64.rpm 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; ganglia-gmond-3.1.7-3.el6.rf.x86_64.rpm 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; ganglia-gmond-python-3.1.7-3.el6.rf.x86_64.rpm 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; ganglia-web-3.1.7-3.el6.rf.x86_64.rpm 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; gg.sh 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; hadoop1.2 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; hadoop-1.2.0.tar.gz 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; installganglia.sh 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; install.log 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; install.log.syslog 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; jdk1.7 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; jdk-7u51-linux-x64.tar.gz 2014-05-28 15:27:19.455:INFO::GanyMedUtil out&gt; jmxtrans-20121016.145842.6a28c97fbb-0.noarch.rpm 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; libconfuse-2.7-6.2.x86_64.rpm 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; libevent-2.0.21-stable 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; libevent-2.0.21-stable.tar.gz 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; memcached-1.4.15 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; memcached-1.4.15.tar.gz 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; my.sh 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; pp 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; solr-4.3.1.tgz 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; test.jar 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; test.py 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; zookeeper 2014-05-28 15:27:19.456:INFO::GanyMedUtil out&gt; 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/ &amp;&amp; ls &amp;&amp; date 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; bb.sh 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; build 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; build.xml 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; CHANGES.txt 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; conf 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; default.properties 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; docs 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; ivy 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; lib 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; LICENSE.txt 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; NOTICE.txt 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; README.txt 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; runtime 2014-05-28 15:27:19.789:INFO::GanyMedUtil out&gt; src 2014-05-28 15:27:19.790:INFO::GanyMedUtil out&gt; 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/ &amp;&amp; ls &amp;&amp; date </pre> 能够在在应用程序中,执行shell脚本是非常有用的,我们可以自己写一个shell脚本,把多个shell命令,封装起来,然后执行一次shell即可。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间:2014-07-07
最后修改:2014-07-07
这样如何保证安全呢
|
|
返回顶楼 | |