文档章节

SSH

Pig-man
 Pig-man
发布于 2017/09/12 09:37
字数 383
阅读 15
收藏 0

远程执行Linux命令,依赖ganymed-ssh2.jar

public class SSHUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(SSHUtil.class);
	
	public static String execCommand(String host, int port, String username, String password, String command) {
		String result = "";
		Connection conn = null;
		Session ssh = null;
		try {
			//conn
			conn = new Connection(host, port);
			conn.connect();
			logger.info("Connecting to "+host+":"+port+"...");
			boolean authenticated = conn.authenticateWithPassword(username, password);
			if(!authenticated) {
				logger.info("Username or password is wrong");
			} else {
				logger.info("Connected");
				//open session
				ssh = conn.openSession();
				//exec command
				logger.info("Command start");
				logger.info("Command " + command);
				ssh.execCommand(command);
				InputStream  is = new StreamGobbler(ssh.getStdout());
				BufferedReader br = new BufferedReader(new InputStreamReader(is));
				while(true){
					String line = br.readLine();
					if(line==null){
						break;
					}
					result+= new String(line.getBytes("GBK"),"UTF-8")+"\r\n";
				}
				logger.info("Command end");
				br.close();
			}
		} catch (ConnectException e) {
			e.printStackTrace();
			logger.error("Connection refused", e);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Exec command error", e);
		} finally {
			if(ssh!=null) {
				ssh.close();
			}
			if(conn!=null) {
				conn.close();
			}
		}
		return result;
	}
	
	public static void main(String[] args) {
		String host = "192.168.3.9";
		int port = 22;
		String username = "root";
		String password = "redhat";
		String command = "uname -s -r -v";
		String result = execCommand(host, port, username, password, command);
		System.out.println("result="+result);
	}

}
---------------------------------------------------------------------------------------------------------

远程执行Linux命令,依赖jsch.jar(JSch=Java Secure Channel)

public class SSHJSchUtil {
	
	private static final Logger logger = LoggerFactory.getLogger(SSHJSchUtil.class);
	
	public static String execCommand(String host, int port, String username,String password, String command) {
		String result = "";
		Session session = null;
		ChannelExec openChannel =null;
		try {
			//Session
			logger.debug("Connecting to "+host+":"+port+"...");
			JSch jsch=new JSch();
			session = jsch.getSession(username, host, port);
			session.setPassword(password);
			session.setTimeout(2000);
			Properties config = new Properties ();
			config.put("StrictHostKeyChecking", "no");
			session.setConfig(config);
			session.connect();
			logger.debug("Connected");
			//Channel
			logger.debug("Command start");
			logger.debug("Command " + command);
			openChannel = (ChannelExec) session.openChannel("exec");
			openChannel.setCommand(command);
			//int exitStatus = openChannel.getExitStatus();
			openChannel.connect();
			InputStream in = openChannel.getInputStream(); 
			BufferedReader br = new BufferedReader(new InputStreamReader(in));
			while(true){
				String line = br.readLine();
				if(line==null){
					break;
				}
				result+= new String(line.getBytes("GBK"),"UTF-8")+"\r\n";
			}
			br.close();
			logger.debug("Command end");
		} catch (JSchException e) {
			e.printStackTrace();
			logger.error("Connection refused", e);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("Exec command error", e);
		} finally {
			if(openChannel!=null&&!openChannel.isClosed()){
				openChannel.disconnect();
			}
			if(session!=null&&session.isConnected()){
				session.disconnect();
			}
		}
		return result;
	}
	
	public static void main(String[] args) {
		String host = "192.168.3.9";
		int port = 22;
		String username = "root";
		String password = "redhat";
		String command = "uname -s -r -v";
		String result = execCommand(host, port, username, password, command);
		System.out.println("result="+result);
	}

}

 

 

© 著作权归作者所有

共有 人打赏支持
Pig-man
粉丝 19
博文 62
码字总数 47800
作品 0
青浦
程序员

暂无文章

正弦 余弦 角度 用于画时钟

<html> <head> <title>时钟</title> </head> <style> #canvas{ background: #1977ca } </style>......

一箭落旄头
31分钟前
2
0
drupal7自定义模块之表单(新手向)

前段时间要给网站做个高级搜索的功能,但drupal7自带的搜索远远无法满足需求,便有了自己开发的需求 我以前没有接触过drupal,做这个功能也是困难重重,几乎是从零开始,我将目光放到了自定义模块...

gcudwork
36分钟前
1
0
驰狼课堂

http://www.chilangedu.com/

求是科技
54分钟前
0
0
jumpserver 报错"Incorrect string value

申明 本文所有内容参考自jumpserver记录命令无法入库问题 #1773 简介 jumpserver 1.4.0在jumpserver.log中大量报错,错误日志 File "/opt/jumpserver/apps/terminal/api.py", line 246, i...

zhnxin
今天
2
0
用户管理相关配置文件及命令

9月19日任务 2.27linux和windows互传文件 3.1 用户配置文件和密码配置文件 3.2 用户组管理 3.3 用户管理 扩展知识 实用小工具 简单命令行下实现Linux/Windows文件互传 前提:使用远程工具Xsh...

robertt15
今天
0
0

没有更多内容

加载失败,请刷新页面

加载更多

返回顶部
顶部