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


Java Connection.close方法代码示例

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


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

示例1: getConnection

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
public Connection getConnection(){
	int round = 0;
   	while(round<maxRoundConnection){
   		try{
   			connection = new Connection(server);
   			connection.connect();
   			boolean isAuthenticated = connection.authenticateWithPassword(username, password);
   			if (isAuthenticated == false) {
   				throw new IOException("Authentication failed...");
   			}
   			break;
   		}catch (Exception e) {
   			e.printStackTrace();
   			connection.close();
   			connection = null;
   			System.err.println("================= connection is null in round "+round);
   			try {
				Thread.sleep(sshReconnectWatingtime*1000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
   		}
   		round++;
   	}
	return connection;
}
 
开发者ID:zhuyuqing,项目名称:bestconf,代码行数:27,代码来源:SUTTest.java

示例2: getConnection

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
public Connection getConnection(){
   	int round = 0;
   	while(round<maxRoundConnection){
   		try{
   			connection = new Connection(server);
   			connection.connect();
   			boolean isAuthenticated = connection.authenticateWithPassword(username, password);
   			if (isAuthenticated == false) {
   				throw new IOException("Authentication failed...");
   			}
   			break;
   		}catch (Exception e) {
   			e.printStackTrace();
   			connection.close();
   			connection = null;
   			System.err.println("================= connection is null in round "+round);
   			try {
				Thread.sleep(sshReconnectWatingtime*1000);
			} catch (InterruptedException e1) {
				e1.printStackTrace();
			}
   		}
   		round++;
   	}
	return connection;
}
 
开发者ID:zhuyuqing,项目名称:bestconf,代码行数:27,代码来源:SUTSystemOperation.java

示例3: close

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
private void close(Connection conn) {
	if (conn != null) {
        try {
        	conn.close();
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        }
    }
}
 
开发者ID:ggj2010,项目名称:javabase,代码行数:10,代码来源:SSHTemplate.java

示例4: exeRemoteConsole

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
public static List<String> exeRemoteConsole(String hostname, String username, String password, String cmd) {
        List<String> result = new ArrayList<String>();
        //指明连接主机的IP地址
        Connection conn = new Connection(hostname);
        Session ssh = null;
        try {
            //连接到主机
            conn.connect();
            //使用用户名和密码校验
            boolean isconn = conn.authenticateWithPassword(username, password);
            if (!isconn) {
                logger.error("用户名称或者是密码不正确");
            } else {
                logger.info("已经连接OK");
                ssh = conn.openSession();
                //使用多个命令用分号隔开
//              ssh.execCommand("pwd;cd /tmp;mkdir shb;ls;ps -ef|grep weblogic");
                ssh.execCommand(cmd);
                //只允许使用一行命令,即ssh对象只能使用一次execCommand这个方法,多次使用则会出现异常
//              ssh.execCommand("mkdir hb");
                //将屏幕上的文字全部打印出来
                InputStream is = new StreamGobbler(ssh.getStdout());
                BufferedReader brs = new BufferedReader(new InputStreamReader(is));
                for (String line = brs.readLine(); line != null; line = brs.readLine()) {
                    result.add(line);
                }
            }
            //连接的Session和Connection对象都需要关闭
            if (ssh != null) {
                ssh.close();
            }
            conn.close();
        } catch (IOException e) {
            logger.error("", e);
        }
        return result;
    }
 
开发者ID:canmind,项目名称:fastdfs-zyc,代码行数:38,代码来源:Tools.java

示例5: exportDataBase

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
/**
     * export database;
     */
    public void exportDataBase() {
        logger.info("start backup database");
        String username = Config.getProperty("jdbc.username_dev");
        String password = Config.getProperty("jdbc.password_dev");
        String database = Config.getProperty("jdbc.database");
        String host = Config.getProperty("jdbc.host_dev");
        String os = System.getProperty("os.name");
        String file_path = null;
//        if (os.toLowerCase().startsWith("win")) {       //根据系统类型
//            file_path = System.getProperty("user.dir") + "\\sql\\";
//        } else {
//            file_path = System.getProperty("user.dir") + "/sql/";//保存的路径
//        }
        file_path = System.getProperty("myblog.path") + "sql";
        String file_name = "/myblog" + DateTime.now().toString("yyyyMMddHHmmss") + ".sql";
        String file = file_path + file_name;
        logger.info("file_path and file_name: " + file);
        //server
        String s_host = Config.getProperty("server.host");
        Integer s_port = Config.getIntProperty("server.port");
        String s_username = Config.getProperty("server.username");
        String s_password = Config.getProperty("server.password");
        try {
            StringBuffer sb = new StringBuffer();
            sb.append("mysqldump -u " + username + " -p" + password + " -h " + host + " " +
                    database + " >" + file);
            String sql = sb.toString();
            logger.info(sql);
            //connect to server
            Connection connection = new Connection(s_host, s_port);
            connection.connect();
            boolean isAuth = connection.authenticateWithPassword(s_username, s_password);
            if (!isAuth) {
                logger.error("server login error");
            }
            Session session = connection.openSession();
            session.execCommand(sql);
            InputStream stdout = new StreamGobbler(session.getStdout());
            BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
            while (true) {
                String line = br.readLine();
                if (line == null)
                    break;
                System.out.println(line);
            }
            session.close();
            connection.close();
            stdout.close();
            br.close();
            logger.info("backup finish");
            logger.info(sb.toString());
        } catch (Exception e) {
            logger.error("error", e);
        }
    }
 
开发者ID:Zephery,项目名称:newblog,代码行数:59,代码来源:MysqlUtil.java

示例6: main

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args)
{
	String hostname = "127.0.0.1";
	String username = "joe";
	String password = "joespass";

	try
	{
		/* Create a connection instance */

		Connection conn = new Connection(hostname);

		/* Now connect */

		conn.connect();

		/* Authenticate.
		 * If you get an IOException saying something like
		 * "Authentication method password not supported by the server at this stage."
		 * then please check the FAQ.
		 */

		boolean isAuthenticated = conn.authenticateWithPassword(username, password);

		if (isAuthenticated == false)
			throw new IOException("Authentication failed.");

		/* Create a session */

		Session sess = conn.openSession();

		sess.execCommand("uname -a && date && uptime && who");

		System.out.println("Here is some information about the remote host:");

		/* 
		 * This basic example does not handle stderr, which is sometimes dangerous
		 * (please read the FAQ).
		 */

		InputStream stdout = new StreamGobbler(sess.getStdout());

		BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

		while (true)
		{
			String line = br.readLine();
			if (line == null)
				break;
			System.out.println(line);
		}

		/* Show exit status, if available (otherwise "null") */

		System.out.println("ExitCode: " + sess.getExitStatus());

		/* Close this session */

		sess.close();

		/* Close the connection */

		conn.close();

	}
	catch (IOException e)
	{
		e.printStackTrace(System.err);
		System.exit(2);
	}
}
 
开发者ID:dreajay,项目名称:jcode,代码行数:72,代码来源:Basic.java

示例7: main

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args)
{
	String hostname = "my-ssh-server";
	String username = "joe";
	String password = "joespass";

	String proxyHost = "192.168.1.1";
	int proxyPort = 3128; // default port used by squid
	
	try
	{
		/* Create a connection instance */

		Connection conn = new Connection(hostname);

		/* We want to connect through a HTTP proxy */
		
		conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort));
		
		// if the proxy requires basic authentication:
		// conn.setProxyData(new HTTPProxyData(proxyHost, proxyPort, "username", "secret"));
		
		/* Now connect (through the proxy) */

		conn.connect();

		/* Authenticate.
		 * If you get an IOException saying something like
		 * "Authentication method password not supported by the server at this stage."
		 * then please check the FAQ.
		 */

		boolean isAuthenticated = conn.authenticateWithPassword(username, password);

		if (isAuthenticated == false)
			throw new IOException("Authentication failed.");

		/* Create a session */

		Session sess = conn.openSession();

		sess.execCommand("uname -a && date && uptime && who");

		System.out.println("Here is some information about the remote host:");

		/* 
		 * This basic example does not handle stderr, which is sometimes dangerous
		 * (please read the FAQ).
		 */

		InputStream stdout = new StreamGobbler(sess.getStdout());

		BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

		while (true)
		{
			String line = br.readLine();
			if (line == null)
				break;
			System.out.println(line);
		}

		/* Show exit status, if available (otherwise "null") */

		System.out.println("ExitCode: " + sess.getExitStatus());

		/* Close this session */

		sess.close();

		/* Close the connection */

		conn.close();

	}
	catch (IOException e)
	{
		e.printStackTrace(System.err);
		System.exit(2);
	}
}
 
开发者ID:dreajay,项目名称:jcode,代码行数:82,代码来源:BasicWithHTTPProxy.java

示例8: main

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args)
{
	String hostname = "127.0.0.1";
	String username = "joe";

	File keyfile = new File("~/.ssh/id_rsa"); // or "~/.ssh/id_dsa"
	String keyfilePass = "joespass"; // will be ignored if not needed

	try
	{
		/* Create a connection instance */

		Connection conn = new Connection(hostname);

		/* Now connect */

		conn.connect();

		/* Authenticate */

		boolean isAuthenticated = conn.authenticateWithPublicKey(username, keyfile, keyfilePass);

		if (isAuthenticated == false)
			throw new IOException("Authentication failed.");

		/* Create a session */

		Session sess = conn.openSession();

		sess.execCommand("uname -a && date && uptime && who");

		InputStream stdout = new StreamGobbler(sess.getStdout());

		BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

		System.out.println("Here is some information about the remote host:");

		while (true)
		{
			String line = br.readLine();
			if (line == null)
				break;
			System.out.println(line);
		}

		/* Close this session */
		
		sess.close();

		/* Close the connection */

		conn.close();

	}
	catch (IOException e)
	{
		e.printStackTrace(System.err);
		System.exit(2);
	}
}
 
开发者ID:dreajay,项目名称:jcode,代码行数:61,代码来源:PublicKeyAuthentication.java

示例9: main

import ch.ethz.ssh2.Connection; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException
{
	String hostname = "somehost";
	String username = "joe";
	String password = "joespass";

	File knownHosts = new File("~/.ssh/known_hosts");

	try
	{
		/* Load known_hosts file into in-memory database */

		if (knownHosts.exists())
			database.addHostkeys(knownHosts);

		/* Create a connection instance */

		Connection conn = new Connection(hostname);

		/* Now connect and use the SimpleVerifier */

		conn.connect(new SimpleVerifier(database));

		/* Authenticate */

		boolean isAuthenticated = conn.authenticateWithPassword(username, password);

		if (isAuthenticated == false)
			throw new IOException("Authentication failed.");

		/* Create a session */

		Session sess = conn.openSession();

		sess.execCommand("uname -a && date && uptime && who");

		InputStream stdout = new StreamGobbler(sess.getStdout());
		BufferedReader br = new BufferedReader(new InputStreamReader(stdout));

		System.out.println("Here is some information about the remote host:");

		while (true)
		{
			String line = br.readLine();
			if (line == null)
				break;
			System.out.println(line);
		}

		/* Close this session */

		sess.close();

		/* Close the connection */

		conn.close();

	}
	catch (IOException e)
	{
		e.printStackTrace(System.err);
		System.exit(2);
	}
}
 
开发者ID:dreajay,项目名称:jcode,代码行数:65,代码来源:UsingKnownHosts.java


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