ANT를 이용하여 FTP와 Telnet 작업 진행하기
FTP, Telnet 작업을 위한 환경 세팅
FTP와 Telnet은 ANT의 Core Task에 있는 Command가 아니라 Optional Task이기 때문에 추가적으로 설치해야되는 라이브러리가 있다. ANT에서 FTP와 Telnet을 사용하기 위하여 필요한 라이브러리는 Apache Jakarta 프로젝트에서 개발한 Commons Net과 ORO
이다.
FTP와 Telnet 사용을 위하여 라이브러리를 설치하는 과정은 다음과 같다.
- 먼저 Jakarta 프로젝트
에서 이 두개의 라이브러리를 다운받는다.
- 다운 받은 압축파일을 원하는 위치에 압축을 푼다.
다음 과정은 Eclipse를 사용할 때와 Prompt상에서 ANT를 실행할 때로 나뉘어 설명하도록 하겠다.
! Eclipse를 사용할 때.
- Eclipse에서 Window | Preferences | Ant | Runtime으로 이동한 다음 Classpath Tab의 Ant Home Entries에 앞에서 다운받은 두개의 라이브러리를 추가해준다.
- 이 두개의 라이브러리를 위와 같이 세팅하면 모든 세팅은 완료된다.
! Prompt상에서 ANT를 실행할 때.
- 앞에서 다운받는 두개의 라이브러리를 ANT_HOME/lib에 복사한다.
이상으로 FTP와 Telnet을 사용하기 위한 기본적인 세팅은 완료하였다. ANT에서 FTP와 Telnet을 사용하기 위하여 Dependency관계에 있는 라이브러리는 http://ant.apache.org/manual/install.html#commons-net에서 확인할 수 있다.
FTP 사용을 위한 ANT Script 작성
앞의 세팅이 정상적으로 완료되었다면 FTP를 위한 ANT Script를 작성하여 원하는 파일을 FTP 서버에 전송하는 것이 가능하다. 다음은 ANT 사용자 문서에서 제공하는 스크립트의 일부분이다.
<ftp server="ftp.apache.org" port="2121" remotedir="/pub/incoming" userid="coder" password="java1" depends="yes" binary="no" > <fileset dir="htdocs/manual"> <include name="**/*.html"/> </fileset> </ftp>
위 스크립트의 세부 항목과 더 많은 스크립트 예제는 http://ant.apache.org/manual/OptionalTasks/ftp.html에서 참조할 수 있다.
Telnet 사용을 위한 ANT Script 작성
앞의 세팅이 정상적으로 완료되었다면 Telnet을 위한 ANT Script를 작성하여 Remote서버에서 원하는 작업을 하는 것이 가능하다. 다음은 ANT 사용자 문서에서 제공하는 스크립트의 일부분이다.
<telnet server="localhost">
<read>ogin:</read>
<write>bob</write>
<read>assword:</read>
<write>badpass</write>
<read>/home/bob</read>
<write>ls</write>
<read>/home/bob</read>
</telnet>
위 스크립트의 세부 항목과 더 많은 스크립트 예제는 http://ant.apache.org/manual/OptionalTasks/telnet.html에서 참조할 수 있다.
telnet을 사용할 때 중요한 점은 read/write를 적절하게 사용해야 한다는 것이다. write 스크립트는 우리가 실행하고자하는 작업들을 명시할 수 있다. read는 write 작업을 완료한 다음에 해당 구문이 있는 곳까지 읽어서 Console에 출력하는 것이다. 그러므로 write 스크립트를 통하여 작업을 수행한 다음 해당 작업이 정확하게 실행되었는지를 확인하기 위하여 read 스크립트를 작성해주어야 한다. |
telnet의 read 스크립트는 보통 Remote Server의 prompt를 사용한다. 예를 들어 telnet으로 Remote Server에 접근 했을 때의 첫 prompt가 /usr/home/javajigi>이고 cd 명령어를 이용하여 /usr/java 디렉토리로 이동하고자 할 때의 telnet 스크립트를 보면 다음과 같다.
<telnet userid="javajigi" password="javajigipass" server="localhost" timeout="20"> <read>/usr/home/javajigi></read> <write>cd /usr/java</write> <read>/usr/java></read> <write>ls -al</write> <read>/usr/java></read> </telnet>
위와 같이 작성하면 각 단계별로 실행되는 모든 Command들의 상황을 로컬상에서 볼 수 있게 된다.
![]() |
ANT를 이용하여 FTP와 Telnet 작업 진행하기 - 통합 개발 환경 - Conflue | ![]() |
FTP
Description
The ftp task implements a basic FTP client that can send, receive, list, delete files, and create directories. See below for descriptions and examples of how to perform each task.
Note: This task depends on external libraries not included in the Ant distribution. See Library Dependencies for more information. Get the latest version of this library, for the best support in Ant
The ftp task attempts to determine what file system is in place on the FTP server. Supported server types are Unix, NT, OS2, VMS, and OS400. In addition, NT and OS400 servers which have been configured to display the directory in Unix style are also supported correctly. Otherwise, the system will default to Unix standards. remotedir must be specified in the exact syntax required by the ftp server. If the usual Unix conventions are not supported by the server, separator can be used to set the file separator that should be used instead.
See the section on directory based tasks, on how the inclusion/exclusion of files works, and how to write patterns.
This task does not currently use the proxy information set by the <setproxy>
task, and cannot go through a firewall via socks.
Warning: there have been problems reported concerning the ftp get with the newer
attribute. Problems might be due to format of ls -l differing from what is expected by commons-net, for instance due to specificities of language used by the ftp server in the directory listing. If you encounter such a problem, please send an email including a sample directory listing coming from your ftp server (ls -l on the ftp prompt).
If you can connect but not upload or download, try setting the passive
attribute to true to use the existing (open) channel, instead of having the server try to set up a new connection.
Parameters
Note about remotedir attribute
Action |
meaning of remotedir |
use of nested fileset (s) |
send/put |
base directory to which the files are sent |
they are used normally and evaluated on the local machine |
recv/get |
base directory from which the files are retrieved |
the remote files located under the remotedir matching the include/exclude patterns of the fileset |
del/delete |
base directory from which files get deleted |
the remote files located under the remotedir matching the include/exclude patterns of the fileset |
list |
base directory from which files are listed |
the remote files located under the remotedir matching the include/exclude patterns of the fileset |
mkdir | directory to create |
not used |
chmod | base directory from which the mode of files get changed |
the remote files located under the remotedir matching the include/exclude patterns of the fileset |
rmdir |
base directory from which directories get removed |
the remote directories located under the remotedir matching the include/exclude patterns of the fileset |
Parameters specified as nested elements
fileset
The ftp task supports any number of nested <fileset>
elements to specify the files to be retrieved, or deleted, or listed, or whose mode you want to change.
The attribute followsymlinks
of fileset
is supported on local (put) as well as remote (get, chmod, delete) filesets. Before ant 1.6 there was no support of symbolic links in remote filesets. In order to exclude symbolic links (preserve the behavior of ant 1.5.x and older), you need to explicitly set followsymlinks
to false
. On remote filesets hidden files are not checked for being symbolic links. Hidden files are currently assumed to not be symbolic links.
Remote filesets do not support selectors.
Sending Files
The easiest way to describe how to send files is with a couple of examples:
<ftp server="ftp.apache.org" userid="anonymous" password="me@myorg.com"> <fileset dir="htdocs/manual"/> </ftp>
Logs in to ftp.apache.org
as anonymous
and uploads all files in the htdocs/manual
directory to the default directory for that user.
<ftp server="ftp.apache.org" remotedir="incoming" userid="anonymous" password="me@myorg.com" depends="yes"> <fileset dir="htdocs/manual"/> </ftp>
Logs in to ftp.apache.org
as anonymous
and uploads all new or changed files in the htdocs/manual
directory to the incoming
directory relative to the default directory for anonymous
.
<ftp server="ftp.apache.org" port="2121" remotedir="/pub/incoming" userid="coder" password="java1" passive="yes" depends="yes" binary="no"> <fileset dir="htdocs/manual"> <include name="**/*.html"/> </fileset> </ftp>
Logs in to ftp.apache.org
at port 2121
as coder
with password java1
and uploads all new or changed HTML files in the htdocs/manual
directory to the /pub/incoming
directory. The files are transferred in text mode. Passive mode has been switched on to send files from behind a firewall.
<ftp server="ftp.hypothetical.india.org" port="2121" remotedir="/pub/incoming" userid="coder" password="java1" depends="yes" binary="no" systemTypeKey="Windows" serverTimeZoneConfig="India/Calcutta"> <fileset dir="htdocs/manual"> <include name="**/*.html"/> </fileset> </ftp>
Logs in to a Windows server at ftp.hypothetical.india.org
at port 2121
as coder
with password java1
and uploads all new or changed (accounting for timezone differences) HTML files in the htdocs/manual
directory to the /pub/incoming
directory. The files are transferred in text mode.
<ftp server="ftp.nt.org" remotedir="c:\uploads" userid="coder" password="java1" separator="\" verbose="yes"> <fileset dir="htdocs/manual"> <include name="**/*.html"/> </fileset> </ftp>
Logs in to the Windows-based ftp.nt.org
as coder
with password java1
and uploads all HTML files in the htdocs/manual
directory to the c:\uploads
directory. Progress messages are displayed as each file is uploaded.
Getting Files
Getting files from an FTP server works pretty much the same way as sending them does. The only difference is that the nested filesets use the remotedir attribute as the base directory for the files on the FTP server, and the dir attribute as the local directory to put the files into. The file structure from the FTP site is preserved on the local machine.
<ftp action="get" server="ftp.apache.org" userid="anonymous" password="me@myorg.com"> <fileset dir="htdocs/manual"> <include name="**/*.html"/> </fileset> </ftp>
Logs in to ftp.apache.org
as anonymous
and recursively downloads all .html files from default directory for that user into the htdocs/manual
directory on the local machine.
<ftp action="get" server="ftp.apache.org" userid="anonymous" password="me@myorg.com" systemTypeKey="UNIX" defaultDateFormatConfig="yyyy-MM-dd HH:mm"> <fileset dir="htdocs/manual"> <include name="**/*.html"/> </fileset> </ftp>
If apache.org ever switches to a unix FTP server that uses the new all-numeric format for timestamps, this version would become necessary. It would accomplish the same functionality as the previous example but would successfully handle the numeric timestamps. The systemTypeKey
is not necessary here but helps clarify what is going on.
<ftp action="get" server="ftp.hypthetical.fr" userid="anonymous" password="me@myorg.com" defaultDateFormatConfig="d MMM yyyy" recentDateFormatConfig="d MMM HH:mm" serverLanguageCodeConfig="fr"> <fileset dir="htdocs/manual"> <include name="**/*.html"/> </fileset> </ftp>
Logs into a UNIX FTP server at ftp.hypothetical.fr
which displays dates with French names in Standard European format, as anonymous
, and recursively downloads all .html files from default directory for that user into the htdocs/manual
directory on the local machine.
Deleting Files
As you've probably guessed by now, you use nested fileset elements to select the files to delete from the remote FTP server. Again, the filesets are relative to the remote directory, not a local directory. In fact, the dir attribute of the fileset is ignored completely.<ftp action="del" server="ftp.apache.org" userid="anonymous" password="me@myorg.com"> <fileset> <include name="**/*.tmp"/> </fileset> </ftp>
Logs in to ftp.apache.org
as anonymous
and tries to delete all *.tmp files from the default directory for that user. If you don't have permission to delete a file, a BuildException is thrown.
Listing Files
<ftp action="list" server="ftp.apache.org" userid="anonymous" password="me@myorg.com" listing="data/ftp.listing"> <fileset> <include name="**"/> </fileset> </ftp>
This provides a file listing in data/ftp.listing
of all the files on the FTP server relative to the default directory of the anonymous
user. The listing is in whatever format the FTP server normally lists files.
Creating Directories
Note that with the mkdir action, the directory to create is specified using the remotedir attribute.
<ftp action="mkdir" server="ftp.apache.org" userid="anonymous" password="me@myorg.com" remotedir="some/remote/dir"/>
This creates the directory some/remote/dir
beneath the default root directory. As with all other actions, the directory separator character must be correct according to the desires of the FTP server.
Removing Directories
This action uses nested fileset elements to select the directories to remove from the remote FTP server. The filesets are relative to the remote directory, not a local directory. The dir attribute of the fileset is ignored completely. The directories to be removed must be empty, or contain only other directories that have been also selected to be removed by the filesets patterns, otherwise a BuildException will be thrown. Also, if you don't have permission to remove a directory, a BuildException is thrown.<ftp action="rmdir" server="ftp.apache.org" userid="anonymous" password="me@myorg.com" remotedir="/somedir" > <fileset> <include name="dira"/> <include name="dirb/**"/> </fileset> </ftp>
Logs in to ftp.apache.org
as anonymous
and tries to remove /somedir/dira
directory and all the directories tree starting at, and including, /somedir/dirb
. When removing the /somedir/dirb
tree, it will start at the leaves moving up to the root, so that when it tries to remove a directory it is sure all the directories under it are already removed. Obviously all the files in the tree must have been already deleted.
As an example suppose you want to delete everything contained into /somedir
, so invoke first the <ftp>
task with action="delete"
, then with action="rmdir"
specifying in both cases remotedir="/somedir"
and
<fileset> <include name="**"/> </fileset>The directory specified in the
remotedir
parameter is never selected for remove, so if you need to remove it, specify its parent in remotedir
parameter and include it in the <fileset>
pattern, like "somedir/**"
.