I am using JSch API to connect to remote server through SFTP. I need to get a copy of a folder which is exist in the remote server into the same server. Is there any method implemented to do this kind of things in JSch? Or be kind to give me an advice to do the above use-case. (I am working in Scala).
We can not use "sftp" channel to do this task ans we have to use "exec" channel to do this task. Using "exec" channel we can execute Linux commands as follows,
val command = "mkdir testDir"
val channelExec: ChannelExec = session.openChannel("exec").asInstanceOf[ChannelExec]
channelExec.setCommand(command);
channelExec.connect
Go through the following links to get more details
http://www.programcreek.com/java-api-examples/index.php?api=com.jcraft.jsch.ChannelExec
http://www.journaldev.com/246/java-program-to-run-shell-commands-on-ssh-enabled-system
http://www.jcraft.com/jsch/examples/Exec.java.html
Thank you for all participants
Support for copying files remotely is rare in SFTP servers. There's copy-file extension to SFTP, but few servers/clients do support it.
See draft-ietf-secsh-filexfer-extensions-00.
In the most widespread OpenSSH SFTP server it is supported only by very recent version 9.0. And JSch does not support it at all.
Alternatives:
Download the folder and reupload it to a new location (pure SFTP solution)
Use cp command in a "exec" channel (not SFTP anymore, requires shell access)
Related
One of the requirements is to keep remote Windows Server intact.
No third party software allowed (no WinSCP, etc).
So we configure Windows Server with WinRM and allow remote access, AllowUnencrypted=true, Auth basic=true, etc...
Then we create job and execute command on Windows server like "ifconfig" successfully.
When it comes to executing inline script or copying file - Rundeck is trying to copy script/file to remote Windows server.
By default:
plugin.script-copy.default.command=get-services
where "get-services" seems to be free-form text rather than executable.
If we want to use SCP or SSH instead, here we have problem -> Windows Server doesn't have WinSCP or SSH or Python installed by default.
Is there any way to copy/deliver script to target/remote Windows Server 2008 using embedded capabilities only (no third-party software allowed) ?
Versions:
Rundeck 2.6.2 running on Linux
Windows Server 2008 R2 Enterprise, Service Pack 1
Thank you.
You can use the WinRM plugin (AKA "Overthere WinRM"), configure it, and use the copy file step on your job workflow (keep in mind that you need the 1.3.4 WinRM plugin at least which support copy file).
You need to download the plugin and put it in Rundeck the libext directory.
Add the Windows resources.xml entry (for "Overthere" WinRM plugin):
<node name="windows" description="Windows node" tags="" hostname="192.168.1.81" osArch="x86" osFamily="windows" osName="Windows 2008R2" osVersion="2008" username="user" winrm-protocol="http" winrm-auth-type="basic" winrm-cmd="CMD" winrm-password-storage-path="keys/winpasswd"/>
Set WinRM as your default node executor / default node file copier, and use the copy file step on your workflow like this.
So, this is important: the WinRM plugin isn't in active development (and Rundeck 2.6 branch is out of support/maintenance), the best way to deal with this is to move to the latest Rundeck version and use the PyWinRM plugin (out of the box with Rundeck, on active development and easiest to configure compared by the old "Overthere" WinRM plugin) and use the copy step as the same way.
I have scala application with akka steams. So the flow of my application is like this:
1. Check if file exists on FTP - I'm doing it with the org.apache.commons.net.ftp.FTPClient
2. If it exists stream it via alpakka library(and make some stream transformations)
My application works locally and it can connect to the server.
The problem is when it is being deployed to dcos/mesos. I get an issue:
java.io.IOException: /path/file.txt: No such file or directory
I can say for sure that file still exists there. Also when I try to connect from docker container locally through the ftp I've got something like this:
ftp> open some.ftp.address.com
Connected to some.ftp.address.com.
220 Microsoft FTP Service
Name (some.ftp.address.com:root): USER
331 Password required
Password:
230 User logged in.
Remote system type is Windows_NT.
ftp> dir
501 Server cannot accept argument.
ftp: bind: Address already in use
ftp>
Not sure if its still helpful but I also got my ftp client transfering data from inside a Docker container after changing the data connection to passive. I think that active mode requires the client to have open ports which the server connects to when returning file listing results and during data transfer. However the client ports are not reachable from outside of the docker container since the requests are not routed through (like in a NAT:et network).
Found this post explaning active/passive FTP connections
https://labs.daemon.com.au/t/active-vs-passive-ftp/182
So my problem was really weird. But I've managed to fix this way.
Quick answer: I was using alpakka ftp lib this way:
Ftp
.fromPath(url, user, pass, Paths.get(s"/path/$fileName"))
But using this way it works:
val ftpSettings = FtpSettings(
host = InetAddress.getByName(url),
port = 21,
NonAnonFtpCredentials(user, pass),
binary = true,
passiveMode = true
)
Ftp
.fromPath(Paths.get(s"/path/$fileName"), ftpSettings)
Longer answer: I started investigating alpakka lib and I've discovered that it uses the same lib that works for me during checking if file exists!
https://github.com/akka/alpakka/blob/master/ftp/src/main/scala/akka/stream/alpakka/ftp/impl/FtpOperations.scala
So I've started digging and it seems that most likely tahat setting passive mode to true was the solution. But it's weird because I've read that windows ftp server does not support passive mode...
I hope someone could clarify my doubts one day, but at the moment I'm happy because it works :)
all, I'm new to this site, and Linux. I've just installed fluentd on Linux Mint. I want to use it to tale .evl logs at remote sites (by ip address) on our network, and send an email when a certain phrase appears. I'm reading on how to set up the tail input plugin. However, in the source section of fluent.conf, how do I specify the path for a remote file?
Fluentd as of Aug 2015, doesn't support tailing the remote files (#see in_tail plugin). You probably need to copy the files from remote to local via scp or rsync. Or you can post the data remotely via http (#see in_http plugin)
Let say I have a file /home/user/dir1/file.txt on a remote SFTP server. I want to copy this file to lets say /home/user/dir2/file.txt, while I am in a sftp session (I have server which allows only sftp connections, but no ssh connections!) connected to this server?
Is it possible to do so? If yes, what is the command?
For example the following command would rename move a file from one directory to another on the remote server.
sftp> rename dir1/file.txt dir2/file.txt
I am looking for a command which would copy a file from one directory to another on the remote server, if one exists.
The work around is to download the file from first location and upload to the second location, but that is not an option when the file in question is a big one and one is working with a slow network connection!
Not sure what you mean by the "command". Are you referring to an SFTP protocol request? Or a command of some scriptable/command-line SFTP client (e.g. the OpenSSH sftp)?
The SFTP protocol on its own does not allow duplication of a remote file. Though there's an optional extension of the protocol named copy-file that serves the purpose.
Quoting the copy-file extension specification:
6. Copying Remote Files
byte SSH_FXP_EXTENDED
uint32 request-id
string "copy-file"
string source-file
string destination-file
bool overwrite-destination
This request copies a file from one location to another on the server. The server responds with SSH_FXP_STATUS.
Not many SFTP servers support the extension though. I know that ProFTPD mod_sftp and Bitvise WinSSHD do. The OpenSSH supports related copy-data only in very recent version 9.0.
I have a website to which I have FTP access only (otherwise I'd use rsync for this) and I'd like to keep a local copy of it. At the moment I run the following wget command every once in a while
wget -m --ftp-user=me --ftp-password=secret ftp://my.server.com
When there are many updates it does get tedious with wget only having one connection at a time. I read about aria2 but couldn't find any hints as to answer the questions whether it would be possible to use aria2 as a replacement for this purpose?
No, according to the aria2 docs the option for downloading only newer files only works with http(s).
--conditional-get[=true|false]
Download file only when the local file is older than remote file. This function only works with HTTP(S) downloads only. It does not work if file size is specified in Metalink.