Jump to content
Claris Engage 2025 - March 25-26 Austin Texas ×

This topic is 3960 days old. Please don't post here. Open a new topic instead.

Recommended Posts

Posted

So here's code for sending or receiving a file through SCP (SSH secure copy) with ScriptMaster.

Requires the Orion SSH-2 java library from http://sourceforge.net/apps/mediawiki/orion-ssh2/index.php .

They can be easily adapted to use put/get methods that will get or put multiple files at once. Comments and suggestions always welcome.

 

 

SCPGet ( hostname ; privateKey ; username ; localTargetDirectory ; remoteFile )

import java.io.IOException;

import com.trilead.ssh2.Connection;
import com.trilead.ssh2.SCPClient;

try {

// Convert private key to character array
char[] privateKeyArray = privateKey.toCharArray();

// Create and open connection
Connection theConnection = new Connection(hostname);
theConnection.connect();

// Authenticate with public (private really) key
boolean isSuccess = theConnection.authenticateWithPublicKey(username, privateKeyArray, null);
if (isSuccess == false)
throw new RuntimeException( "ERROR: Authentication error" );

// Create SCP client and get remote file to local target directory
SCPClient theClient = new SCPClient (theConnection);
theClient.get(remoteFile, localTargetDirectory);

// Close the connection 
theConnection.close();   

return;
}
catch (IOException e) {
throw new RuntimeException( e );
}

SCPPut ( privateKey ; hostname ; username ; remoteTargetDirectory ; localFile )

import java.io.IOException;

import com.trilead.ssh2.Connection;
import com.trilead.ssh2.SCPClient;

try {

// Convert private key to character array
char[] privateKeyArray = privateKey.toCharArray();

// Create and open connection
Connection theConnection = new Connection(hostname);
theConnection.connect();

// Authenticate with public (private really) key
boolean isSuccess = theConnection.authenticateWithPublicKey(username, privateKeyArray, null);
if (isSuccess == false)
throw new RuntimeException( "ERROR: Authentication error" );

// Create SCP client and put local file to remote target directory with mode 600
SCPClient theClient = new SCPClient (theConnection);
theClient.put(localFile, remoteTargetDirectory);

// Close the connection 
theConnection.close();   

return;
}
catch (IOException e) {
throw new RuntimeException( e );
}

Code on,

JP

This topic is 3960 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.