As I've readen ant doesn't provide 'fileset' attribute when downloading files from remote machine via scp task. It works when sending files from local machine, but It doesn't work when starting in remote machine. That's from documentation. So in remote machine I have some folders and load of files in every directory. I want to download all folders and one specified file from every of them. Downloading all files and then deleting unneeded files won't be solution beacuse there are thousands of files.
So. How to download all folders (only create the on disk without content) from specified directory in remote machine and then download one specified file from every directory in remote machine and put it to corresponding folder using ant?
Since you haven't specified I'll assume that your local and remote systems are unix based and therefore support rsync and ssh.
A more cross-platform solution is challenging...
Example
Configure SSH
Generate an SSH key (specify an empty passphrase):
ssh-keygen -f rsync
This will generate 2 files, corresponding to the private and public keys:
|-- rsync
`-- rsync.pub
Install the public key on the remote server
ssh-copy-id -i rsync.pub user#remote
Test that you can now perform a password-less login, using the ssh private key to authenticate:
ssh -i rsync user#remote
ANT
The "download" target invokes rsync to copy the remote file system tree locally. If required one can additionally specify rsync exclusions (see the rsync doco).
<project name="rsync" default="download">
<target name="download">
<exec executable="rsync">
<arg line="-avz -e 'ssh -i rsync' user#remote:/path/to/data/ data"/>
</exec>
</target>
<target name="clean">
<delete dir="data"/>
</target>
</project>
Related
I am trying to automate the deployment process through Ant build XML file. My Ant is of version 1.9.4. As part of that I am trying to send the built war file on to the remote Tomcat server. So, I am using the below code of SCP task.
<target name="scp_task">
<scp file="antproject1.war" todir="${username}#${ipaddress}:${tomcat.webapps.dir}" password="${password}"/>
</target>
Along with that I have ant-jsch-1.9.4.jar in my ANT_HOME/lib directory. And when I am trying to run the Ant command in Windows DOS command prompt. When run the command ant scp_task, I am getting the message as
BUILD FAILED: C:\Users\USER.ssh\known_hosts (The system cannot find the file specified)
Please help what else should I have to add for the code so that I could deploy my war file perfectly.
You have to create the known_hosts file in the given path with a public host key of the remote server.
The file uses a common OpenSSH format like:
example.com,93.184.216.34 ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEAq2A7hRGmdnm9tUDbO9IDSwBK6TbQa+PXYPCPy6rbTrTtw7PHkccKrpp0yVhp5HdEIcKr6pLlVDBfOLX9QUsyCOV0wzfjIJNlGEYsdlLJizHhbn2mUjvSAHQqZETYP81eFzLQNnPHt4EVVUh7VfDESU84KezmD5QlWpXLmvU31/yMf+Se8xhHTvKSCZIFImWwoG6mbUoWf9nzpIoaSjB+weqqUUmpaaasXVal72J+UX2B+2RPW3RcT0eOzQgqlJL3RKrTJvdsjE3JEAvGq3lGHSZXy28G3skua2SmVi/w4yCE6gbODqnTWlg7+wC604ydGXA8VJiS5ap43JXiUFFAaQ==
You can also skip the host key validation, but only if you do not care about security (like if you are connecting within a private network).
<scp trust="true" .../>
See SCP Task documentation.
I am trying to copy files in remote serve to local using scp task in ant. The thing is, I want to exclude certain files with extension *.txt, so I tried using excludes tag. But it seems not to work. And It copies all the files including the files with extension *.txt
<scp file="username:pwd#remotemachine:/path/to/files/*" todir="copycontent" trust="true">
<fileset dir="files" >
<exclude name="**/*.txt"/>
</fileset>
</scp>
The Ant SCP task has some limitations for your scenario:
"FileSet only works for copying files from the local machine to a remote machine." (from the Ant SCP manual page)
The SCP element itself does not provide attributes for includes/excludes patterns
So options for selective copying from remote to local are limited. More flexibility for copying from local to remote (using fileset).
Rather than excluding *.txt, you could instead include one or more file patterns one or more scp blocks.
Or an alternative if the local system is unix-based could be to exec rsync, as suggested in this answer to a similar question.
what we need to know for copying the file other than ant Please explain in brief do we need to install anything like openssh or not needed or any other way without installing.
The SCP task allows you to copy files to remote hosts. It does not require openSSH on the machine running Ant (it uses jsch.jar as specified in ant library dependencies)- obviously, it (or another SSH Server) will be required on the target!
In my code am using something like
<sshexec host="somehost"
username="dude"
password="yo"
command="ftp_download"/>
<ftp action="get" server="server_todownload" userid="user" password="aaa" remotedir="dowload/dir">
<fileset dir="${installerdirectory}"><include name="${file_name}"/></fileset>
</ftp>
In "command" of sshexec how i can pass the ftp action as parameters??
is there any example or document is there to get more knowledge??
Thanks in advance
I guess you want to:
ssh to another machine, and
download something on that machine using ftp.
I think you misunderstood something. <sshexec> is a task that sshes to a machine and executes a command on that machine. The command it executes must exist on that machine. For example, your code:
<sshexec host="somehost" username="dude" password="yo" command="ftp_download"/>
There must be a command named ftp_download existing on somehost or your <sshexec> will fail. You can't use an ant task in your local build file as a parameter because <sshexec> doesn't work in this way at all.
Suggestion:
You can put the ftp task in an ant build file, and deploy it to the remote machine (you can use scp task) before sshexec. In sshexec's command, you write ant -f path_to_build_file ftp_target. You need to ensure that there is ant on that machine.
I would like to optimize my scp deployment which currently copies all files to only copy files that have changed since the last build. I believe it should be possible with the current setup somehow, but I don't know how to do this.
I have the following:
Project/src/blah/blah/ <---- files I am editing (mostly PHP in this case, some static assets)
Project/build <------- I have a local build step that I use to copy the files to here
I have an scp task right now that copies all of Project/build out to a remote server when I need it.
Is it possible to somehow take advantage of this extra "build" directory to accomplish what I want -- meaning I only want to upload the "diff" between src/** and build/**. Is it possible to somehow retrieve this as a fileset in ANT and then scp that?
I do realize that what it means is that if I somehow delete/mess around with files on the server in between, the ANT script would not notice, but for me this is okay.
You can tell ant scp to only copy files which have been modified since the last push using the modified tag like so:
<scp trust="true" sftp="true"... >
<fileset dir="${local.dir}">
<modified>
<param name="cache.cachefile" value="localdev.cache"/>
</modified>
</fileset>
</scp>
The first time you use this, it will send all files and cache the timestamps in the cachefile declared in the param. After that, it will only send the modified ones.
Tested and verified in sftp mode.
I think you need to use rsync instead. I found the following article that answers your question.
In a nutshell rsync will resume where it left off and it should be possible to tunnel it over ssh.