SSH
博客专区 > Pig-man 的博客 > 博客详情
SSH
Pig-man 发表于2个月前
SSH
  • 发表于 2个月前
  • 阅读 7
  • 收藏 0
  • 点赞 1
  • 评论 0
摘要: 远程执行Linux命令

远程执行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);
	}

}

 

 

标签: SSH jsch ganymed-ssh2
共有 人打赏支持
粉丝 19
博文 57
码字总数 43860
×
Pig-man
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: