How to send a file using SFTP

Michael Dziubin
Kilo Contributor

I want to send a csv file from ServiceNow to other server using SFTP. I tried j2ssh package:

var ssh = Packages.com.sshtools.j2ssh.SshClient(); 
var ignore = Packages.com.sshtools.j2ssh.transport.IgnoreHostKeyVerification(); 
var pwd = Packages.com.sshtools.j2ssh.authentication.PasswordAuthenticationClient();
pwd.setUsername(this.username);
pwd.setPassword(this.password);

var properties = Packages.com.sshtools.j2ssh.configuration.SshConnectionProperties();
properties.setHost(this.hostname);
properties.setPort(this.port);
var output = Packages.java.io.ByteArrayOutputStream();

ssh.connect(properties, ignore);

At first I tried to establish SSH connection and then switch to SFTP but I'm always getting error at ssh.connect line:

Evaluator: com.sshtools.j2ssh.transport.TransportProtocolException: The connection did not complete

Any advice how to connect to an SFTP? Maybe there are other packages for it in ServiceNow, btw I don't have a Mid-Server.

12 REPLIES 12

Omkar Mone
Mega Sage

Hi Michael,

 

 Following links should be helpful. These links help you to upload file to sftp server but you can use some other function to retrieve files from sftp server:

 

Push .csv file from mid server to an SFTP location

 

Send Attachment to SFTP Location

 

 

 

Mark Correct if it helps.

Warm Regards,

Omkar Mone

find_real_file.png

www.dxsherpa.com

I mentioned in my question that I don't have a Mid-Server so these links are irrelevant .

Hi 

Just make sure that your firewall is allowing traffic/connection from servicenow.

Goto Data Source (system Import Sets->Data Source) and create new Data source with below configuration.

 

  1. Name - You can put any name of your data source. If your organization is following any naming convention, follow that.
  2. Import set table label - Add name of the import set table. This will auto create an import set table. This is the staging table where your file will be recorded. If you already have the import set table created then you can put that name in the import set table name field.
  3. Type - There are 3 options LDAP, File and JDBC for the type. Select the type as File.
  4. Format - There are multiple format. In this case we are loading the csv file. So select the format as CSV.
  5. Zipped - If the source file is zipped then check the Zipped checkbox.
  6. File retrieval methods - Select as SFTP.
  7. FilePath - This is the path where your file is kept in Kiteworks. Let's say the file name is TestFile.csv and is stored in TestFolder. Then the FilePath will be /TestFolder/TestFile.csv
  8. Server - This is the server name of the Kiteworks.
  9. Username - Name of the user. This user should have access to read the file that is stored in respective folder. Select the username that is setup in above Kiteworks profile setup.
  10. Password - Password of the user profile.

Submit this record and reopen the same record. Once you reopen you will find that a connection URL has been generated automatically. Now you are ready to check the connection.
Scroll Down the form and you will see that there are few related links. Click on the Test load 10 records. Once you click on this link, servicenow integration engine will try to connect to SFTP Server and will fetch the file that you have configured in the data source settings. If the connection is successful then data in csv file will be loaded in the configured import set table. Now you can setup the transform map and process the data accordingly.

 

Mark Correct if it helps.

Warm Regards,

Omkar Mone

find_real_file.png

www.dxsherpa.com

 

Thank you for this answer but I need a solution that can be implemented in a script, I want to transfer temporary file created in code. Unfortunately I can't create a permanent file from a script.