Ant scp command with permissions - ant

I am working with ANT 'SCP' command but it doesn't retain the permission on SCP. So I thought I would use SCP on unix machine as executable as work around but I am wondering how I set the password on exec command? Any ideas on How work on it?
<exec executable="scp" failonerror="true">
<arg value="-r"/>
<arg value="tcbuild#pghlinux6:${checkoutDir}/Package/"/>
<arg value="/licregs.140"/>
</exec>

I got away with this one in ANT. By Using sshexec to remotely change permissions and using SCP command.

Try using an authorized key. If the key is authorized, then you don't need the password. If the key has a password you can use ssh-agent to retain a copy of the decripted key.

There's an ant bug open for the fact that the scp hardcodes the permissions of the created files. If sshexec isn't an option, there's a patch attached to the bug which could be applied to your local ant which allows scp to take chmod and umask options, like the ftp task does.

Related

Unable to transfer file with <scp> Ant task and username containing spaces

I am using the following Ant script with username and IP address. I want to transfer a file. If username has a space in it, the file is not transferring.
<project name="Hello World Project" default="info">
<property file="build.properties"/>
<target name="info">
<scp file="/home/smith/Gjs/ws2/abc.txt"
todir="Srupanan k:rakesh#19.1.21.6:~/" trust="true"/>
</target>
</project>
Is there any way to use <scp> with a username containing a space?
I checked the source code and from what I saw, the ant task does not mind spaces at all, it only checks for the : and # in the toDir.
There might be a problem in the Jsch library it uses to handle the ssh connection, although I doubt it would be easy to debug and fix. You should however make sure you're using an up-to-date installation of ant.
You could also try to use the sftp flag of the ant task in hope that the sftp implementation will handle spaces better.
If all fails you should still be able use an exec task to upload via a native scp call.
Good luck !

BUILD FAILED: C:\Users\USER\.ssh\known_hosts (The system cannot find the file specified)

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.

sshexec command - ftp get as parametes

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.

Ant exec task as another user

I would like to execute a shell script from an ant build (I saw the exec task seems to do it), but this script has to be executed from a user different thant the one launching ant. Is there a way to do this?
You could use the sshsexec task. Connect locally or to a remote machine:
<sshexec host="localhost"
username="dude"
password="yo"
command="touch somefile"/>
This task will require the optional jsch.jar to be installed in your ANT lib.
Specifying a pirvate key would enable a password-less login.
Couldn't you execute, from ant, a script that executes the actual script using sudo? See How to pass the password to su/sudo/ssh without overriding the TTY? for how to pass the password from the command line.

Ant: Unable to process environment variables set and working directoy

I need to set some 10 environment variables before my CMAKE can run. This is what I am doing
Top of my build.xml has the following
<property environment="env" />
Creating a batch file with all the environment variables. I have something like this in my ANT Build.xml
Calling runCompile
I then try to call CMAKE
Problems i am facing are:
I am unable to get any of the environment variables ex: when i try to print ${env.CMAKE_COMMAND} it prints ${env.CMAKE_COMMAND} and not value
When i try to goto working directory and execute the batch file, this also does not work.
Any idea how I can achieve the above?
Sorry I am unable to paste the XML code here. Hence attached as image.
Looking at the exec task documentation you will find a hint about the nested element env. With this it should be possible to specify environment variables to pass to the system command, like shown in the example:
<exec executable="emacs">
<env key="DISPLAY" value=":1.0"/>
</exec>

Resources