本文整理汇总了Java中com.jcraft.jsch.JSch.addIdentity方法的典型用法代码示例。如果您正苦于以下问题:Java JSch.addIdentity方法的具体用法?Java JSch.addIdentity怎么用?Java JSch.addIdentity使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.JSch
的用法示例。
在下文中一共展示了JSch.addIdentity方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: fileFetch
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public static void fileFetch(String host, String user, String keyLocation, String sourceDir, String destDir) {
JSch jsch = new JSch();
Session session = null;
try {
// set up session
session = jsch.getSession(user,host);
// use private key instead of username/password
session.setConfig(
"PreferredAuthentications",
"publickey,gssapi-with-mic,keyboard-interactive,password");
jsch.addIdentity(keyLocation);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
// copy remote log file to localhost.
ChannelSftp channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
channelSftp.get(sourceDir, destDir);
channelSftp.exit();
} catch (Exception e) {
e.printStackTrace();
} finally {
session.disconnect();
}
}
示例2: build
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public JSch build() throws JSchException {
LOGGER.debug("Initializing JSch Logger");
JSch.setLogger(new JschLogger());
final JSch jsch = new JSch();
try {
if (null != knownHostsFileName && new File(knownHostsFileName).exists()) {
jsch.setKnownHosts(knownHostsFileName);
}
if (null != privateKeyFileName && new File(privateKeyFileName).exists()) {
jsch.addIdentity(privateKeyFileName);
}
} catch (JSchException e) {
LOGGER.error("Could not access known hosts or private key file.", e);
if (!(e.getCause() instanceof FileNotFoundException)) {
throw new JSchException();
}
}
return jsch;
}
示例3: connect
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
/**
* Connect to a remote host and return a channel (session and jsch are set)
*/
Channel connect(String channleType, String sshCommand) throws Exception {
JSch.setConfig("StrictHostKeyChecking", "no"); // Not recommended, but useful
jsch = new JSch();
// Some "reasonable" defaults
if (Gpr.exists(defaultKnownHosts)) jsch.setKnownHosts(defaultKnownHosts);
for (String identity : defaultKnownIdentity)
if (Gpr.exists(identity)) jsch.addIdentity(identity);
// Create session and connect
if (debug) Gpr.debug("Create conection:\n\tuser: '" + host.getUserName() + "'\n\thost : '" + host.getHostName() + "'\n\tport : " + host.getPort());
session = jsch.getSession(host.getUserName(), host.getHostName(), host.getPort());
session.setUserInfo(new SshUserInfo());
session.connect();
// Create channel
channel = session.openChannel(channleType);
if ((sshCommand != null) && (channel instanceof ChannelExec)) ((ChannelExec) channel).setCommand(sshCommand);
return channel;
}
示例4: testTestCommand
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Test
public void testTestCommand() throws JSchException, IOException {
JSch jsch = new JSch();
Session session = jsch.getSession("admin", "localhost", properties.getShell().getPort());
jsch.addIdentity("src/test/resources/id_rsa");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelShell channel = (ChannelShell) session.openChannel("shell");
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
channel.setInputStream(new PipedInputStream(pos));
channel.setOutputStream(new PipedOutputStream(pis));
channel.connect();
pos.write("test run bob\r".getBytes(StandardCharsets.UTF_8));
pos.flush();
verifyResponse(pis, "test run bob");
pis.close();
pos.close();
channel.disconnect();
session.disconnect();
}
开发者ID:anand1st,项目名称:sshd-shell-spring-boot,代码行数:24,代码来源:SshdShellAutoConfigurationWithPublicKeyAndBannerImageTest.java
示例5: connectWithIdentity
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public void connectWithIdentity(String identityPath, String passPhrase) throws JSchException,FileNotFoundException {
if (identityPath == null || new File(identityPath).exists() == false) {
throw new FileNotFoundException("Identity file not found !") ;
}
if (m_session != null) {
m_session.disconnect();
}
preConnect();
JSch jsch = new JSch();
if (passPhrase != null && passPhrase.length() >0) {
jsch.addIdentity(identityPath, passPhrase);
}
else {
jsch.addIdentity(identityPath);
}
SSH2User userInfo = new SSH2User(m_username, passPhrase);
m_session = jsch.getSession(m_username, m_host, m_port);
m_session.setConfig(infos);
m_session.setUserInfo(userInfo);
m_session.connect();
}
示例6: SSHShell
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
/**
* Creates SSHShell.
*
* @param host the host name
* @param port the ssh port
* @param userName the ssh user name
* @param sshPrivateKey the ssh password
* @return the shell
* @throws JSchException
* @throws IOException
*/
private SSHShell(String host, int port, String userName, byte[] sshPrivateKey)
throws JSchException, IOException {
Closure expectClosure = getExpectClosure();
for (String linuxPromptPattern : new String[]{"\\>", "#", "~#", "~\\$"}) {
try {
Match match = new RegExpMatch(linuxPromptPattern, expectClosure);
linuxPromptMatches.add(match);
} catch (MalformedPatternException malformedEx) {
throw new RuntimeException(malformedEx);
}
}
JSch jsch = new JSch();
jsch.setKnownHosts(System.getProperty("user.home") + "/.ssh/known_hosts");
jsch.addIdentity(host, sshPrivateKey, (byte[]) null, (byte[]) null);
this.session = jsch.getSession(userName, host, port);
this.session.setConfig("StrictHostKeyChecking", "no");
this.session.setConfig("PreferredAuthentications", "publickey,keyboard-interactive,password");
session.connect(60000);
this.channel = (ChannelShell) session.openChannel("shell");
this.expect = new Expect4j(channel.getInputStream(), channel.getOutputStream());
channel.connect();
}
示例7: createSession
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
private Session createSession(String host, Args args) throws JSchException {
JSch jsch = new JSch();
for (String keyFile : getKeyFiles()) {
jsch.addIdentity(keyFile);
}
JSch.setLogger(new LogAdapter());
Session session = jsch.getSession(args.user, host, args.sshPort);
session.setConfig("StrictHostKeyChecking", "no");
return session;
}
示例8: establishWithKey
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public static Session establishWithKey(String sshHost, int sshPort, String user, String keyFilePath) throws JSchException {
File keyFile = new File(keyFilePath);
if (!keyFile.exists()) {
String errorMsg = "Could not find SSH public key file in path: " + keyFilePath;
logger.info(errorMsg);
throw new JSchException(errorMsg);
}
Session session;
JSch jsch = new JSch();
try {
jsch.addIdentity(keyFile.getAbsolutePath());
session = jsch.getSession(user, sshHost, sshPort);
}
catch (JSchException e) {
logger.error("SSH connection attempt to host: " + sshHost + ":" + sshPort + " failed");
throw e;
}
return connect(session, sshHost, sshPort);
}
示例9: testJschConnection
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Test
public void testJschConnection() throws InterruptedException, SftpException, JSchException, IOException {
JSch jsch = new JSch();
String passphrase = "";
jsch.addIdentity(privateKey,
StringUtil.isEmpty(passphrase) ?
null : passphrase);
Session session = jsch.getSession(user, host, port);
System.out.println("session created.");
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
config.put("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.setConfig(config);
session.connect();
Thread.sleep(500);
session.disconnect();
}
示例10: pushToRepository
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
/**
* Push all changes and tags to given remote.
*
* @param git
* instance.
* @param remote
* to be used.
* @param passphrase
* to access private key.
* @param privateKey
* file location.
*
* @return List of all results of given push.
*/
public Iterable<PushResult> pushToRepository(Git git, String remote, String passphrase, Path privateKey) {
SshSessionFactory sshSessionFactory = new JschConfigSessionFactory() {
@Override
protected void configure(OpenSshConfig.Host host, Session session) {
session.setUserInfo(new PassphraseUserInfo(passphrase));
}
@Override
protected JSch createDefaultJSch(FS fs) throws JSchException {
if (privateKey != null) {
JSch defaultJSch = super.createDefaultJSch(fs);
defaultJSch.addIdentity(privateKey.toFile().getAbsolutePath());
return defaultJSch;
} else {
return super.createDefaultJSch(fs);
}
}
};
try {
return git.push()
.setRemote(remote)
.setPushAll()
.setPushTags()
.setTransportConfigCallback(transport -> {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
})
.call();
} catch (GitAPIException e) {
throw new IllegalStateException(e);
}
}
示例11: provideJSch
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Provides
static JSch provideJSch(
@Config("rdeSshIdentity") String identity,
@Key("rdeSshClientPrivateKey") String privateKey,
@Key("rdeSshClientPublicKey") String publicKey) {
applyAppEngineKludge();
JSch jsch = new JSch();
try {
jsch.addIdentity(
identity,
privateKey.getBytes(UTF_8),
publicKey.getBytes(UTF_8),
null);
} catch (JSchException e) {
throw new RuntimeException(e);
}
// TODO(b/13028224): Implement known hosts checking.
JSch.setConfig("StrictHostKeyChecking", "no");
return jsch;
}
示例12: copyFile
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
/**
*
* @param sourceFilePath
* @param destinationFilePath
* @param destinationHost
* @param destinationPort
* @param destinationUserName
* @param destinationUserPassword
* @param destinationPrivateKeyFilePath
* @param destinationPrivateKeyPassphrase
* @throws IOException
* @throws JSchException
* @throws SftpException
*/
public static void copyFile(
String sourceFilePath,
String destinationFilePath,
String destinationHost,
String destinationPort,
String destinationUserName,
String destinationUserPassword,
String destinationPrivateKeyFilePath,
String destinationPrivateKeyPassphrase) throws IOException, JSchException, SftpException {
JSch jsch = new JSch();
if (!StringUtils.isEmptyOrNull(destinationPrivateKeyFilePath)) {
jsch.addIdentity(destinationPrivateKeyFilePath, destinationPrivateKeyPassphrase);
}
int destinationPortNumber = 22;
if (!StringUtils.isEmptyOrNull(destinationPort)) {
destinationPortNumber = Integer.parseInt(destinationPort);
}
Session session = jsch.getSession(destinationUserName, destinationHost, destinationPortNumber);
if (!StringUtils.isEmptyOrNull(destinationUserPassword)) {
session.setPassword(destinationUserPassword);
}
// Properties config = new Properties();
// config.put("StrictHostKeyChecking", "no");
// session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp channelSftp = (ChannelSftp)channel;
File destinationFile = new File(destinationFilePath);
channelSftp.cd(destinationFile.getPath());
channelSftp.put(new FileInputStream(sourceFilePath), destinationFile.getName());
}
示例13: open
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Override
public void open() throws RemoteConnectionException
{
try
{
jsch = new JSch();
LOGGER.debug("Adding publickey %s", host.getKeyFile());
jsch.addIdentity(host.getKeyFile());
openSession();
connectionRegistry.add(this);
} catch (JSchException e)
{
throw new RemoteConnectionException(e);
}
}
示例14: open
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Override
public void open() throws RemoteConnectionException
{
try
{
jsch = new JSch();
LOGGER.debug("Adding publickey %s", host.getKeyFile());
jsch.addIdentity(host.getKeyFile());
openSession();
connectionRegistry.add(this);
} catch (JSchException e)
{
throw new RemoteConnectionException(e);
}
}
示例15: connect
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
/**
* Connects to and maintains a ssh connection. If this method is called
* twice, a reconnection is attempted
*
* @throws SshException If the members of the SshConnection class are not properly set or if
* there was a problem actually connecting to the specified host.
* @throws IllegalStateException If connect() was called once successfully. connect() and any setter
* method can't be called after successfully connecting without first
* disconnecting.
*/
public void connect() throws SshException {
exceptIfAlreadyConnected();
try {
JSch jsch = new JSch();
validateMembers();
if (this.usePrivateKey) {
jsch.addIdentity(this.privateKeyFile.getAbsolutePath());
}
this.sshSession = jsch.getSession(this.username, this.host, this.port);
this.sshSession.setConfig(SSH_PROPERTIES);
if (!this.usePrivateKey && this.password != null) {
this.sshSession.setPassword(this.password);
}
this.sshSession.connect();
} catch (JSchException e) {
throw new SshException(e);
}
}