All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.wl4g.component.common.cli.ssh2.JschHolder Maven / Gradle / Ivy

/*
 * Copyright 2017 ~ 2025 the original author or authors. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wl4g.component.common.cli.ssh2;

import com.google.common.annotations.Beta;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.KeyPair;
import com.wl4g.component.common.function.CallbackFunction;
import com.wl4g.component.common.function.ProcessFunction;

import java.io.*;

/**
 * Jsch based SSH2 tools.
 *
 * @author Wangl.sir <[email protected]>
 * @version v1.0 2019年5月24日
 * @since
 */
@Beta
public class JschHolder extends SSH2Holders {

	// --- Transfer files. ---

	@Override
	public void scpGetFile(String host, String user, char[] pemPrivateKey, String password, File localFile, String remoteFilePath)
			throws Exception {
		throw new UnsupportedOperationException();
	}

	@Override
	public void scpPutFile(String host, String user, char[] pemPrivateKey, String password, File localFile, String remoteDir)
			throws Exception {
		throw new UnsupportedOperationException();
	}

	@Override
	protected void doScpTransfer(String host, String user, char[] pemPrivateKey, String password,
			CallbackFunction processor) throws Exception {
		throw new UnsupportedOperationException();
	}

	// --- Execution commands. ---

	@Override
	public Ssh2ExecResult execWaitForResponse(String host, String user, char[] pemPrivateKey, String password, String command,
			long timeoutMs) throws Exception {
		throw new UnsupportedOperationException();
	}

	@Override
	public  T execWaitForComplete(String host, String user, char[] pemPrivateKey, String password, String command,
			ProcessFunction processor, long timeoutMs) throws Exception {
		throw new UnsupportedOperationException();
	}

	@Override
	public  T doExecCommand(String host, String user, char[] pemPrivateKey, String password, String command,
			ProcessFunction processor) throws Exception {
		throw new UnsupportedOperationException();
	}

	// --- Tool function's. ---

	@Override
	public Ssh2KeyPair generateKeypair(AlgorithmType type, String comment) throws Exception {
		int algType = KeyPair.RSA;
		if (type == AlgorithmType.DSA) {
			algType = KeyPair.DSA;
		} else if (type == AlgorithmType.ECDSA) {
			algType = KeyPair.ECDSA;
		}
		KeyPair kpair = KeyPair.genKeyPair(new JSch(), algType);

		// 私钥
		ByteArrayOutputStream baos = new ByteArrayOutputStream();// 向OutPutStream中写入
		kpair.writePrivateKey(baos);
		final String privateKey = baos.toString();
		// 公钥
		baos = new ByteArrayOutputStream();
		kpair.writePublicKey(baos, comment);
		final String publicKey = baos.toString();
		kpair.dispose();

		// To rsa.pub
		// String publicKeyString =
		// RSAEncrypt.loadPublicKeyByFile(filePath,filename + ".pub");
		// System.out.println(publicKeyString.length());
		// System.out.println(publicKeyString);

		// To rsa
		// String privateKeyString =
		// RSAEncrypt.loadPrivateKeyByFile(filePath,filename);
		// System.out.println(privateKeyString.length());
		// System.out.println(privateKeyString);

		return new Ssh2KeyPair(privateKey, publicKey);
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy