Giter Site home page Giter Site logo

jschmanager's Introduction

JSchManager

A wrapper library for the SSH library JSch by JCraft.

Features

SSHChannelShell:

Is capable of issuing a command sequence, which is done via the shell-channel of JSch.

Internally SSHChannelShell reads all available data from the channels inputstream after issuing a command.

The output of a command can be retrieved as an ArrayList or be appended to a StringBuilder.

A missing feature is, that the integer return-value of a command can't be retrieved, therefore you have to analyze the string-output and know what to expect.

All shell-output is cleaned of escape-sequences.

Authentication via public/private key is supported via JSch but not this wrapper. Comming soon (tm)!

See Example further down.

SSHChannelExec

This class is able to retrieve the integer return-value of a command but cannot issue a command sequence, to o e.g. cd multiple times and then list the contents of a file.

The shell-output is not cleaned of esacpe-sequences.

See Example further down

Other Channel-Types and usages

See /test/java/de/jackleemmerdeur/JSchManagerTest.java

Install library

git clone https://github.com/JackLeEmmerdeur/JschManager.git

Open the folder with IntelliJ

Open the Maven-tool-window and click install

Usage

Create a new Maven-project.

Create a the classpath de.myorg (replace myorg with your organization) in the src/main/java folder

Add App.java to that classpath

Use dependency and optional maven-shade-plugin from pom.xml below.

If the connection or single commands seem to run slow you can try to tweak the parameters for the overloaded methods:
  • JSchManager.openSession()
  • JSchManager.openChannelExec()
  • JSchManager.openChannelSFTP()
  • JSchManager.openChannelShell()
  • SSHChannelExec.readAllFromChannelExec()
  • SSHChannelShell.queryBuilder()
  • SSHChannelShell.queryArray()

Example

package de.myorg.test;

import de.jackleemmerdeur.*;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class App {
    public static void main(String args[]) {
        try (JSchManager w = new JSchManager(false)) {
            SSHSession s = w.openSession("test", "192.168.178.46", "pi", "raspberry");

            // Use a shell to issue consecutive commands
            try (SSHChannelShell c = w.openChannelShell(s, 1000)) {
                c.exec("cd .local");

                List<String> a = c.queryArray("ls -la");
                for (String l : a) {
                    System.out.println(l);
                }

                c.exec("cd ..");

                StringBuilder b = new StringBuilder();
                c.queryBuilder("cat .bashrc", b);
                System.out.println(b);
            }

            // Use an exec channel to issue a one-time command (or multiple in one go).
            // No interaction with the SSHChannelExec-Instance is allowed after readAllFromChannelExec() is executed.
            // The try-with-block ensures resource deallocation and channel-closing.
            AtomicInteger retcode = new AtomicInteger();
            try (SSHChannelExec e = w.openChannelExec("test", "cd .local && cd share && cd xorg && cat Xorg.0.log")) {
                StringBuilder b = new StringBuilder();
                e.readAllFromChannelExec(b, retcode, 0);
                System.out.println(b);
            } finally {
                System.out.println(retcode.get());
            }

        } catch (Exception e) {
            System.err.println(e.getMessage());
        }
    }
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
		 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>de.myorg</groupId>
	<artifactId>Test</artifactId>
	<version>1.0-SNAPSHOT</version>
	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<maven.compiler.source>1.8</maven.compiler.source>
		<maven.compiler.target>1.8</maven.compiler.target>
	</properties>
	<build>
		<plugins>
			<plugin>
				<groupId>org.apache.maven.plugins</groupId>
				<artifactId>maven-shade-plugin</artifactId>
				<version>3.2.4</version>
				<executions>
					<execution>
						<phase>package</phase>
						<goals>
							<goal>shade</goal>
						</goals>
						<configuration>
							<transformers>
								<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
									<mainClass>de.myorg.test.App</mainClass>
								</transformer>
							</transformers>
						</configuration>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
	<dependencies>
		<dependency>
			<groupId>de.jackleemmerdeur</groupId>
			<artifactId>JSchManager</artifactId>
			<version>0.1</version>
		</dependency>
	</dependencies>
</project>

jschmanager's People

Contributors

jackleemmerdeur avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.