當前位置: 首頁>>代碼示例>>Java>>正文


Java Session.setPassword方法代碼示例

本文整理匯總了Java中com.jcraft.jsch.Session.setPassword方法的典型用法代碼示例。如果您正苦於以下問題:Java Session.setPassword方法的具體用法?Java Session.setPassword怎麽用?Java Session.setPassword使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在com.jcraft.jsch.Session的用法示例。


在下文中一共展示了Session.setPassword方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: executeCommand

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
public void executeCommand(final String command) throws IOException { // Cliente SSH final
    JSch jsch = new JSch();
    Properties props = new Properties();
    props.put("StrictHostKeyChecking", "no");
    try {
        Session session = jsch.getSession(user, host, 22);
        session.setConfig(props);
        session.setPassword(password);
        session.connect();
        java.util.logging.Logger.getLogger(RemoteShell.class.getName())
                .log(Level.INFO, session.getServerVersion());

        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        // Daqui para baixo é somente para imprimir a saida
        channel.setInputStream(null);

        ((ChannelExec) 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;
                }
                System.out.print(new String(tmp, 0, i));
            }
            if (channel.isClosed()) {
                if (in.available() > 0) {
                    continue;
                }
                System.out
                        .println("exit-status: " + channel.getExitStatus());
                break;
            }
            try {
                Thread.sleep(1000);
            } catch (Exception ee) {
            }
        }
        channel.disconnect();
        session.disconnect();

    } catch (JSchException ex) {
        java.util.logging.Logger.getLogger(RemoteShell.class.getName())
                .log(Level.SEVERE, null, ex);
    }
}
 
開發者ID:nailtonvieira,項目名稱:pswot-cloud-java-spring-webapp,代碼行數:54,代碼來源:RemoteShell.java

示例2: connectAndExecute

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
public static String connectAndExecute(String user, String host, String password, String command1) {
	String CommandOutput = null;
	try {

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");
		JSch jsch = new JSch();

		Session session = jsch.getSession(user, host, 22);
		session.setPassword(password);
		session.setConfig(config);
		session.connect();
		// System.out.println("Connected");

		Channel channel = session.openChannel("exec");
		((ChannelExec) channel).setCommand(command1);
		channel.setInputStream(null);
		((ChannelExec) 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;
				// System.out.print(new String(tmp, 0, i));
				CommandOutput = new String(tmp, 0, i);
			}

			if (channel.isClosed()) {
				// System.out.println("exit-status: " +
				// channel.getExitStatus());
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (Exception ee) {
			}
		}
		channel.disconnect();
		session.disconnect();
		// System.out.println("DONE");

	} catch (Exception e) {
		e.printStackTrace();
	}
	return CommandOutput;

}
 
開發者ID:saiscode,項目名稱:kheera,代碼行數:54,代碼來源:SeleniumGridManager.java

示例3: create

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
@Override
public Session create(ConnectionDetails connectionDetails) throws Exception {
    log.debug("Creating session for "+connectionDetails);
    Session session = null;
    try {
        byte[] privateKey = connectionDetails.getPrivateKey();
        if (privateKey != null) {
            jsch.addIdentity(connectionDetails.getUsername(), privateKey, null, connectionDetails.getPassword().getBytes());
        }
        session = jsch.getSession(connectionDetails.getUsername(), connectionDetails.getHost(), connectionDetails.getPort());
        session.setPassword(connectionDetails.getPassword());
        if (!hostKeyValidation) {
            session.setConfig("StrictHostKeyChecking", "no");
        }
        session.setDaemonThread(true);
        session.connect();
    } catch (Exception e) {
        log.error("Failed to connect to "+connectionDetails);
        throw e;
    }
    return session;
}
 
開發者ID:tilln,項目名稱:jmeter-sshmon,代碼行數:23,代碼來源:SSHSessionFactory.java

示例4: connect

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
public static Session connect(String host, Integer port, String user, String password) throws JSchException{
	Session session = null;
	try {
		JSch jsch = new JSch();
		if(port != null){
			session = jsch.getSession(user, host, port.intValue());
		}else{
			session = jsch.getSession(user, host);
		}
		session.setPassword(password);
		
		session.setConfig("StrictHostKeyChecking", "no");
		//time out
		session.connect(3000);
	} catch (JSchException e) {
		e.printStackTrace();
		System.out.println("SFTPUitl connection error");
		throw e;
	}
	return session;
}
 
開發者ID:zhuyuqing,項目名稱:bestconf,代碼行數:22,代碼來源:SFTPUtil.java

示例5: validateCredentials

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
public Message validateCredentials(String host, String user, String password) {

		JSch jsch = new JSch();

		Session session;
		try {
			session = jsch.getSession(user, host, SSH_PORT);

			session.setPassword(password);

			java.util.Properties config = new java.util.Properties();
			config.put(STRICT_HOST_KEY_CHECKING, STRICT_HOST_KEY_CHECKING_DEFAULT_VALUE);
			session.setConfig(config);

			session.setConfig(PREFERRED_AUTHENTICATIONS, PREFERRED_AUTHENTICATIONS_DEFAULT_VALUES);

			session.connect();
			session.disconnect();
		} catch (Exception e) {
			return getErrorMessage(e);
		}

		return new Message(MessageType.SUCCESS, GENERAL_SUCCESS_MESSAGE);
	}
 
開發者ID:capitalone,項目名稱:Hydrograph,代碼行數:25,代碼來源:SCPUtility.java

示例6: deleteFile

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
/**
 * @param host
 * @param user
 * @param pwd
 * @param remoteFile
 */
public void deleteFile(String host, String user, String pwd,
		String remoteFile) {
	try {
		JSch ssh = new JSch();
		Session session = ssh.getSession(user, host, 22);

		java.util.Properties config = new java.util.Properties();
		config.put("StrictHostKeyChecking", "no");
		session.setConfig(config);
		session.setPassword(pwd);

		session.connect();
		Channel channel = session.openChannel("exec");
		channel.connect();

		String command = "rm -rf " + remoteFile;
		System.out.println("command: " + command);
		// ((ChannelExec) channel).setCommand(command);

		channel.disconnect();
		session.disconnect();
	} catch (JSchException e) {
		System.out.println(e.getMessage().toString());
		e.printStackTrace();
	}
}
 
開發者ID:capitalone,項目名稱:Hydrograph,代碼行數:33,代碼來源:ScpFrom.java

示例7: executeSSH

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
private boolean executeSSH(){ 
	//get deployment descriptor, instead of this hard coded.
	// or execute a script on the target machine which download artifact from nexus
       String command ="nohup java -jar -Dserver.port=8091 ./work/codebox/chapter6/chapter6.search/target/search-1.0.jar &";
      try{	
   	   System.out.println("Executing "+ command);
          java.util.Properties config = new java.util.Properties(); 
          config.put("StrictHostKeyChecking", "no");
          JSch jsch = new JSch();
          Session session=jsch.getSession("rajeshrv", "localhost", 22);
          session.setPassword("rajeshrv");
          
          session.setConfig(config);
          session.connect();
          System.out.println("Connected");
           
          ChannelExec channelExec = (ChannelExec)session.openChannel("exec");
          InputStream in = channelExec.getInputStream();
          channelExec.setCommand(command);
          channelExec.connect();
         
          BufferedReader reader = new BufferedReader(new InputStreamReader(in));
          String line;
          int index = 0;

          while ((line = reader.readLine()) != null) {
              System.out.println(++index + " : " + line);
          }
          channelExec.disconnect();
          session.disconnect();

          System.out.println("Done!");

      }catch(Exception e){
          e.printStackTrace();
          return false;
      }
	
	return true;
}
 
開發者ID:rajeshrv,項目名稱:SpringMicroservice,代碼行數:41,代碼來源:DeploymentEngine.java

示例8: sshCall

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
protected void sshCall(String username, String password, SshExecutor executor, String channelType) {
    try {
        JSch jsch = new JSch();
        Session session = jsch.getSession(username, props.getShell().getHost(), props.getShell().getPort());
        session.setPassword(password);
        Properties config = new Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        Channel channel = session.openChannel(channelType);
        PipedInputStream pis = new PipedInputStream();
        PipedOutputStream pos = new PipedOutputStream();
        channel.setInputStream(new PipedInputStream(pos));
        channel.setOutputStream(new PipedOutputStream(pis));
        channel.connect();
        try {
            executor.execute(pis, pos);
        } finally {
            pis.close();
            pos.close();
            channel.disconnect();
            session.disconnect();
        }
    } catch(JSchException | IOException ex) {
        fail(ex.toString());
    }
}
 
開發者ID:anand1st,項目名稱:sshd-shell-spring-boot,代碼行數:28,代碼來源:AbstractSshSupport.java

示例9: openSession

import com.jcraft.jsch.Session; //導入方法依賴的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;
}
 
開發者ID:Asymmetrik,項目名稱:nifi-nars,代碼行數:41,代碼來源:AbstractScpProcessor.java

示例10: 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;
}
 
開發者ID:Coding,項目名稱:WebIDE-Backend,代碼行數:26,代碼來源:MultiUserSshSessionFactory.java

示例11: exec

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
@SneakyThrows
public static int exec(@Nonnull String host,
                       int port,
                       @Nonnull String username,
                       @Nonnull String password,
                       @Nonnull String cmd) {
    JSch jSch = new JSch();

    Session session = jSch.getSession(username, host, port);
    session.setPassword(password);
    java.util.Properties config = new java.util.Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.connect();
    Channel channel = session.openChannel("exec");
    ((ChannelExec) channel).setCommand(cmd);
    ((ChannelExec) channel).setInputStream(null);
    ((ChannelExec) channel).setErrStream(System.err);
    InputStream inputStream = channel.getInputStream();
    channel.connect();

    log.info("# successful connect to server [{}:{}]", host, port);

    log.info("# exec cmd [{}]", cmd);

    StringBuilder sb = new StringBuilder();
    byte[] bytes = new byte[1024];
    int exitStatus;
    while (true) {
        while (inputStream.available() > 0) {
            int i = inputStream.read(bytes, 0, 1024);
            if (i < 0) {
                break;
            }
            sb.append(new String(bytes, 0, i, StandardCharsets.UTF_8));
        }
        if (channel.isClosed()) {
            if (inputStream.available() > 0) {
                continue;
            }
            exitStatus = channel.getExitStatus();
            break;
        }
        Thread.sleep(1000);
    }
    if (StringUtils.isNotEmpty(sb)) {
        log.info("# cmd-rs \n" + sb);
    }
    channel.disconnect();
    session.disconnect();

    log.info("# successful disconnect to server [{}:{}]", host, port);

    return exitStatus;
}
 
開發者ID:srarcbrsent,項目名稱:tc,代碼行數:56,代碼來源:TcSshBin.java

示例12: 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;
}
 
開發者ID:danielemaddaluno,項目名稱:command4j,代碼行數:23,代碼來源:SessionFactory.java

示例13: doSshTunnel

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
private Session doSshTunnel(int nLocalPort) throws JSchException {
	final JSch jsch = new JSch();
	Session session = jsch.getSession(strSshUser, strSshHost, nSshPort);

	if (!useKeyFile)
		session.setPassword(strSshPassword);
	else
		jsch.addIdentity(new File(keyFilePath).getAbsolutePath());

	final Properties config = new Properties();
	config.put("StrictHostKeyChecking", "no");
	session.setConfig(config);

	session.connect();
	session.setPortForwardingL(nLocalPort, databaseServerName, databaseServerPort);

	return session;
}
 
開發者ID:organicsmarthome,項目名稱:OSHv4,代碼行數:19,代碼來源:SingleSQLAndSSHConnectionProvider.java

示例14: getSession

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
public synchronized Session getSession(Uri path) throws JSchException{
    String username="anonymous";

    String password = "";
    NetworkCredentialsDatabase database = NetworkCredentialsDatabase.getInstance();
    Credential cred = database.getCredential(path.toString());
    if(cred==null){
        cred = new Credential("anonymous","",buildKeyFromUri(path).toString(), true);

    }
    if(cred!=null){
        password= cred.getPassword();
        username = cred.getUsername();
    }

    Session session = sessions.get(cred);
    if(session!=null){
        if(!session.isConnected())
            try {
                session.connect();
            } catch (JSchException e1) {
                removeSession(path);
                return getSession(path);
            }
        return session;
    }

    JSch jsch=new JSch();
    try {
        session = jsch.getSession(username, path.getHost(), path.getPort());
        session.setPassword(password);
        java.util.Properties config = new java.util.Properties();
        config.put("StrictHostKeyChecking", "no");
        session.setConfig(config);
        session.connect();
        sessions.put(cred, session);
        return session;
    } catch (JSchException e) {
        // TODO Auto-generated catch block
        throw e;

    }
    

}
 
開發者ID:archos-sa,項目名稱:aos-FileCoreLibrary,代碼行數:46,代碼來源:SFTPSession.java

示例15: connect

import com.jcraft.jsch.Session; //導入方法依賴的package包/類
/**
 * 連接到服務器
 */
private static Session connect(String host,String user,String pass) throws JSchException{
	JSch sch = new JSch();
	Session session = sch.getSession(user, host, 22);
	session.setPassword(pass);
	Properties config = new Properties();
    config.put("StrictHostKeyChecking", "no");
    session.setConfig(config);
    session.setTimeout(60000);
    session.connect();
    return session;
}
 
開發者ID:yanfanvip,項目名稱:RedisClusterManager,代碼行數:15,代碼來源:ScpClient.java


注:本文中的com.jcraft.jsch.Session.setPassword方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。