使用Ant進行ssh和scp操作

一、簡介:
  現在我們安裝Linux的時候通常考慮到安全因素(默認情況下)是不打開telnet服務的,而ssh服務是有的,ant很早就支持telnet,但要求我們在Linux下要安裝telnet-server,並要啓用該服務。

還好自Ant1.60開始支持了SSH 及SCP 操作了,早在Ant之前若要支持SSH、SCP、SFTP等任務就必須下載j2ssh的j2ssh-ant.jar和j2ssh-core.jar(在http://www.sourceforge.net的j2ssh下有下載)。現在可以使用Ant提供的Sshexec和scp任務,由$ANT_HOME/lib/ant-jsch.jar提供支持,但是同樣你也要在http://www.jcraft.com/jsch/index.html下載一個依賴包jsch-0.1.24.jar(文件名因版本而不同),jsch同樣也是http://www.sourceforge.net下的一個項目。

你需要把下載的jsch-0.1.24拷貝到$ANT_HOME/lib下,如果是Eclipse下的Ant環境必須在Window->Preferences->Ant->Runtime->Classpath中加入jsch-0.1.24。

JSch是一個SSH2的純Java實現
JSch允許你連接到sshd server並採用端口映射, X11 映射; Scp文件傳輸等,你可以把集成JSch提供的功能到你自己的Java項目中,JSch 的授權方式爲 BSD形式。

二、簡單例子:
  下面是用JSch完成Sshexec和scp兩個任務的最簡單例子,如果需要更詳細的內容,請參考Ant用戶手冊

[Sshexec任務]
<BR><target name="sshexec">
      <sshexec host="192.168.122.180" username="root"  password="123456"
         trust="true" command="cd /;ls"/>
</target>

注意上面的trust屬性一般設置爲true, 如果爲默認值false時,那麼就要求你所連接的host必須存在於你的knownhosts文件中,並且這個文件也必須是存在的,否則會出現 com.jcraft.jsch.JSchException: reject HostKey: 192.168.122.180異常。執行Linux下的命令時可以用分號";"把多個命令隔開,它們將會依次執行,而不需要寫多個sshexec進行多次連接,每次連接只執行一個命令。

該任務的執行後輸出結果如下:

sshexec:
  [sshexec] Connecting to 192.168.122.180:22
  [
sshexec] backup
  [
sshexec] bin
  [
sshexec] boot
  [
sshexec] dev
  [
sshexec] etc
  ...................

[scp任務]

1.拷貝單個文件到遠端服務器
<scp file="c:/cmd.txt" todir="root:[email protected]:/tmp" trust="true"/>

<scp file="c:/cmd.txt" todir="[email protected]:/tmp" password="123456" trust="true"/>

2.拷貝遠端文件本地
<scp file="root:[email protected]:/tmp/cmd.txt" todir="D:/my-app"  trust="true"/>

3.拷貝遠端目錄到本地,將以遞歸形式操作
<scp file="root:[email protected]:/tmp/*" todir="d:/my-app" trust="true"/>

4.拷貝本地目錄中的內容到遠端,遞歸形式,但不在服務器上建立my-app目錄
<scp todir="root:[email protected]:/tmp/" trust="true">
   
<fileset dir="d:/my-app"/>
</scp>

5.拷貝一系列的文件到遠端,會建立相應的層次目錄,不建立my-app目錄
<scp todir="root:[email protected]:/tmp" trust="true">
   
<fileset dir="d:/my-app">
      
<include name="**/*.java" />
   
</fileset>
</scp>

<scp todir="root:[email protected]:/tmp" trust="true">
   
<fileset dir="d:/my-app" excludes="**/*.java"/>
</scp>

最後一個任務的執行輸出結果如下(其他略):

scp:
      [scp]Connecting to 192.168.122.180:22
      [
scp] Sending: cmd.txt : 0
      [
scp] File transfer time: 0.0 Average Rate: ? B/s
      [
scp] Sending: pom.xml : 852
      [
scp] File transfer time: 0.0 Average Rate: ∞ B/s
      [
scp] Sending: application.properties : 142
      [
scp] File transfer time: 0.0 Average Rate: ∞ B/s
      [
scp] Sending: application.properties : 45
      [
scp] File transfer time: 0.0 Average Rate: ∞ B/s
      [
scp] Sending: test.properties : 0
      [
scp] File transfer time: 0.02 Average Rate: 0.0 B/s
      [
scp] Sending: application.properties : 153
      [
scp] File transfer time: 0.0 Average Rate: ∞ B/s
      [
scp] Sending: application.properties : 45
      [
scp] File transfer time: 0.0 Average Rate: ∞ B/s
      [
scp] done.

三、其他例子:

例子1:
<?xml   version= "1.0 "?>
<project   name= "buildssh "   default= "DEFAULT "   basedir= ". ">
<target   name= "init ">
<!--   set   properties,   mkdir,   etc.   -->
<property   file= "build.properties "   />
<property   name= "this.project "   value= "buildssh "   />
<echo   message= "init   in   ${this.project} "   />
<tstamp   />
</target>

<target   name= "DEFAULT "   depends= "init ">
<echo   message= "connecting   to   ${build.server} "   />
<sshexec   host= "Linux   server   IP   address "   username= "Linux   server     username "   password= "Linux   server   password "   trust= "true "   command= "Command   you   want   to   run   on   the   server "   />
</target>
</project>

例子2:
import   com.jcraft.jsch.Channel;      
import   com.jcraft.jsch.ChannelSftp;      
import   com.jcraft.jsch.JSch;      
import   com.jcraft.jsch.Session;      
import   com.jcraft.jsch.UserInfo;      
   
public   class   ExecSCP   {      
        public   static   final   UserInfo   defaultUserInfo   =   new   UserInfo(){      
                public   String   getPassphrase()   {      
                        return   null;      
                }      
   
                public   String   getPassword()   {      
                        return   null;      
                }      
   
                public   boolean   promptPassword(String   arg0)   {      
                        return   false;      
                }      
   
                public   boolean   promptPassphrase(String   arg0)   {      
                        return   false;      
                }      
   
                public   boolean   promptYesNo(String   arg0)   {      
                        return   true;      
                }      
   
                public   void   showMessage(String   arg0)   {      
                }      
        };      
   
        /**    
          *   @param   args    
          */    
public   static   void   main(String[]   args)   throws   Exception{
String   hostname   =   "www.mozat.com ";
String   username   =   "wiimii ";
String   password   =   "jtev000 ";
String   remoteFile   =   "Setup.ini ";
String   localFile   =   "C:\\ ";

                JSch   jsch=new   JSch();

                Session   session=jsch.getSession(username,   hostname,   990);
                session.setPassword(password);
                session.setUserInfo(defaultUserInfo);
                session.connect();

                Channel   channel=session.openChannel( "sftp ");      
                channel.connect();      
                ChannelSftp   c=(ChannelSftp)channel;      
                     
                c.get(remoteFile,   localFile);  
               
                session.disconnect();
              }
}

 
 
發佈了40 篇原創文章 · 獲贊 16 · 訪問量 19萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章