本文整理汇总了Java中net.schmizz.sshj.SSHClient类的典型用法代码示例。如果您正苦于以下问题:Java SSHClient类的具体用法?Java SSHClient怎么用?Java SSHClient使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
SSHClient类属于net.schmizz.sshj包,在下文中一共展示了SSHClient类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: uploadFile
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
@Test
public void uploadFile() throws IOException, NoSuchProviderException, NoSuchAlgorithmException {
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(SecurityUtils.getFingerprint(subject.getHostsPublicKey()));
ssh.connect("localhost", subject.getPort());
try {
ssh.authPublickey("this_is_ignored", new KeyPairWrapper(
KeyPairGenerator.getInstance("DSA", "SUN").generateKeyPair()));
final SFTPClient sftp = ssh.newSFTPClient();
File testFile = local.newFile("fred.txt");
try {
sftp.put(new FileSystemFile(testFile), "/fred.txt");
} finally {
sftp.close();
}
} finally {
ssh.disconnect();
}
assertThat(sftpHome.getRoot().list(), is(new String[]{"fred.txt"}));
}
示例2: executeCommand
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
@Override
public String executeCommand(ShellCommand aCommand) throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost", 2222);
try {
ssh.authPublickey("root");
try (Session session = ssh.startSession()) {
Session.Command command = session.exec(aCommand.getCommand());
String text = IOUtils.readFully(command.getInputStream()).toString();
command.join(5, TimeUnit.SECONDS);
return text;
}
} finally {
ssh.disconnect();
}
}
示例3: transferEvents
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
private void transferEvents(SSHClient sshClient, List<MotionEvent> motionEvents) throws IOException, ParseException {
for (MotionEvent motionEvent : motionEvents) {
ByteArraySourceFile byteArraySourceFile = new ByteArraySourceFile.Builder()
.fileData(motionEvent.getImage())
.name(motionEvent.getId() + ".jpg")
.build();
String day = concurrentDateFormatAccess.convertDateToString(motionEvent.getTimestamp());
SFTPClient sftpClient = sshClient.newSFTPClient();
String destinationPath = eyeballsConfiguration.getSftpDestinationDirectory() + "/" + day + "/";
sftpClient.mkdirs(destinationPath);
sftpClient.put(byteArraySourceFile, destinationPath);
}
}
示例4: connectfromPItoServer
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
public static void connectfromPItoServer(int fotosAnzahl, int belichtungsDauer)
throws IOException {
final String ipServer = Inet4Address.getLocalHost().getHostAddress();
// System.out.println(Inet4Address.getAllByName("raspberrypi"));
// System.out.println(fotosAnzahl);
final SSHClient ssh = new SSHClient();
ssh.addHostKeyVerifier(new NullHostKeyVerifier());
// for(int i = 0; i < InetAddress.)
// ssh.connect("192.168.2.100", 22);
ssh.connect(Settings.raspberryIP, 22);
try {
ssh.authPassword("pi", "raspberry");
final Session session = ssh.startSession();
try {
System.out.println("BEGINNE MIt tray");
final Command cmd = session.exec("java -jar Desktop/Driver/Java/client.jar " + fotosAnzahl + " " + ipServer + " " + belichtungsDauer);
System.out.println(IOUtils.readFully(cmd.getInputStream()).toString());
cmd.join(5, TimeUnit.SECONDS);
System.out.println("\n** exit status: " + cmd.getExitStatus());
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例5: build
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
private void build() throws IOException {
if (init) {
return;
}
ssh = new SSHClient();
ssh.addHostKeyVerifier(new HostKeyVerifier() {
@Override
public boolean verify(String arg0, int arg1, PublicKey arg2) {
return true;
}
});
ssh.connect(hostname, port);
if (privateKey != null) {
privateKeyFile = File.createTempFile("zstack", "tmp");
FileUtils.writeStringToFile(privateKeyFile, privateKey);
ssh.authPublickey(username, privateKeyFile.getAbsolutePath());
} else {
ssh.authPassword(username, password);
}
init = true;
}
示例6: runCommand
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
@Override
public RemoteProcess runCommand(List<String> command) throws InterruptedException, IOException {
SSHClient ssh;
ssh = connectWithSSH();
try {
ProcessAdapter process = runCommandAndWrapInProcessAdapter(command, ssh);
process.run();
process.waitFor();
return process;
} catch (InterruptedException | IOException e) {
try {
ssh.disconnect();
} catch (IOException ioException) {
LOGGER.warn("runCommand(): Could not disconnect ssh.", ioException);
}
throw e;
}
}
示例7: connect
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
@Override
public SSHClient connect(final HostKeyCallback key, final LoginCallback prompt) throws BackgroundException {
try {
final DefaultConfig configuration = new DefaultConfig();
if("zlib".equals(preferences.getProperty("ssh.compression"))) {
configuration.setCompressionFactories(Arrays.asList(
new DelayedZlibCompression.Factory(),
new ZlibCompression.Factory(),
new NoneCompression.Factory()));
}
else {
configuration.setCompressionFactories(Collections.singletonList(new NoneCompression.Factory()));
}
configuration.setVersion(new PreferencesUseragentProvider().get());
final KeepAliveProvider heartbeat;
if(preferences.getProperty("ssh.heartbeat.provider").equals("keep-alive")) {
heartbeat = KeepAliveProvider.KEEP_ALIVE;
}
else {
heartbeat = KeepAliveProvider.HEARTBEAT;
}
configuration.setKeepAliveProvider(heartbeat);
return this.connect(key, configuration);
}
catch(IOException e) {
throw new SFTPExceptionMappingService().map(e);
}
}
示例8: testAllHMAC
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
@Test
public void testAllHMAC() throws Exception {
final Host host = new Host(new SFTPProtocol(), "test.cyberduck.ch");
final SFTPSession session = new SFTPSession(host);
final DefaultConfig defaultConfig = new DefaultConfig();
defaultConfig.setMACFactories(
new HMACSHA2256.Factory(),
new HMACSHA2512.Factory()
);
for(net.schmizz.sshj.common.Factory.Named<MAC> mac : defaultConfig.getMACFactories()) {
final DefaultConfig configuration = new DefaultConfig();
configuration.setMACFactories(Collections.singletonList(mac));
final SSHClient client = session.connect(new DisabledHostKeyCallback(), configuration);
assertTrue(client.isConnected());
client.close();
}
}
示例9: connect_to_virtual_box
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
@Test
public void connect_to_virtual_box() throws IOException {
SSHClient ssh = new SSHClient();
ssh.loadKnownHosts();
ssh.connect("localhost", 2222);
try {
ssh.authPublickey("root");
try (Session session = ssh.startSession()) {
Session.Command command = session.exec("ls -l");
System.out.println(IOUtils.readFully(command.getInputStream()).toString());
command.join(5, TimeUnit.SECONDS);
}
} finally {
ssh.disconnect();
}
}
示例10: receiveFile
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
public void receiveFile(String lfile, String rfile) throws Exception {
final SSHClient ssh = getConnectedClient();
try {
final Session session = ssh.startSession();
try {
ssh.newSCPFileTransfer().download(rfile, new FileSystemFile(lfile));
} catch (SCPException e) {
if (e.getMessage().contains("No such file or directory"))
logger.warn("No file or directory `{}` found on {}.", rfile, ip);
else
throw e;
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例11: scpUpload
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
/**
* Upload one or more files via the same SSH/SCP connection to a remote host.
*/
public static void scpUpload(HostInfo hostInfo, List<FromTo> fromTos) throws IOException {
SSHClient ssh = getSshClient(hostInfo);
try {
Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
for (FromTo ft: fromTos) {
System.out.format("SCP cp %s -> %s/%s%n", ft.from, hostInfo.host, ft.to);
ssh.newSCPFileTransfer().upload(ft.from, ft.to);
}
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例12: scpDownload
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
public static void scpDownload(HostInfo hostInfo, FromTo fromTo) throws IOException {
SSHClient ssh = getSshClient(hostInfo);
try {
Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
ssh.newSCPFileTransfer().download(fromTo.from, fromTo.to);
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例13: executeCommand
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
public static int executeCommand(HostInfo hostInfo, String command, boolean debugOutputEnabled) throws IOException {
SSHClient ssh = getSshClient(hostInfo);
try {
Session session = ssh.startSession();
session.allocateDefaultPTY();
try {
if (debugOutputEnabled) {
System.out.println("About to run: " + command);
}
Command cmd = session.exec(command);
readCommandOutput(cmd);
cmd.join();
printExitCode(cmd.getExitStatus());
return cmd.getExitStatus();
} finally {
session.close();
}
} finally {
ssh.disconnect();
ssh.close();
}
}
示例14: SshConnection
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
/**
* Connects to a ssh server
*
* <p>
* Do not call this constructor directly, use {@link SshConnector} instead for IoC
*
* @param config the ssh configuration
* @param client an instance of {@link SSHClient} to use
* @throws IOException failed to connect to the server
*/
public SshConnection(SshConfig config, SSHClient client) throws IOException {
// TODO see if the SSHClient can be injected differently
this.client = client;
this.config = config;
if (!config.getVerifyHostKey()) {
client.addHostKeyVerifier(new PromiscuousVerifier());
}
client.connect(config.getHost(), config.getPort());
if (config.getPassword() != null) {
client.authPassword(config.getUser(), config.getPassword());
} else if (config.getKeyFile() != null) {
client.authPublickey(config.getUser(), config.getKeyFile().toString());
} else {
client.authPassword(config.getUser(), "");
}
}
示例15: connect
import net.schmizz.sshj.SSHClient; //导入依赖的package包/类
@Override
public void connect(String host) throws IOException {
sshClient = new SSHClient();
if (knownHosts == null) {
sshClient.loadKnownHosts();
} else {
sshClient.loadKnownHosts(knownHosts.toFile());
}
sshClient.connect(host);
if (privateKey != null) {
sshClient.authPublickey(user, privateKey.toString());
} else if (password != null) {
sshClient.authPassword(user, password);
} else {
throw new RuntimeException("Either privateKey nor password is set. Please call one of the auth method.");
}
}