Connect sftp through python script

Hello,
Can you please help me to how to connect ftp server using python script in snaplogic

Thanks
Mahesh

Hi @Mahesh7225,

Is there a particular reason not to use the Read & Write snaps for manipulating files on FTP? If you decide to go through Python after all, you’ll need to use the ftplib library. You can find detailed information on how to use it on the following link:

ftplib — FTP protocol client

Regards,
Bojan

Hi bojanvelevski,

Thanks for reply , through read/write/directory browser snap, i am unable to connect to the ftp server (error :- “Unable to create filesystem object for sftp” , message:- “Failed to get SFTP session connected”) so trying to connect through script.

Thanks
Mahesh

Usually it is a syntax error in the SFTP path. It needs to follow the next convention:

sftp://server-name/main-directory/sub-directory/file.txt

If that is ok, re-check your account information.

i tried this but still getting error "Reason: _socket.error: [Errno 111] Connection refused in "

#from com.snaplogic.scripting.language import pysftp
#from import ScriptHook
from ftplib import FTP
from com.snaplogic.scripting.language import ScriptHook
from com.snaplogic.scripting.language.ScriptHook import *

myHostname = “hostname”
myUsername = “username”
myPassword = “password”

ftp = FTP(“hostname”,“22”)
ftp.login(“username”,“password”)"

Hi

Still i am facing issue to connect sftp server through script,
can anyone help me out to connect to sftp server through javascript or python.

Thanks in advance
Mahesh

Hello
i tried another code and still getting error "Reason: :42:4 Expected ; but found sftp let sftp = new Client(); ^ in "

// Ensure compatibility with both JDK 7 and 8 JSR-223 Script Engines
try { load(“nashorn:mozilla_compat.js”); } catch(e) { }
// Import the interface required by the Script snap.
importPackage(com.snaplogic.scripting.language);
// Import the serializable Java type we’ll use for the output data.
importClass(java.util.LinkedHashMap);
var impl = {
input : input,
output : output,
error : error,
log : log,

execute : function () {
   this.log.info("Executing Transform Script");
    while (this.input.hasNext()) {
        try {

var Client = require(‘ssh2-sftp-client’);
let sftp = new Client();
sftp.connect({
host: ‘hostname’,
port: ‘22’,
username: ‘user1’,
password: ‘Password1’
}).then(() => {
return sftp.list(’/files/upd/’);
}).then(data => {
console.log(data, ‘the data info’);
}).catch(err => {
console.log(err, ‘catch error’);
});
}
catch (err) {
var errDoc = new LinkedHashMap();
errDoc.put(“error”, err);
this.log.error(err);
this.error.write(errDoc);
}
}
this.log.info(“Script executed”);
},
cleanup : function () {
this.log.info(“Cleaning up”)
}
};

var hook = new com.snaplogic.scripting.language.ScriptHook(impl);