本文整理汇总了Java中com.jcraft.jsch.Session.setUserInfo方法的典型用法代码示例。如果您正苦于以下问题:Java Session.setUserInfo方法的具体用法?Java Session.setUserInfo怎么用?Java Session.setUserInfo使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.jcraft.jsch.Session
的用法示例。
在下文中一共展示了Session.setUserInfo方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
ChannelSftp init(String filename) throws JSchException, UnsupportedEncodingException {
jsch = new JSch();
ConnectionInfo ci = splitStringToConnectionInfo(filename);
Session session = jsch.getSession(ci.username, ci.host, ci.port);
UserInfo ui = new SftpUserInfo(ci.password);
session.setUserInfo(ui);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp) channel;
logDebug("success: init Sftp");
return c;
}
示例2: pushToRepository
import com.jcraft.jsch.Session; //导入方法依赖的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);
}
}
示例3: scpFileFromRemoteServer
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
/**
*
* Scp file from remote server
*
* @param host
* @param user
* @param password
* @param remoteFile
* @param localFile
* @throws JSchException
* @throws IOException
*/
public void scpFileFromRemoteServer(String host, String user, String password, String remoteFile, String localFile) throws JSchException, IOException {
String prefix = null;
if (new File(localFile).isDirectory()) {
prefix = localFile + File.separator;
}
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
// username and password will be given via UserInfo interface.
UserInfo userInfo = new UserInformation(password);
session.setUserInfo(userInfo);
session.connect();
// exec 'scp -f remoteFile' remotely
String command = "scp -f " + remoteFile;
Channel channel = session.openChannel("exec");
((ChannelExec) channel).setCommand(command);
// get I/O streams for remote scp
OutputStream out = channel.getOutputStream();
InputStream in = channel.getInputStream();
channel.connect();
byte[] buf = new byte[1024];
// send '\0'
buf[0] = 0;
out.write(buf, 0, 1);
out.flush();
readRemoteFileAndWriteToLocalFile(localFile, prefix, out, in, buf);
session.disconnect();
}
示例4: createSession
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
private Session createSession(CredentialsProvider credentialsProvider,
FS fs, String user, final String pass, String host, int port,
final OpenSshConfig.Host hc) throws JSchException {
final Session session = createSession(credentialsProvider, hc, user, host, port, fs);
// We retry already in getSession() method. JSch must not retry
// on its own.
session.setConfig("MaxAuthTries", "1"); //$NON-NLS-1$ //$NON-NLS-2$
if (pass != null)
session.setPassword(pass);
final String strictHostKeyCheckingPolicy = hc
.getStrictHostKeyChecking();
if (strictHostKeyCheckingPolicy != null)
session.setConfig("StrictHostKeyChecking", //$NON-NLS-1$
strictHostKeyCheckingPolicy);
final String pauth = hc.getPreferredAuthentications();
if (pauth != null)
session.setConfig("PreferredAuthentications", pauth); //$NON-NLS-1$
if (credentialsProvider != null && !(credentialsProvider instanceof PrivateKeyCredentialsProvider)
&& (!hc.isBatchMode() || !credentialsProvider.isInteractive())) {
session.setUserInfo(new CredentialsProviderUserInfo(session,
credentialsProvider));
}
configure(hc, session);
return session;
}
示例5: create
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
@Override
public Session create(ServerDetails serverDetails) throws Exception {
Session session = null;
try {
JSch jsch = new JSch();
if (serverDetails.getPrivateKeyLocation() != null) {
jsch.addIdentity(serverDetails.getPrivateKeyLocation());
}
session = jsch.getSession(serverDetails.getUser(), serverDetails.getHost(), serverDetails.getPort());
session.setConfig("StrictHostKeyChecking", "no"); //
UserInfo userInfo = new JschUserInfo(serverDetails.getUser(), serverDetails.getPassword());
session.setUserInfo(userInfo);
session.setTimeout(60000);
session.setPassword(serverDetails.getPassword());
session.connect();
} catch (Exception e) {
throw new RuntimeException(
"ERROR: Unrecoverable error when trying to connect to serverDetails : "
+ serverDetails, e);
}
return session;
}
示例6: pullFromRepository
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
/**
* Pull repository from current branch and remote branch with same name as current
*
* @param git
* instance.
* @param remote
* to be used.
* @param remoteBranch
* to use.
* @param passphrase
* to access private key.
* @param privateKey
* file location.
*/
public PullResult pullFromRepository(final Git git, final String remote, String remoteBranch, final String passphrase,
final 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.pull()
.setRemote(remote)
.setRemoteBranchName(remoteBranch)
.setTransportConfigCallback(transport -> {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
})
.call();
} catch (GitAPIException e) {
throw new IllegalStateException(e);
}
}
示例7: cloneRepository
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
/**
* Clones a private remote git repository. Caller is responsible of closing git repository.
*
* @param remoteUrl
* to connect.
* @param localPath
* where to clone the repo.
* @param passphrase
* to access private key.
* @param privateKey
* file location. If null default (~.ssh/id_rsa) location is used.
*
* @return Git instance. Caller is responsible to close the connection.
*/
public Git cloneRepository(final String remoteUrl, final Path localPath, final String passphrase,
final 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.cloneRepository()
.setURI(remoteUrl)
.setTransportConfigCallback(transport -> {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
})
.setDirectory(localPath.toFile())
.call();
} catch (GitAPIException e) {
throw new IllegalStateException(e);
}
}
示例8: executeSshCommand
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
/**
* Connect to a remote host via SSH and execute a command.
*
* @param host
* Host to connect to
* @param user
* User to login with
* @param password
* Password for the user
* @param command
* Command to execute
* @return The result of the command execution
*/
private Result executeSshCommand(final String host, final String user,
final String password, final String command) {
StringBuilder result = new StringBuilder();
try {
JSch jsch = new JSch();
Session session = jsch.getSession(user, host, 22);
session.setUserInfo(createUserInfo(password));
session.connect(5000);
ChannelExec channel = (ChannelExec) session.openChannel("exec");
channel.setCommand(command);
channel.setInputStream(null);
channel.setErrStream(System.err);
InputStream in = channel.getInputStream();
channel.connect();
byte[] tmp = new byte[1024];
while (true) {
while (in.available() > 0) {
int i = in.read(tmp, 0, 1024);
if (i < 0)
break;
result.append(new String(tmp, 0, i));
}
if (channel.isClosed()) {
break;
}
}
channel.disconnect();
session.disconnect();
} catch (Exception jex) {
return createResult(Result.Status.ERROR, jex.getMessage());
}
return createResult(Result.Status.OK, result.toString());
}
示例9: startNewSession
import com.jcraft.jsch.Session; //导入方法依赖的package包/类
private Session startNewSession(boolean acquireChannel) throws JSchException, InterruptedException {
Session newSession = null;
final AtomicBoolean cancelled = new AtomicBoolean(false);
ConnectingProgressHandle.startHandle(env, new Cancellable() {
@Override
public boolean cancel() {
cancelled.set(true);
return true;
}
});
try {
while (!cancelled.get()) {
try {
newSession = jsch.getSession(env.getUser(), env.getHostAddress(), env.getSSHPort());
int serverAliveInterval = Integer.getInteger("jsch.server.alive.interval", 0); // NOI18N
if (serverAliveInterval > 0) {
newSession.setServerAliveInterval(serverAliveInterval);
int serverAliveCount = Integer.getInteger("jsch.server.alive.count", 5); // NOI18N
newSession.setServerAliveCountMax(serverAliveCount);
}
newSession.setUserInfo(userInfo);
for (Entry<String, String> entry : jschSessionConfig.entrySet()) {
newSession.setConfig(entry.getKey(), entry.getValue());
}
Authentication auth = Authentication.getFor(env);
final String preferredAuthKey = "PreferredAuthentications"; // NOI18N
if (!jschSessionConfig.containsKey(preferredAuthKey)) {
String methods = auth.getAuthenticationMethods().toJschString();
if (methods != null) {
log.finest("Setting auth method list to " + methods); //NOI18N
newSession.setConfig(preferredAuthKey, methods);
}
}
if (USE_JZLIB) {
newSession.setConfig("compression.s2c", "[email protected],zlib,none"); // NOI18N
newSession.setConfig("compression.c2s", "[email protected],zlib,none"); // NOI18N
newSession.setConfig("compression_level", "9"); // NOI18N
}
if (RemoteStatistics.COLLECT_STATISTICS && RemoteStatistics.COLLECT_TRAFFIC) {
newSession.setSocketFactory(MeasurableSocketFactory.getInstance());
}
newSession.connect(auth.getTimeout()*1000);
break;
} catch (JSchException ex) {
if (!UNIT_TEST_MODE) {
String msg = ex.getMessage();
if (msg == null) {
throw ex;
}
if (msg.startsWith("Auth fail") || msg.startsWith("SSH_MSG_DISCONNECT: 2")) { // NOI18N
PasswordManager.getInstance().clearPassword(env);
}
} else {
throw ex;
}
} catch (CancellationException cex) {
cancelled.set(true);
}
}
if (cancelled.get()) {
throw new InterruptedException("StartNewSession was cancelled ..."); // NOI18N
}
// In case of any port-forwarding previously set for this env
// init the new session appropriately
portForwarding.initSession(newSession);
sessions.put(newSession, new AtomicInteger(JSCH_CHANNELS_PER_SESSION - (acquireChannel ? 1 : 0)));
log.log(Level.FINE, "New session [{0}] started.", new Object[]{System.identityHashCode(newSession)}); // NOI18N
} finally {
ConnectingProgressHandle.stopHandle(env);
}
return newSession;
}