当前位置: 首页>>代码示例>>Java>>正文


Java Channel.connect方法代码示例

本文整理汇总了Java中com.jcraft.jsch.Channel.connect方法的典型用法代码示例。如果您正苦于以下问题:Java Channel.connect方法的具体用法?Java Channel.connect怎么用?Java Channel.connect使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.jcraft.jsch.Channel的用法示例。


在下文中一共展示了Channel.connect方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: upload

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
private void upload(String filename) {
    try {
        JSch jsch = new JSch();
        Session session = jsch.getSession(login, server, 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("sftp");
        channel.connect();
        ChannelSftp channelSftp = (ChannelSftp) channel;
        channelSftp.cd(workingDirectory);
        File f = new File(filename);

        channelSftp.put(new FileInputStream(f), f.getName());

        f.delete();

    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
 
开发者ID:SKNZ,项目名称:LesPatternsDuSwag,代码行数:26,代码来源:SFTPUploader.java

示例2: executeCommand

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
 * Executes a given command. It opens exec channel for the command and closes
 * the channel when it's done.
 *
 * @param session ssh connection to a remote server
 * @param command command to execute
 * @return command output string if the command succeeds, or null
 */
private static String executeCommand(Session session, String command) {
    if (session == null || !session.isConnected()) {
        return null;
    }

    log.trace("Execute command {} to {}", command, session.getHost());

    try {
        Channel channel = session.openChannel("exec");
        ((ChannelExec) channel).setCommand(command);
        channel.setInputStream(null);
        InputStream output = channel.getInputStream();

        channel.connect();
        String result = CharStreams.toString(new InputStreamReader(output));
        channel.disconnect();

        log.trace("Result of command {} on {}: {}", command, session.getHost(), result);

        return result;
    } catch (JSchException | IOException e) {
        log.error("Failed to execute command {} on {} due to {}", command, session.getHost(), e.toString());
        return null;
    }
}
 
开发者ID:opencord,项目名称:vtn,代码行数:34,代码来源:RemoteIpCommandUtil.java

示例3: getRemoteFileList

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@SuppressWarnings("unchecked")
public static Vector<LsEntry> getRemoteFileList(String user, String password, String addr, int port, String cwd) throws JSchException, 
	SftpException, Exception {
	
	Session session = getSession(user, password, addr, port);	
	Vector<LsEntry> lsVec=null;
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;
	try {
		lsVec=(Vector<LsEntry>)sftpChannel.ls(cwd); //sftpChannel.lpwd()
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();			
	}		
	return lsVec;		
}
 
开发者ID:billchen198318,项目名称:bamboobsc,代码行数:22,代码来源:SFtpClientUtils.java

示例4: listFiles

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
 * Lists directory files on remote server.
 * @throws URISyntaxException 
 * @throws JSchException 
 * @throws SftpException 
 */
private void listFiles() throws URISyntaxException, JSchException, SftpException {
	
	JSch jsch = new JSch();
	JSch.setLogger(new JschLogger());
	setupSftpIdentity(jsch);

	URI uri = new URI(sftpUrl);
	Session session = jsch.getSession(sshLogin, uri.getHost(), 22);
	session.setConfig("StrictHostKeyChecking", "no");
	session.connect();
	System.out.println("Connected to SFTP server");

	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;
	Vector<LsEntry> directoryEntries = sftpChannel.ls(uri.getPath());
	for (LsEntry file : directoryEntries) {
		System.out.println(String.format("File - %s", file.getFilename()));
	}
	sftpChannel.exit();
	session.disconnect();
}
 
开发者ID:szaqal,项目名称:KitchenSink,代码行数:29,代码来源:App.java

示例5: executeCommand

import com.jcraft.jsch.Channel; //导入方法依赖的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

示例6: connectAndExecute

import com.jcraft.jsch.Channel; //导入方法依赖的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

示例7: init

import com.jcraft.jsch.Channel; //导入方法依赖的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;

}
 
开发者ID:PhilippC,项目名称:keepass2android,代码行数:19,代码来源:SftpStorage.java

示例8: put

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
 * 本地檔案放到遠端SFTP上
 * 	
 * @param user
 * @param password
 * @param addr
 * @param port
 * @param localFile
 * @param remoteFile
 * @throws JSchException
 * @throws SftpException
 * @throws Exception
 */
public static void put(String user, String password, String addr, int port,
		List<String> localFile, List<String> remoteFile) throws JSchException, SftpException, Exception {
	
	Session session = getSession(user, password, addr, port);	
	Channel channel = session.openChannel("sftp");
	channel.connect();
	ChannelSftp sftpChannel = (ChannelSftp) channel;		
	try {
		for (int i=0; i<localFile.size(); i++) {
			String rf=remoteFile.get(i);
			String lf=localFile.get(i);
			logger.info("put local file: " + lf + " write to " + addr + " :" + rf );
			sftpChannel.put(lf, rf);
			logger.info("success write to " + addr + " :" + rf);
		}
	} catch (Exception e) {
		e.printStackTrace();
		throw e;
	} finally {
		sftpChannel.exit();
		channel.disconnect();
		session.disconnect();				
	}		
}
 
开发者ID:billchen198318,项目名称:bamboobsc,代码行数:38,代码来源:SFtpClientUtils.java

示例9: copyFile

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
/**
	 * 
	 * @param sourceFilePath
	 * @param destinationFilePath
	 * @param destinationHost
	 * @param destinationPort
	 * @param destinationUserName
	 * @param destinationUserPassword
	 * @param destinationPrivateKeyFilePath
	 * @param destinationPrivateKeyPassphrase
	 * @throws IOException
	 * @throws JSchException
	 * @throws SftpException
	 */
	public static void copyFile(
			String sourceFilePath,
			String destinationFilePath,
			String destinationHost,
			String destinationPort,
			String destinationUserName,
			String destinationUserPassword,
			String destinationPrivateKeyFilePath,
			String destinationPrivateKeyPassphrase) throws IOException, JSchException, SftpException {
		
		JSch jsch = new JSch();
		
		if (!StringUtils.isEmptyOrNull(destinationPrivateKeyFilePath)) {
			jsch.addIdentity(destinationPrivateKeyFilePath, destinationPrivateKeyPassphrase);
		}
		
		int destinationPortNumber = 22;
		if (!StringUtils.isEmptyOrNull(destinationPort)) {
			destinationPortNumber = Integer.parseInt(destinationPort);
		}
		
		Session session = jsch.getSession(destinationUserName, destinationHost, destinationPortNumber);
		if (!StringUtils.isEmptyOrNull(destinationUserPassword)) {
			session.setPassword(destinationUserPassword);
		}
		
//		Properties config = new Properties();
//        config.put("StrictHostKeyChecking", "no");
//        session.setConfig(config);
        session.connect();
        
        Channel channel = session.openChannel("sftp");
        channel.connect();
        
        ChannelSftp channelSftp = (ChannelSftp)channel;        
        File destinationFile = new File(destinationFilePath);
        channelSftp.cd(destinationFile.getPath());
        channelSftp.put(new FileInputStream(sourceFilePath), destinationFile.getName());
		
		
	}
 
开发者ID:Web-of-Building-Data,项目名称:Ifc2Rdf,代码行数:56,代码来源:SshFileUploader.java

示例10: testAuthenticate

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Test
public void testAuthenticate() throws Exception {
  startShell();
  for (boolean interactive : new boolean[]{false, true}) {
    Session session = createSession("paulo", "secret", interactive);
    session.connect();
    Channel channel = session.openChannel("shell");
    channel.connect();
    InputStream in = channel.getInputStream();
    byte[] out = new byte[2];
    assertEquals(2, in.read(out));
    assertEquals("% ", new String(out));
    channel.disconnect();
    session.disconnect();
  }
}
 
开发者ID:vert-x3,项目名称:vertx-shell,代码行数:17,代码来源:SSHTestBase.java

示例11: sendCommand

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
public String sendCommand(String command) throws JSchException, IOException {
   StringBuilder outputBuffer = new StringBuilder();

 
      Channel channel = sesConnection.openChannel("exec");
      ((ChannelExec)channel).setCommand(command);
      InputStream commandOutput = channel.getInputStream();
      channel.connect();
      int readByte = commandOutput.read();

      while(readByte != 0xffffffff)
      {
         outputBuffer.append((char)readByte);
         readByte = commandOutput.read();
      }

      channel.disconnect();
   

   return outputBuffer.toString();
}
 
开发者ID:lucee,项目名称:Lucee,代码行数:22,代码来源:SSHManager.java

示例12: testRead

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Test
public void testRead(TestContext context) throws Exception {
  Async async = context.async();
  termHandler = term -> {
    term.stdinHandler(s -> {
      context.assertEquals("hello", s);
      async.complete();
    });
  };
  startShell();
  Session session = createSession("paulo", "secret", false);
  session.connect();
  Channel channel = session.openChannel("shell");
  channel.connect();
  OutputStream out = channel.getOutputStream();
  out.write("hello".getBytes());
  out.flush();
  channel.disconnect();
  session.disconnect();
}
 
开发者ID:vert-x3,项目名称:vertx-shell,代码行数:21,代码来源:SSHServerTest.java

示例13: testWrite

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
@Test
public void testWrite() throws Exception {
  termHandler = term -> {
    term.write("hello");
  };
  startShell();
  Session session = createSession("paulo", "secret", false);
  session.connect();
  Channel channel = session.openChannel("shell");
  channel.connect();
  Reader in = new InputStreamReader(channel.getInputStream());
  int count = 5;
  StringBuilder sb = new StringBuilder();
  while (count > 0) {
    int code = in.read();
    if (code == -1) {
      count = 0;
    } else {
      count--;
      sb.append((char)code);
    }
  }
  assertEquals("hello", sb.toString());
  channel.disconnect();
  session.disconnect();
}
 
开发者ID:vert-x3,项目名称:vertx-shell,代码行数:27,代码来源:SSHServerTest.java

示例14: doSingleTransfer

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
private void doSingleTransfer() throws IOException, JSchException {
    StringBuilder sb = new StringBuilder("scp -t ");
    if (getPreserveLastModified()) {
        sb.append("-p ");
    }
    if (getCompressed()) {
        sb.append("-C ");
    }
    sb.append(remotePath);
    final String cmd = sb.toString();
    final Channel channel = openExecChannel(cmd);
    try {
        final OutputStream out = channel.getOutputStream();
        final InputStream in = channel.getInputStream();

        channel.connect();

        waitForAck(in);
        sendFileToRemote(localFile, in, out);
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
}
 
开发者ID:apache,项目名称:ant,代码行数:26,代码来源:ScpToMessage.java

示例15: doMultipleTransfer

import com.jcraft.jsch.Channel; //导入方法依赖的package包/类
private void doMultipleTransfer() throws IOException, JSchException {
    StringBuilder sb = new StringBuilder("scp -r -d -t ");
    if (getPreserveLastModified()) {
        sb.append("-p ");
    }
    if (getCompressed()) {
        sb.append("-C ");
    }
    sb.append(remotePath);
    final Channel channel = openExecChannel(sb.toString());
    try {
        final OutputStream out = channel.getOutputStream();
        final InputStream in = channel.getInputStream();

        channel.connect();

        waitForAck(in);
        for (Directory current : directoryList) {
            sendDirectory(current, in, out);
        }
    } finally {
        if (channel != null) {
            channel.disconnect();
        }
    }
}
 
开发者ID:apache,项目名称:ant,代码行数:27,代码来源:ScpToMessage.java


注:本文中的com.jcraft.jsch.Channel.connect方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。