net.schmizz.sshj.xfer.scp.SCPFileTransfer Java Examples
The following examples show how to use
net.schmizz.sshj.xfer.scp.SCPFileTransfer.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SshMachine.java From karamel with Apache License 2.0 | 6 votes |
@Override public void downloadRemoteFile(String remoteFilePath, String localFilePath, boolean overwrite) throws KaramelException, IOException { connect(); SCPFileTransfer scp = client.newSCPFileTransfer(); File f = new File(localFilePath); f.mkdirs(); // Don't collect logs of values, just overwrite if (f.exists()) { if (overwrite) { f.delete(); } else { throw new KaramelException(String.format("%s: Local file already exist %s", machineEntity.getId(), localFilePath)); } } // If the file doesn't exist, it should quickly throw an IOException scp.download(remoteFilePath, localFilePath); }
Example #2
Source File: Main.java From aedict with GNU General Public License v3.0 | 5 votes |
private void upload() throws Exception { System.out.println("Uploading"); final SSHClient ssh = new SSHClient(); ssh.loadKnownHosts(); String password = config.password; if (password == null) { System.out.println("Enter password"); final Scanner s = new Scanner(System.in); password = s.nextLine(); if (MiscUtils.isBlank(password)) { throw new RuntimeException("Invalid password: blank"); } } System.out.println("Connecting"); ssh.connect("rt.sk"); try { System.out.println("Authenticating"); ssh.authPassword("moto", password); System.out.println("Uploading version"); final String targetFName = REMOTE_DIR + "/" + config.getTargetFileName(); exec(ssh, "echo `date +%Y%m%d` >" + REMOTE_DIR + "/" + config.getTargetFileName() + ".version"); exec(ssh, "rm -f " + targetFName); System.out.println("Uploading"); final SCPFileTransfer ft = ssh.newSCPFileTransfer(); ft.upload(config.getTargetFileName(), targetFName); } finally { ssh.disconnect(); } }
Example #3
Source File: ConfiguratorOnCreationTest.java From roboconf-platform with Apache License 2.0 | 5 votes |
@Test public void testPrepareConfiguration() throws Exception { // Prepare the mocks File tmpDir = this.folder.newFolder(); TargetHandlerParameters parameters = new TargetHandlerParameters() .targetProperties( new HashMap<String,String>( 0 )); SSHClient ssh = Mockito.mock( SSHClient.class ); SCPFileTransfer scp = Mockito.mock( SCPFileTransfer.class ); Mockito.when( ssh.newSCPFileTransfer()).thenReturn( scp ); // Invoke the method EmbeddedHandler embedded = new EmbeddedHandler(); embedded.karafData = this.folder.newFolder().getAbsolutePath(); ConfiguratorOnCreation configurator = new ConfiguratorOnCreation( parameters, "ip", "machineId", embedded ); Map<String,String> keyToNewValue = configurator.prepareConfiguration( parameters, ssh, tmpDir ); // We made one upload for this method! Mockito.verify( scp ).upload( Mockito.any( LocalSourceFile.class ), Mockito.anyString()); // Prevent a compilation warning about leaks configurator.close(); // Verify the map's content Assert.assertEquals( 4, keyToNewValue.size()); Assert.assertEquals( "", keyToNewValue.get( AGENT_APPLICATION_NAME )); Assert.assertEquals( "", keyToNewValue.get( AGENT_SCOPED_INSTANCE_PATH )); Assert.assertEquals( "", keyToNewValue.get( AGENT_DOMAIN )); Assert.assertEquals( "file:" + DEFAULT_SCP_AGENT_CONFIG_DIR + "/" + USER_DATA_FILE, keyToNewValue.get( AGENT_PARAMETERS )); }
Example #4
Source File: ConfiguratorOnTerminationTest.java From roboconf-platform with Apache License 2.0 | 5 votes |
@Test public void testPrepareConfiguration() throws Exception { // Prepare the mocks File tmpDir = this.folder.newFolder(); TargetHandlerParameters parameters = new TargetHandlerParameters() .targetProperties( new HashMap<String,String>( 0 )); SSHClient ssh = Mockito.mock( SSHClient.class ); SCPFileTransfer scp = Mockito.mock( SCPFileTransfer.class ); Mockito.when( ssh.newSCPFileTransfer()).thenReturn( scp ); // Invoke the method EmbeddedHandler embedded = new EmbeddedHandler(); embedded.karafData = this.folder.newFolder().getAbsolutePath(); ConfiguratorOnTermination configurator = new ConfiguratorOnTermination( parameters, "ip", "machineId", embedded ); Map<String,String> keyToNewValue = configurator.prepareConfiguration( parameters, ssh, tmpDir ); // There is no upload by this method Mockito.verifyZeroInteractions( scp ); // Prevent a compilation warning about leaks configurator.close(); // Verify the map's content Assert.assertEquals( 4, keyToNewValue.size()); Assert.assertEquals( "", keyToNewValue.get( AGENT_APPLICATION_NAME )); Assert.assertEquals( "", keyToNewValue.get( AGENT_SCOPED_INSTANCE_PATH )); Assert.assertEquals( "", keyToNewValue.get( AGENT_DOMAIN )); Assert.assertEquals( Constants.AGENT_RESET, keyToNewValue.get( AGENT_PARAMETERS )); }
Example #5
Source File: ConfiguratorOnCreationTest.java From roboconf-platform with Apache License 2.0 | 4 votes |
@Test public void testUpdateAgentConfigurationFile() throws Exception { // Prepare the mocks File tmpDir = this.folder.newFolder(); File agentConfigurationFile = new File( tmpDir, Constants.KARAF_CFG_FILE_AGENT ); Properties props = new Properties(); props.setProperty( "a0", "c0" ); props.setProperty( "a1", "c213" ); props.setProperty( "a2", "c2" ); props.setProperty( "a3", "c3" ); props.setProperty( "a4", "c4" ); props.setProperty( "a5", "c5" ); Utils.writePropertiesFile( props, agentConfigurationFile ); TargetHandlerParameters parameters = new TargetHandlerParameters() .targetProperties( new HashMap<String,String>( 0 )); final Map<String,String> keyToNewValue = new HashMap<> (); keyToNewValue.put( "a1", "b1" ); keyToNewValue.put( "a2", "b2" ); keyToNewValue.put( "a3", "b3" ); SSHClient ssh = Mockito.mock( SSHClient.class ); SCPFileTransfer scp = Mockito.mock( SCPFileTransfer.class ); Mockito.when( ssh.newSCPFileTransfer()).thenReturn( scp ); // Invoke the method EmbeddedHandler embedded = new EmbeddedHandler(); embedded.karafData = this.folder.newFolder().getAbsolutePath(); ConfiguratorOnCreation configurator = new ConfiguratorOnCreation( parameters, "ip", "machineId", embedded ); configurator.updateAgentConfigurationFile( parameters, ssh, tmpDir, keyToNewValue ); // Verify ArgumentCaptor<String> remotePathCaptor = ArgumentCaptor.forClass( String.class ); ArgumentCaptor<FileSystemFile> fileCaptor = ArgumentCaptor.forClass( FileSystemFile.class ); Mockito.verify( scp ).download( remotePathCaptor.capture(), fileCaptor.capture()); Assert.assertEquals( tmpDir, fileCaptor.getValue().getFile()); Assert.assertEquals( new File( DEFAULT_SCP_AGENT_CONFIG_DIR, Constants.KARAF_CFG_FILE_AGENT ).getAbsolutePath(), remotePathCaptor.getValue()); // 1st: we upload the user data // 2nd: we reupload the same file than the one we downloaded ArgumentCaptor<String> remotePathCaptor2 = ArgumentCaptor.forClass( String.class ); ArgumentCaptor<FileSystemFile> fileCaptor2 = ArgumentCaptor.forClass( FileSystemFile.class ); Mockito.verify( scp ).upload( fileCaptor2.capture(), remotePathCaptor2.capture()); Assert.assertEquals( agentConfigurationFile.getAbsolutePath(), fileCaptor2.getValue().getFile().getAbsolutePath()); Assert.assertEquals( DEFAULT_SCP_AGENT_CONFIG_DIR, remotePathCaptor2.getValue()); // And no additional call Mockito.verifyNoMoreInteractions( scp ); Mockito.verify( ssh, Mockito.times( 2 )).newSCPFileTransfer(); Mockito.verifyNoMoreInteractions( ssh ); // Verify the properties were correctly updated in the file Properties readProps = Utils.readPropertiesFile( agentConfigurationFile ); Assert.assertEquals( "c0", readProps.get( "a0" )); Assert.assertEquals( "b1", readProps.get( "a1" )); Assert.assertEquals( "b2", readProps.get( "a2" )); Assert.assertEquals( "b3", readProps.get( "a3" )); Assert.assertEquals( "c4", readProps.get( "a4" )); Assert.assertEquals( "c5", readProps.get( "a5" )); Assert.assertEquals( props.size(), readProps.size()); // Prevent a compilation warning about leaks configurator.close(); }