本文整理汇总了Java中com.jcraft.jsch.JSch.setKnownHosts方法的典型用法代码示例。如果您正苦于以下问题:Java JSch.setKnownHosts方法的具体用法?Java JSch.setKnownHosts怎么用?Java JSch.setKnownHosts使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.JSch
的用法示例。
在下文中一共展示了JSch.setKnownHosts方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: 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;
}
示例2: 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;
}
示例3: 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();
}
示例4: isValidKnownHostsFile
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
private static boolean isValidKnownHostsFile(String knownHostsFile) {
JSch test = new JSch();
try {
test.setKnownHosts(knownHostsFile);
} catch (JSchException ex) {
return false;
}
return true;
}
示例5: openSession
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
protected Session openSession(final ProcessContext context) throws JSchException {
final JSch jsch = new JSch();
final String hostKeyVal = context.getProperty(HOST_KEY_FILE).getValue();
if (hostKeyVal != null) {
jsch.setKnownHosts(hostKeyVal);
}
final Session session = jsch.getSession(context.getProperty(USERNAME).evaluateAttributeExpressions().getValue(),
context.getProperty(HOSTNAME).evaluateAttributeExpressions().getValue(),
context.getProperty(PORT).evaluateAttributeExpressions().asInteger().intValue());
final Properties properties = new Properties();
properties.setProperty("StrictHostKeyChecking", context.getProperty(STRICT_HOST_KEY_CHECKING).asBoolean() ? "yes" : "no");
properties.setProperty("PreferredAuthentications", "publickey,password,keyboard-interactive");
final PropertyValue compressionValue = context.getProperty(USE_COMPRESSION);
if (compressionValue != null && "true".equalsIgnoreCase(compressionValue.getValue())) {
properties.setProperty("compression.s2c", "[email protected],zlib,none");
properties.setProperty("compression.c2s", "[email protected],zlib,none");
} else {
properties.setProperty("compression.s2c", "none");
properties.setProperty("compression.c2s", "none");
}
session.setConfig(properties);
final String privateKeyFile = context.getProperty(PRIVATE_KEY_PATH).evaluateAttributeExpressions().getValue();
if (privateKeyFile != null) {
jsch.addIdentity(privateKeyFile, context.getProperty(PRIVATE_KEY_PASSPHRASE).evaluateAttributeExpressions().getValue());
}
final String password = context.getProperty(PASSWORD).evaluateAttributeExpressions().getValue();
if (password != null) {
session.setPassword(password);
}
session.setTimeout(context.getProperty(CONNECTION_TIMEOUT).asTimePeriod(TimeUnit.MILLISECONDS).intValue());
session.connect();
return session;
}
示例6: connect
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public void connect() {
if (session != null) {
throw new IllegalStateException("Already Connected");
}
try {
JSch jSch = new JSch();
if (knownHosts != null) {
jSch.setKnownHosts(knownHosts);
}
if (identity != null) {
jSch.addIdentity(identity);
}
session = jSch.getSession(user, host, 22);
Properties properties = new Properties();
properties.setProperty("StrictHostKeyChecking", "no");
properties.setProperty("PreferredAuthentications", "publickey");
session.setConfig(properties);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
LOGGER.info("connect() - Connected to %s", host);
} catch (Exception ex) {
// Failed to connect
session = null;
LOGGER.error("connect() - Failed to connect to %s - %s ", host, ex.getMessage());
throw new RuntimeException(ex);
}
}
示例7: knownHosts
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
private static void knownHosts(final JSch sch, FS fs) throws JSchException {
final File home = fs.userHome();
if (home == null)
return;
final File known_hosts = new File(new File(home, ".ssh"), "known_hosts"); //$NON-NLS-1$ //$NON-NLS-2$
try {
final FileInputStream in = new FileInputStream(known_hosts);
try {
sch.setKnownHosts(in);
} finally {
in.close();
}
} catch (FileNotFoundException none) {
// Oh well. They don't have a known hosts in home.
} catch (IOException err) {
// Oh well. They don't have a known hosts in home.
}
}
示例8: getHostKeyRepository
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
/**
* @param knownHostFile
* @return
* @throws JSchException
*/
private static HostKeyRepository getHostKeyRepository( String knownHostFile )
throws JSchException
{
JSch jsch = new JSch();
jsch.setKnownHosts( knownHostFile );
return jsch.getHostKeyRepository();
}
示例9: build
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public JSch build() throws SshException, JSchException {
JSch jsch = new JSch();
if (knownHosts != null) {
jsch.setKnownHosts(knownHosts);
}
if (authentication == null) {
throw new SshException("Error: authentication not set");
}
authentication.prepare(jsch);
return jsch;
}
示例10: connectAndGetSftpChannel
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public ChannelSftp connectAndGetSftpChannel() throws JSchException {
JSch jsch = new JSch();
jsch.setKnownHosts(context.getFilesDir().getAbsolutePath() + File.separator + "jsch_known_hosts");
String username = prefs.sshUserName().getOr(null);
String host = prefs.sshServerAddress().getOr(null);
String port = prefs.sshServerPort().getOr(null);
if (username == null || host == null || port == null)
throw new IllegalStateException("Please enter your server settings first");
Session session = jsch.getSession(username, host, Integer.parseInt(port));
UserInfo info = storedOrAskedUserInfo;
session.setUserInfo(info);
try {
session.connect(5000);
storedOrAskedUserInfo.confirmPasswordRight();
}
catch (JSchException je) {
if ("auth fail".equals(je.getMessage().toLowerCase())) { // undocumented!
storedOrAskedUserInfo.clearPassword();
}
throw je;
}
ChannelSftp channel = (ChannelSftp)session.openChannel("sftp");
channel.connect(5000);
return channel;
}
示例11: connect
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Override
public void connect(String host) throws IOException {
if (session == null) {
try {
JSch sshClient = new JSch();
if (privateKey != null) {
sshClient.addIdentity(privateKey.toString());
}
if (knownHosts != null) {
sshClient.setKnownHosts(knownHosts.toString());
} else {
sshClient.setKnownHosts("~/.ssh/known_hosts");
}
session = sshClient.getSession(user, host);
if (password != null) {
session.setPassword(password);
} else if (privateKey == null){
throw new IOException("Either privateKey nor password is set. Please call one of the authentication method.");
}
session.connect();
} catch (JSchException ex) {
throw new IOException(ex);
}
}
}
示例12: createSession
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
protected Session createSession() throws JSchException {
final JSch jsch = new JSch();
final Session session = jsch.getSession( username, host, port );
if ( this.knownHostsInputStream != null ) {
jsch.setKnownHosts( this.knownHostsInputStream );
}
if ( this.strictHostkeyChecking == StrictHostkeyChecking.OFF ) {
LOG.warn( "Session created with StrictHostKeyChecking disabled." );
// If this property is set to ``yes'', jsch will never automatically add
// host keys to the $HOME/.ssh/known_hosts file, and refuses to connect
// to hosts whose host key has changed. This property forces the user
// to manually add all new hosts. If this property is set to ``no'',
// jsch will automatically add new host keys to the user known hosts files.
session.setConfig( "StrictHostKeyChecking", "no" );
}
session.setPassword( password );
// socket timeout in milliseconds
session.setTimeout( socketTimeoutMs );
return session;
}
示例13: getServerHostKey
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Override
public String getServerHostKey(String host, int port, Algorithm algorithm) throws Throwable {
JSch jsch = new JSch();
// set the algorithm/key type
JSch.setConfig("server_host_key", algorithm.signature());
// in-memory buffer & stream to store the remote host key.
byte[] knownHosts = new byte[8192];
ByteArrayInputStream in = new ByteArrayInputStream(knownHosts);
// set the in-memory stream as key repository
jsch.setKnownHosts(in);
// create a session where user name is nobody, it can be anything except null. Because it will fail to
// authenticate anyway. The remote host key will be added to repository before the authentication.
com.jcraft.jsch.Session session = jsch.getSession("nobody", host, port);
// accept unknown key
session.setConfig("StrictHostKeyChecking", "no");
try {
// connect to remote host. It will do host key exchange first then authentication.
session.connect();
} catch (JSchException e) {
// authentication fails but it is ok.
} finally {
if (session.isConnected()) {
// by any chance it is connected, disconnect it.
session.disconnect();
}
}
return session.getHostKey().getKey();
}
示例14: JschConnection
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
public JschConnection(ConnectionDetails cxnDetails, int maxChannels, boolean verbose) throws Throwable {
_cxnDetails = cxnDetails;
_maxChannels = maxChannels;
_verbose = verbose;
_channels = Collections.synchronizedCollection(new HashSet<com.jcraft.jsch.Channel>());
_jsch = new JSch();
_jsch.setKnownHosts(new ByteArrayInputStream(new byte[8192]));
String hostKey = _cxnDetails.hostKey();
if (hostKey != null) {
_jsch.getHostKeyRepository()
.add(new HostKey(_cxnDetails.host(), Base64.getDecoder().decode(hostKey.getBytes())), userInfo());
}
/*
* added user's identity (private & public keys)
*/
if (_cxnDetails.privateKey() != null) {
_jsch.addIdentity(_cxnDetails.username() + "_identity", _cxnDetails.privateKey().getBytes(),
_cxnDetails.publicKey() != null ? _cxnDetails.publicKey().getBytes() : null,
_cxnDetails.passphrase() != null ? _cxnDetails.passphrase().getBytes() : null);
}
/*
* connect
*/
_jschSession = _jsch.getSession(_cxnDetails.username(), _cxnDetails.host(), _cxnDetails.port());
_jschSession.setConfig("StrictHostKeyChecking", _cxnDetails.hostKey() != null ? "yes" : "no");
_jschSession.setUserInfo(userInfo());
if (_verbose) {
System.out.print("opening connection to " + _cxnDetails.host() + ":" + _cxnDetails.port() + " ...");
}
_jschSession.connect();
if (_verbose) {
System.out.println("done");
}
}
示例15: getServerHostKey
import com.jcraft.jsch.JSch; //导入方法依赖的package包/类
@Override
public String getServerHostKey(String host, int port, KeyType type) throws Throwable {
JSch jsch = new JSch();
// set the algorithm/key type
JSch.setConfig("server_host_key", type.publicKeyTypeName());
// in-memory buffer & stream to store the remote host key.
byte[] knownHosts = new byte[8192];
ByteArrayInputStream in = new ByteArrayInputStream(knownHosts);
// set the in-memory stream as key repository
jsch.setKnownHosts(in);
// create a session where user name is nobody, it can be anything except
// null. Because it will fail to authenticate anyway. The remote host
// key will be added to repository before the authentication.
com.jcraft.jsch.Session session = jsch.getSession("nobody", host, port);
// accept unknown key
session.setConfig("StrictHostKeyChecking", "no");
try {
// connect to remote host. It will do host key exchange first then
// authentication.
session.connect();
} catch (JSchException e) {
// authentication fails but it is ok.
} finally {
if (session.isConnected()) {
// by any chance it is connected, disconnect it.
session.disconnect();
}
}
return session.getHostKey().getKey();
}