本文整理匯總了Java中com.jcraft.jsch.JSch.setConfig方法的典型用法代碼示例。如果您正苦於以下問題:Java JSch.setConfig方法的具體用法?Java JSch.setConfig怎麽用?Java JSch.setConfig使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.jcraft.jsch.JSch
的用法示例。
在下文中一共展示了JSch.setConfig方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: 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;
}
示例2: probeSSH
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
protected final void probeSSH(final String host, final KeyPair key) {
final Callable<Boolean> callable = () -> {
final JSch jsch = new JSch();
final Session session = jsch.getSession("ec2-user", host);
jsch.addIdentity(key.getKeyName(), key.getKeyMaterial().getBytes(), null, null);
jsch.setConfig("StrictHostKeyChecking", "no"); // for testing this should be fine. adding the host key seems to be only possible via a file which is not very useful here
session.connect(10000);
session.disconnect();
return true;
};
Assert.assertTrue(this.retry(callable));
}
示例3: reconfigureServerHostKeyOrder
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
protected static void reconfigureServerHostKeyOrder(ServerHostKeySortOrder hostKeySortOrder) {
List<HostKeyType> serverHostKeys = new ArrayList<>(getServerHostKeys());
if (hostKeySortOrder == ServerHostKeySortOrder.PREFER_ECDSA) {
Collections.sort(serverHostKeys, CMP_PREFER_ECDSA);
} else if (hostKeySortOrder == ServerHostKeySortOrder.PREFER_RSA) {
Collections.sort(serverHostKeys, CMP_PREFER_RSA);
} else {
throw new IllegalArgumentException("Unknown host key sort order: " + hostKeySortOrder);
}
if (!getServerHostKeys().equals(serverHostKeys)) {
log.debug("changing server host key order to: " + serverHostKeys);
List<String> serverHostKeyNames = new ArrayList<>();
for (HostKeyType serverHostKey : serverHostKeys) {
serverHostKeyNames.add(serverHostKey.getTypeString());
}
String newHostKeyOrder = Utils.join(serverHostKeyNames, SERVER_HOST_KEY_SEPARATOR);
JSch.setConfig(JSCH_CONFIG_KEY_SERVER_HOST_KEY, newHostKeyOrder);
}
}
示例4: 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;
}
示例5: SshXpraConnector
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
public SshXpraConnector(XpraClient client, String host, String username, int port, UserInfo userInfo) {
super(client);
this.host = host;
this.username = username;
this.port = port;
this.userInfo = userInfo;
JSch.setConfig("compression_level", "0");
}
示例6: initSsh
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
public static void initSsh(TestAccount a) {
final Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
JSch.setConfig(config);
// register a JschConfigSessionFactory that adds the private key as identity
// to the JSch instance of JGit so that SSH communication via JGit can
// succeed
SshSessionFactory.setInstance(
new JschConfigSessionFactory() {
@Override
protected void configure(Host hc, Session session) {
try {
final JSch jsch = getJSch(hc, FS.DETECTED);
jsch.addIdentity("KeyPair", a.privateKey(), a.sshKey.getPublicKeyBlob(), null);
} catch (JSchException e) {
throw new RuntimeException(e);
}
}
});
}
示例7: 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();
}
示例8: 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();
}
示例9: testReExchangeFromJschClient
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
@Test
public void testReExchangeFromJschClient() throws Exception {
Assume.assumeTrue("DH Group Exchange not supported", SecurityUtils.isDHGroupExchangeSupported());
setUp(0L, 0L, 0L);
JSch.setConfig("kex", BuiltinDHFactories.Constants.DIFFIE_HELLMAN_GROUP_EXCHANGE_SHA1);
JSch sch = new JSch();
com.jcraft.jsch.Session s = sch.getSession(getCurrentTestName(), TEST_LOCALHOST, port);
try {
s.setUserInfo(new SimpleUserInfo(getCurrentTestName()));
s.connect();
com.jcraft.jsch.Channel c = s.openChannel(Channel.CHANNEL_SHELL);
c.connect();
try (OutputStream os = c.getOutputStream();
InputStream is = c.getInputStream()) {
String expected = "this is my command\n";
byte[] bytes = expected.getBytes(StandardCharsets.UTF_8);
byte[] data = new byte[bytes.length + Long.SIZE];
for (int i = 1; i <= 10; i++) {
os.write(bytes);
os.flush();
int len = is.read(data);
String str = new String(data, 0, len);
assertEquals("Mismatched data at iteration " + i, expected, str);
outputDebugMessage("Request re-key #%d", i);
s.rekey();
}
} finally {
c.disconnect();
}
} finally {
s.disconnect();
}
}
示例10: getSshClient
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
/**
* Returns a new SSH client with the configured SSH key. This client can be used to create a new
* SSH session.
*/
private JSch getSshClient() throws JSchException {
JSch jsch = new JSch();
jsch.addIdentity(getUserProperty(SSH_KEY));
JSch.setConfig("StrictHostKeyChecking", "no"); //$NON-NLS-1$ //$NON-NLS-2$
return jsch;
}
示例11: configure
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
@Override
protected void configure() {
Properties properties = loadProperties(configFile);
Names.bindProperties(binder(), properties);
bind(Properties.class).toInstance(properties);
Preconditions.checkArgument(!Strings.isNullOrEmpty(checkoutPath), "checkout-path must be set");
JSch.setConfig("StrictHostKeyChecking", "no");
//Evidently some CVS servers dont like re-use of connections. Ours apparently likes them though
System.setProperty("javacvs.multiple_commands_warning", "false");
}
示例12: reconfigurePreferredAuthentications
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
protected static void reconfigurePreferredAuthentications() {
JSch.setConfig(JSCH_CONFIG_KEY_PREFERRED_AUTHENTICATIONS, "publickey");
}
示例13: createSession
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
private Session createSession(ScpConfiguration config) {
ObjectHelper.notNull(config, "ScpConfiguration");
try {
final JSch jsch = new JSch();
// get from configuration
if (ObjectHelper.isNotEmpty(config.getCiphers())) {
LOG.trace("Using ciphers: {}", config.getCiphers());
Hashtable<String, String> ciphers = new Hashtable<String, String>();
ciphers.put("cipher.s2c", config.getCiphers());
ciphers.put("cipher.c2s", config.getCiphers());
JSch.setConfig(ciphers);
}
if (ObjectHelper.isNotEmpty(config.getPrivateKeyFile())) {
LOG.trace("Using private keyfile: {}", config.getPrivateKeyFile());
String pkfp = config.getPrivateKeyFilePassphrase();
jsch.addIdentity(config.getPrivateKeyFile(), ObjectHelper.isNotEmpty(pkfp) ? pkfp : null);
}
String knownHostsFile = config.getKnownHostsFile();
if (knownHostsFile == null && config.isUseUserKnownHostsFile()) {
if (userKnownHostFile == null) {
userKnownHostFile = System.getProperty("user.home") + "/.ssh/known_hosts";
LOG.info("Known host file not configured, using user known host file: " + userKnownHostFile);
}
knownHostsFile = userKnownHostFile;
}
jsch.setKnownHosts(ObjectHelper.isEmpty(knownHostsFile) ? null : knownHostsFile);
session = jsch.getSession(config.getUsername(), config.getHost(), config.getPort());
session.setTimeout(config.getTimeout());
session.setUserInfo(new SessionUserInfo(config));
if (ObjectHelper.isNotEmpty(config.getStrictHostKeyChecking())) {
LOG.trace("Using StrickHostKeyChecking: {}", config.getStrictHostKeyChecking());
session.setConfig("StrictHostKeyChecking", config.getStrictHostKeyChecking());
}
int timeout = config.getConnectTimeout();
LOG.debug("Connecting to {} with {} timeout...", config.remoteServerInformation(),
timeout > 0 ? (Integer.toString(timeout) + " ms") : "no");
if (timeout > 0) {
session.connect(timeout);
} else {
session.connect();
}
} catch (JSchException e) {
session = null;
LOG.warn("Could not create ssh session for " + config.remoteServerInformation(), e);
}
return session;
}
示例14: createSession
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
private Session createSession(ScpConfiguration config) {
ObjectHelper.notNull(config, "ScpConfiguration");
try {
final JSch jsch = new JSch();
// get from configuration
if (ObjectHelper.isNotEmpty(config.getCiphers())) {
LOG.debug("Using ciphers: {}", config.getCiphers());
Hashtable<String, String> ciphers = new Hashtable<String, String>();
ciphers.put("cipher.s2c", config.getCiphers());
ciphers.put("cipher.c2s", config.getCiphers());
JSch.setConfig(ciphers);
}
if (ObjectHelper.isNotEmpty(config.getPrivateKeyFile())) {
LOG.debug("Using private keyfile: {}", config.getPrivateKeyFile());
String pkfp = config.getPrivateKeyFilePassphrase();
jsch.addIdentity(config.getPrivateKeyFile(), ObjectHelper.isNotEmpty(pkfp) ? pkfp : null);
}
String knownHostsFile = config.getKnownHostsFile();
jsch.setKnownHosts(ObjectHelper.isEmpty(knownHostsFile) ? DEFAULT_KNOWN_HOSTS : knownHostsFile);
session = jsch.getSession(config.getUsername(), config.getHost(), config.getPort());
session.setTimeout(config.getTimeout());
session.setUserInfo(new SessionUserInfo(config));
if (ObjectHelper.isNotEmpty(config.getStrictHostKeyChecking())) {
LOG.debug("Using StrickHostKeyChecking: {}", config.getStrictHostKeyChecking());
session.setConfig("StrictHostKeyChecking", config.getStrictHostKeyChecking());
} else {
LOG.debug("Using StrickHostKeyChecking: {}", "no");
session.setConfig("StrictHostKeyChecking", "no");
}
int timeout = config.getConnectTimeout();
LOG.debug("Connecting to {} with {} timeout...", config.remoteServerInformation(), timeout > 0 ? (Integer.toString(timeout) + " ms") : "no");
if (timeout > 0) {
session.connect(timeout);
} else {
session.connect();
}
} catch (JSchException e) {
session = null;
LOG.warn("Could not create ssh session for " + config.remoteServerInformation(), e);
}
return session;
}
示例15: create
import com.jcraft.jsch.JSch; //導入方法依賴的package包/類
@Override
public Session create() throws Exception
{
if (connected_.compareAndSet(false, true))
{
JSch jsch = new JSch();
session_ = jsch.getSession(loginCredentials_.getUser(), hostAndPort_.getHost(), hostAndPort_.getPortOrDefault(22));
if (sessionTimeout_ != 0)
{
session_.setTimeout(sessionTimeout_);
}
if (loginCredentials_.getPrivateKey() == null)
{
session_.setPassword(loginCredentials_.getPassword());
}
else if (loginCredentials_.hasUnencryptedPrivateKey())
{
jsch.addIdentity(loginCredentials_.getPrivateKey());
// jsch.addIdentity(loginCredentials_.getUser(), loginCredentials_.getPrivateKey().getBytes(), null, new byte[0]);
}
else if (agentConnector_.isPresent())
{
JSch.setConfig("PreferredAuthentications", "publickey");
jsch.setIdentityRepository(new RemoteIdentityRepository(agentConnector_.get()));
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session_.setConfig(config);
if (proxy_.isPresent())
{
session_.setProxy(proxy_.get());
}
session_.connect(connectTimeout_);
LOGGER.info("ssh session started on {}", hostAndPort_);
}
return session_;
}