本文整理汇总了Java中com.jcraft.jsch.UserInfo类的典型用法代码示例。如果您正苦于以下问题:Java UserInfo类的具体用法?Java UserInfo怎么用?Java UserInfo使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
UserInfo类属于com.jcraft.jsch包,在下文中一共展示了UserInfo类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: init
import com.jcraft.jsch.UserInfo; //导入依赖的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: SshXpraConnector
import com.jcraft.jsch.UserInfo; //导入依赖的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");
}
示例3: scpFileFromRemoteServer
import com.jcraft.jsch.UserInfo; //导入依赖的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: create
import com.jcraft.jsch.UserInfo; //导入依赖的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;
}
示例5: get
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
/**
* Gets SSH Client
*
* @return
*/
@SneakyThrows(JSchException.class)
public Session get() {
JSch jsch = new JSch();
UserInfo userInfo;
Session session = jsch.getSession(username, hostname, port);
if (useKey) {
jsch.addIdentity(key);
userInfo = new EmbeddedUserInfoInteractive();
} else {
session.setPassword(password);
session.setConfig("StrictHostKeyChecking", "no");
userInfo = EmbeddedUserInfo.builder().password(password).build();
}
session.setUserInfo(userInfo);
session.setConfig("HashKnownHosts", "yes");
session.setTimeout(10000);
session.connect();
return session;
}
示例6: connectAndGetSftpChannel
import com.jcraft.jsch.UserInfo; //导入依赖的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;
}
示例7: wrapAuthentication
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
void wrapAuthentication(TransportCommand command, UsernamePasswordCredentialsProvider ownerAuth,
String sshPassword, File credentialsFile, List<String> credentialsList,
UserInfo userInfo) {
if (ownerAuth != null)
command.setCredentialsProvider(ownerAuth);
else
command.setCredentialsProvider(new ElegitCredentialsProvider(credentialsList));
command.setTransportConfigCallback(
new TransportConfigCallback() {
@Override
public void configure(Transport transport) {
if (transport instanceof TransportGitSsh) {
SshTransport sshTransport = (SshTransport) transport;
sshTransport.setSshSessionFactory(sshSessionFactory);
}
}
});
}
示例8: connect
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
@Override
public void connect() throws SocketException, IOException {
try {
session = jsch.getSession(username,host.getHostAddress() , port);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
UserInfo ui=new UserInfoImpl(password,null);
session.setUserInfo(ui);
if(timeout>0) session.setTimeout(timeout);
session.connect();
Channel channel=session.openChannel("sftp");
channel.connect();
channelSftp = (ChannelSftp)channel;
// check fingerprint
if(!StringUtil.isEmpty(fingerprint)) {
if(!fingerprint.equalsIgnoreCase(fingerprint())) {
disconnect();
throw new IOException("given fingerprint is not a match.");
}
}
handleSucess();
}
catch(JSchException e){
handleFail(e, stopOnError);
}
}
示例9: getMemoryUtilisation
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
/**
* Gets the information related to memory utilization for each node.
*
* @param config
* the config
* @param nodeIp
* the node ip
* @return list of pojos containing memory utilization info for each node
* @throws JSchException
* the j sch exception
* @throws IOException
* Signals that an I/O exception has occurred.
*/
public Map<String, String> getMemoryUtilisation(Config config, String nodeIp) throws JSchException, IOException {
JobConfig jobConfig = (JobConfig)config;
Slave slave = RemoteFileUtil.findSlave(nodeIp, jobConfig.getSlaves());
String user = slave.getUser();
String rsaFilePath = jobConfig.getMaster().getRsaFile();
String dsaFilePath = jobConfig.getMaster().getDsaFile();
Session session = null;
Map<String, String> vmStats = null;
try {
session = createSession(user, nodeIp, rsaFilePath, dsaFilePath);
UserInfo ui = new JumbuneUserInfo();
session.setUserInfo(ui);
session.connect();
vmStats = getVmStats(session);
} finally {
session.disconnect();
}
return vmStats;
}
示例10: createSession
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
/**
* For creating a JSCH session.
*
* @param user
* the user
* @param host
* the host
* @param rsaFilePath
* the rsa file path
* @param dsaFilePath
* the dsa file path
* @return a JSCH session
* @throws JSchException
* the j sch exception
*/
public static Session createSession(String user, String host, String rsaFilePath, String dsaFilePath, CommandType commandType) throws JSchException {
JSch jsch = new JSch();
Session session = null;
SwitchedIdentity switchedIdentity = changeIdentityAsPerCommand(user, rsaFilePath, dsaFilePath, commandType);
if(commandType.equals(CommandType.FS)){
jsch.addIdentity(switchedIdentity.getPrivatePath());
}
java.util.Properties conf = new java.util.Properties();
session = jsch.getSession(switchedIdentity.getUser(), host, RemotingConstants.TWENTY_TWO);
UserInfo info = getSwitchedUser(commandType, switchedIdentity);
session.setUserInfo(info);
conf.put(STRICT_HOST_KEY_CHECKING, "no");
session.setConfig(conf);
LOGGER.debug("Session Established, for user ["+switchedIdentity.getUser()+"]");
return session;
}
示例11: verifyPassword
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
public static boolean verifyPassword(String user, String encryptedPasswrd) throws JSchException {
JSch jsch = new JSch();
Session session = null;
java.util.Properties conf = new java.util.Properties();
session = jsch.getSession(user, "localhost", RemotingConstants.TWENTY_TWO);
UserInfo info = new JumbuneUserInfo(StringUtil.getPlain(encryptedPasswrd));
session.setUserInfo(info);
conf.put(STRICT_HOST_KEY_CHECKING, "no");
session.setConfig(conf);
// LOGGER.debug("Session Established, for user ["+user+"]");
boolean isConnected = false;
if(session!=null){
session.connect();
isConnected = session.isConnected();
LOGGER.debug("Session Connected, for user ["+user+"]");
session.disconnect();
}
return isConnected;
}
示例12: execCmd
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
/**
* Execute command
* @param host
* @param user
* @param passwd
* @param cmd
* @return String the reply of remote session
*/
public String execCmd (String host, String user, String passwd, String cmd){
String replyy = "";
String reply = null;
try{
JSch jsch=new JSch();
Session session=jsch.getSession(user, host, 22);
UserInfo ui = new MyUserInfo(passwd);
session.setUserInfo(ui);
session.setTimeout(600000);
session.connect();
Channel channel=session.openChannel("exec");
((ChannelExec)channel).setCommand(cmd);
channel.setInputStream(null);
InputStreamReader in = new InputStreamReader(channel.getInputStream());
OutputStreamWriter out = new OutputStreamWriter(channel.getOutputStream());
BufferedWriter bw = new BufferedWriter(out);
BufferedReader br = new BufferedReader(in);
channel.connect();
while ((reply = br.readLine()) != null) {
bw.write(reply);
replyy=replyy+"\n"+reply;
bw.flush();
Thread.sleep(100);
}
while(true){
if(channel.isClosed()){
break;
} try{
Thread.sleep(1500);
} catch(Exception ee){
}
}
in.close();
out.close();
br.close();
bw.close();
channel.disconnect();
session.disconnect();
}
catch(Exception e){
log.error("ERROR , Possible no connection with : "+user+" "+passwd+ " "+host+"\n\t\t please check LAN and vpn connection or host");
}
return replyy;
}
示例13: outputCommandSSH
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
/**
* Execute SSH command and return output
* @param username The ssh user
* @param host The host to connect
* @param key The ssh key
* @param commandssh The command to launch
* @return the output of the command
*/
public String outputCommandSSH(String username, String host, String key, String commandssh){
String res = "";
try{
JSch jsch=new JSch();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no"); //avoid host key checking
int SSH_port = 22;
jsch.addIdentity(key , "passphrase");
Session session=jsch.getSession(username, host, SSH_port); //SSH connection
if(new Configuration().Debug){
System.out.println("Connection to "+host+" on the port "+SSH_port+" with key: "+key+" and username: "+username);
}
// username and passphrase will be given via UserInfo interface.
UserInfo ui=new MyUserInfo();
session.setUserInfo(ui);
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("exec"); // open channel for exec command
((ChannelExec)channel).setCommand(commandssh);
channel.setInputStream(null);
((ChannelExec)channel).setErrStream(System.err);
InputStream in=channel.getInputStream();
channel.connect();
/*
* get output ssh command launched
*/
byte[] tmp=new byte[1024];
while(true){
while(in.available()>0){
int i=in.read(tmp, 0, 1024);
if(i<0)break;
res += new String(tmp, 0, i);
//System.out.print(new String(tmp, 0, i));
}
if(channel.isClosed()){
if(in.available()>0) continue;
if(new Configuration().Debug){
System.out.println("exit-status: "+channel.getExitStatus());
}
break;
}
try{Thread.sleep(1000);}catch(Exception ee){}
}
channel.disconnect();
session.disconnect();
}
catch(Exception e){
System.out.println(e);
}
return res;
}
示例14: connect
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
public void connect() {
try {
session = jsch.getSession(username, server, port);
if (logger.isDebugEnabled())
logger.debug(String.format(
"login ssh to server %s:%d as user %s password %s", server,
port, username, password));
UserInfo ui = new MyUserInfo(password);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(password);
// session.setUserInfo(ui);
session.connect();
} catch (JSchException e) {
throw new RuntimeException(e);
}
}
示例15: createSession
import com.jcraft.jsch.UserInfo; //导入依赖的package包/类
public Session createSession(PrintStream logger) {
JSch jsch = new JSch();
Session session = null;
try {
session = jsch.getSession(username, ip, port);
session.setPassword(password);
UserInfo ui = new GsshUserInfo(password);
session.setUserInfo(ui);
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setDaemonThread(false);
session.connect();
logger.println("create ssh session success with ip=[" + ip
+ "],port=[" + port + "],username=[" + username
+ "],password=[*******]");
} catch (Exception e) {
logger.println("create ssh session failed with ip=[" + ip
+ "],port=[" + port + "],username=[" + username
+ "],password=[*******]");
e.printStackTrace(logger);
throw new GsshPluginException(e);
}
return session;
}