How to Retrieve a File from a Server Via Sftp

How to retrieve a file from a server via SFTP?

Another option is to consider looking at the JSch library. JSch seems to be the preferred library for a few large open source projects, including Eclipse, Ant and Apache Commons HttpClient, amongst others.

It supports both user/pass and certificate-based logins nicely, as well as all a whole host of other yummy SSH2 features.

Here's a simple remote file retrieve over SFTP. Error handling is left as an exercise for the reader :-)

JSch jsch = new JSch();

String knownHostsFilename = "/home/username/.ssh/known_hosts";
jsch.setKnownHosts( knownHostsFilename );

Session session = jsch.getSession( "remote-username", "remote-host" );
{
// "interactive" version
// can selectively update specified known_hosts file
// need to implement UserInfo interface
// MyUserInfo is a swing implementation provided in
// examples/Sftp.java in the JSch dist
UserInfo ui = new MyUserInfo();
session.setUserInfo(ui);

// OR non-interactive version. Relies in host key being in known-hosts file
session.setPassword( "remote-password" );
}

session.connect();

Channel channel = session.openChannel( "sftp" );
channel.connect();

ChannelSftp sftpChannel = (ChannelSftp) channel;

sftpChannel.get("remote-file", "local-file" );
// OR
InputStream in = sftpChannel.get( "remote-file" );
// process inputstream as needed

sftpChannel.exit();
session.disconnect();

How to take data from SFTP server without downloading files in Python?

Yes and no. You can use the data from the remote (SFTP) server without storing the files to a local disk.

But you cannot use data locally without downloading them. That's impossible. You have to transfer the data to use them – at least to a memory of the local machine.

See A way to load big data on Python from SFTP server, not using my hard disk.

My answer there talks about Paramiko. But pysftp is a just a thin wrapper around Paramiko. Its Connection.open is directly mapped to underlying Paramiko's SFTPClient.open. So you can keep using pysftp:

with sftp.open('filename.txt', bufsize=32768) as f:
# use f as if you have opened a local file with open()

Though I'd recommend you not to: pysftp vs. Paramiko.

Get multiple latest files from sftp server

With a current bash and a for loop:

destdir="/tmp"

# get last 50 file names and save in array fileName
mapfile -t fileName < <(echo "ls -1tr" | sftp myid@removeserver | tail -50)

# get files from array fileName and save in $destdir
for f in "${fileName[@]}"; do echo "get \"$f\" \"$destdir\""; done | sftp myid@removeserver

I assume that the file names do not contain line breaks.

retrieving list of files from SFTP server in java

Do you know which protocol your server is actually using?

You didn't specify what library you were using, but it looks like commons-net. I see that commons-net does have an FTPSClient class, but that is for FTPS, not SFTP.

Assuming you're talking about SFTP, I use the library JSCH.

PHP download from remote server via sftp

Here is a small code on how to read the folder and download all its files:

<?php
$host = 'localhost';
$port = 22;
$username = 'username';
$password = 'password';
$remoteDir = '/must/be/the/complete/folder/path';
$localDir = '/can/be/the/relative/or/absolute/local/path';

if (!function_exists("ssh2_connect"))
die('Function ssh2_connect not found, you cannot use ssh2 here');

if (!$connection = ssh2_connect($host, $port))
die('Unable to connect');

if (!ssh2_auth_password($connection, $username, $password))
die('Unable to authenticate.');

if (!$stream = ssh2_sftp($connection))
die('Unable to create a stream.');

if (!$dir = opendir("ssh2.sftp://{$stream}{$remoteDir}"))
die('Could not open the directory');

$files = array();
while (false !== ($file = readdir($dir)))
{
if ($file == "." || $file == "..")
continue;
$files[] = $file;
}

foreach ($files as $file)
{
echo "Copying file: $file\n";
if (!$remote = @fopen("ssh2.sftp://{$stream}/{$remoteDir}{$file}", 'r'))
{
echo "Unable to open remote file: $file\n";
continue;
}

if (!$local = @fopen($localDir . $file, 'w'))
{
echo "Unable to create local file: $file\n";
continue;
}

$read = 0;
$filesize = filesize("ssh2.sftp://{$stream}/{$remoteDir}{$file}");
while ($read < $filesize && ($buffer = fread($remote, $filesize - $read)))
{
$read += strlen($buffer);
if (fwrite($local, $buffer) === FALSE)
{
echo "Unable to write to local file: $file\n";
break;
}
}
fclose($local);
fclose($remote);
}

You can also resume this code to (it will not copy directories):

while (false !== ($file = readdir($dirHandle)))
{
if ($file == "." || $file == "..")
continue;

echo "Copying file: $file\n";
if(!ssh2_scp_recv($connection, $remoteDir . $file, $localDir . $file))
echo "Could not download: ", $remoteDir, $file, "\n";
}

If you do not use the full path on the remote folder it will not work:

opendir("ssh2.sftp://{$stream}{$remoteDir}")


Related Topics



Leave a reply



Submit