PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0
Showing posts with label sftp. Show all posts
Showing posts with label sftp. Show all posts

Monday, August 1, 2022

[FIXED] How to retrieve a file from a server via SFTP?

 August 01, 2022     ftp, java, security, sftp     No comments   

Issue

I'm trying to retrieve a file from a server using SFTP (as opposed to FTPS) using Java. How can I do this?


Solution

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();


Answered By - Cheekysoft
Answer Checked By - Mildred Charles (PHPFixing Admin)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, July 16, 2022

[FIXED] How to deploy ASP.NET MVC app via sftp

 July 16, 2022     asp.net-mvc, c#, sftp, visual-studio, web-deployment     No comments   

Issue

My university allows me to deploy my web app only via sftp. Visual Studio deployment tool does not support sftp. What's the best way to solve that problem?


Solution

You don't need the VS publish tool.

For a ASP.NET MVC site, you need to upload contents of the project folder, except for anything that will have been compiled... Open Visual Studio, compile your project, then upload via FTP.

Upload the bin folder with the compiled dll's, along with any views, scripts (anything that will be served to the client), and config files in the folder structure inside the project directory.

Basically, upload anything that doesn't have a .cs file extension. The exact files will vary based on your project. (You don't need .pdb files, or your .csproj file either)

If a folder contains only .cs files, you don't need it since it'll be compiled in the dll's (like the Controllers folder). If a folder contains anything else (.cshtml, .js, .css, etc) then upload it.

This can all be done via an FTP client like CuteFTP, FileZilla, Cyberduck etc. Just connect to your server and upload to the directory that is set up for your application in IIS.



Answered By - Erresen
Answer Checked By - Marilyn (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Saturday, May 14, 2022

[FIXED] How do I get the files from SFTP server and move them to another folder in bash script?

 May 14, 2022     bash, linux, sftp, shell, ubuntu     No comments   

Issue

How do I get the one by one files from SFTP server and move them do another folder in Ubuntu bash script?

#!bin/sh
FOLDER=/home/SFTP/Folder1/    

sftp SFTP@ip_address    
cd /home/FSTP/Folder1/    
for file in "$FOLDER"*
<<EOF
cd /home/local/Folder1
get $file
EOF
mv $file /home/SFTP/Done
done

I know it's not right, but i've tried my best and if anyone can help me, i will appreciate it. Thanks in advance.


Solution

OpenSSH sftp is not very powerful client for such tasks. You would have to run it twice. First to collect list of files, use the list to generate list of commands, and execute those in a second run.

Something like this:

# Collect list of files
files=`sftp -b - user@example.com <<EOF
cd /source/folder
ls
EOF`
files=`echo $files|sed "s/.*sftp> ls//"` 

# Use the list to generate list of commands for the second run
(
  echo cd /source/folder
  for file in $files; do
    echo get $file
    echo rename $file /backup/folder/$file
  done
) | sftp -b - user@example.com

Before you run the script on production files, I suggest, you first output the generated command list to a file to check, if the results are as expected.

Just replace the last line with:

) > commands.txt


Answered By - Martin Prikryl
Answer Checked By - Pedro (PHPFixing Volunteer)
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg

Monday, February 7, 2022

[FIXED] Unable to connect with the public key you've provided. Php | Codeigniter

 February 07, 2022     codeigniter, php, sftp, ssh2-sftp     No comments   

Issue

Unable to connect with the public key you've provided.Unable to connect with the public key you've provided.

i am using codeigniter 3.1.x . Keys are valid . I have test the keys using filezilla client. Everything is working but here in php codeigniter am getting below error. Any buddy there who can help me to resolve this error. Or Can suggest any other php library with proper guidence. Thanks in advace

 function sftp()
    {
     $this->load->library('SFTP');
     $private_key=APPPATH.'sftp_keys/new_private.ppk';
     $public_key=APPPATH.'sftp_keys/new_public';
     $config=array();
     $config['hostname']='host';
     $config['username']='username';
     $config['port']='22';
     $config['public_key_url']=$public_key;
     $config['private_key_url']=$private_key;
     $config['method']='auth';
     $config['debug']=TRUE;
     $config['login_via_key']=TRUE;
     $this->sftp->initialize($config);
     
     $connect=$this->sftp->connect($config);
     var_dump($connect);
     $this->ftp->close();
    }

var_dump($config)

array(8) { 
  ["hostname"]=> string(13) "" 
  ["username"]=> string(7) "" 
  ["port"]=> string(2) "22" 
  ["public_key_url"]=> string(62) "/home/sgfasaco/public_html/qa/application/sftp_keys/new_public" 
  ["private_key_url"]=> string(71) "/home/sgfasaco/public_html/qa/application/sftp_keys/id_rsa(private).ppk" 
  ["method"]=> string(3) "key" 
  ["debug"]=> bool(true) 
  ["login_via_key"]=> bool(true)
} 

Solution

Yes i got the solution by trying different libraries. I found the best one working perfectly Flysystem Filesystem Abstraction for PHP (PHP library).

https://flysystem.thephpleague.com/v2/docs/

install the library using command.

composer require league/flysystem-sftp:^2.0

use below code to connect with SFTP using Filesystem.

<?php
require_once('vendor/autoload.php');
use League\Flysystem\Filesystem;
use League\Flysystem\PhpseclibV2\SftpConnectionProvider;
use League\Flysystem\PhpseclibV2\SftpAdapter;
use League\Flysystem\UnixVisibility\PortableVisibilityConverter;
use League\Flysystem\DirectoryAttributes;
use League\Flysystem\FileAttributes;
$private_key='sftp_keys/new_private.ppk';

$filesystem = new Filesystem(new SftpAdapter(
    new SftpConnectionProvider(
        'host', // host (required)
        'username', // username (required)
        null, // password (optional, default: null) set to null if privateKey is used
        $private_key, // private key (optional, default: null) can be used instead of password, set to null if password is set
        null, // passphrase (optional, default: null), set to null if privateKey is not used or has no passphrase
        22, // port (optional, default: 22)
        false, // use agent (optional, default: false)
        30, // timeout (optional, default: 10)
        10, // max tries (optional, default: 4)
        null, // host fingerprint (optional, default: null),
        null // connectivity checker (must be an implementation of 'League\Flysystem\PhpseclibV2\ConnectivityChecker' to check if a connection can be established (optional, omit if you don't need some special handling for setting reliable connections)
    ),
    '/', // root path (required)
    PortableVisibilityConverter::fromArray([
        'file' => [
            'public' => 0640,
            'private' => 0604,
        ],
        'dir' => [
            'public' => 0740,
            'private' => 7604,
        ],
    ])
));
$allFiles = $filesystem->listContents('Outbound')->toArray();
$response = $filesystem->write('Outbound/info.txt', 'Hello How are you',array());
if($response){
    echo "Success";
}else echo "Error";
print_r($allFiles );
?>

Composer.json looks like

{
    "name": "league/flysystem-sftp",
    "description": "Flysystem adapter for SFTP",
    "license": "MIT",
    "authors": [
        {
            "name": "Frank de Jonge",
            "email": "info@frenky.net"
        }
    ],
    "require": {
        "php": ">=5.6.0",
        "league/flysystem-sftp": "^2.1",
        "phpseclib/phpseclib": "~2.0"
    },
    "require-dev": {
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "^5.7.25"
    },
    "autoload": {
        "psr-4": {
            "League\\Flysystem\\Sftp\\": "src/"
        }
    }
}


Answered By - Asad Ali
Read More
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Older Posts Home

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
All Comments
Atom
All Comments

Copyright © PHPFixing