1.本地读取linux文件,即在Windows上链接外部linux

package com.common.utils;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List; import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.Session;
import ch.ethz.ssh2.StreamGobbler; public class SSHUtil { private final static String host = "00.000.000.000";  //linux的外网ip
private final static int port = 22;  //linux端口号,xftp链接linux是所用的端口
private final static String username = "user";  //用户账号
private final static String password = "pwd";  //密码private static SSHUtil ftp = new SSHUtil();
private static Connection con = new Connection(host, port); public static List<String> execCom(String command) {
Session session = ftp.session();
BufferedReader br = null;
List<String> msgList=new ArrayList<String>();
try {
session.requestPTY("vt100", 80, 24, 640, 480, null);
session.execCommand(command);
System.out.println("ExitCode: " + session.getExitStatus());
InputStream stdout = new StreamGobbler(session.getStdout());
br = new BufferedReader(new InputStreamReader(stdout));
while (true) {
String line = br.readLine();
System.out.println(line);
if (line == null) {
break;
}
msgList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
session.close();
con.close();
return msgList;
} public Session session() {
Session session = null;
try {
con.connect(); con.authenticateWithPassword(username, password);
session = con.openSession();
} catch (IOException e) {
e.printStackTrace();
return null;
}
return session;
} }

2.linux服务器中的项目读取linux文件

package comcommon.utils;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.List; public class RunTimeUtil {
private static LogUtil logger = new LogUtil(RunTimeUtil.class); public static List<String> execCom(String cmd) throws IOException, InterruptedException {
List<String> msgList=new ArrayList<String>();
try {
Process process = Runtime.getRuntime().exec(new String[]{"/bin/sh", "-c", cmd});//执行命令 InputStreamReader ir = new InputStreamReader(process.getInputStream());
LineNumberReader input = new LineNumberReader(ir); String line;
while ((line = input.readLine()) != null) {//输出结果
msgList.add(line);
}
} catch (java.io.IOException e) {
logger.info("IOException " + e.getMessage());//捕捉异常
}
return msgList;
}
}

然后调取execCom("所需命令");

java链接linux服务器,命令操作的更多相关文章

  1. linux学习(2):linux服务器常用操作和命令

    linux服务器常用操作和命令 目录 1. 什么是linux服务器load average?2. 如何查看linux服务器负载3. 服务器负载高怎么办?4. 如何查看服务器内存使用率?5. 如何查看单 ...

  2. Go连接到Linux服务器进行操作-执行shell命令&&上传文件

    Go连接到Linux服务器进行操作 使用密码连接Linux服务器 package main import ( "fmt" "golang.org/x/crypto/ssh ...

  3. Linux服务器命令行模式安装Matlab2014a

    Linux服务器命令行模式安装Matlab2014a,有需要的朋友可以参考下. 0.下载安装包 下载Matlab2014a for Linux安装包的ISO镜像文件(感谢万能的度娘)以及破解包(下载地 ...

  4. mac电脑iTerm2链接linux服务器断线解决方案

    问题 用windows链接linux服务器,方式很多,我最经常用的是xshell.但是xshell没有mac版,那用mac电脑,比较好的命令行软件是什么呢?我用的是iTerm2 ,这个功能确实蛮强大, ...

  5. linux的命令操作

    linux的命令操作 1.日常操作命令 **查看当前所在的工作目录pwd **查看当前系统的时间 date **查看有谁在线(哪些人登陆到了服务器)who 查看当前在线last 查看最近的登陆历史记录 ...

  6. linux服务器常用操作和命令

    1. 什么是linux服务器load average? Load是用来度量服务器工作量的大小,即计算机cpu任务执行队列的长度,值越大,表明包括正在运行和待运行的进程数越多.参考资料:http://e ...

  7. 在windows上通过ssh远程链接linux服务器[转]

    本文分别转自 [http://jingyan.baidu.com/article/6d704a130de40e28db51cab5.html] [http://www.cnblogs.com/mliu ...

  8. Linux服务器命令大全

    快捷提示键: table 查看文件夹:  ls , ls –all ,ls –l,ll 进入某个文件夹: cd usr/local 回到root 目录 : cd /root/ 回到根目录:cd / 回 ...

  9. 常用Linux 服务器命令--各种性能指标命令

    如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个专业的 Linux 系统管理员. 监控命令## iostat### iostat命令用来显示存储系统的 ...

随机推荐

  1. volley基本使用方法

    用volley訪问server数据,不用自己额外开线程.以下样例为訪问JSONObject类型的数据,详细使用方法看代码: 首先得有volley的jar包,假设自己没有.去github上下载,然后自己 ...

  2. (七)CAS 本地localhost调试,无法单点退出疑问

            干活的时候要多思考-------- 题记          昨天下午接到任务把内容汇聚平台和分发平台加上统一认证登录,之前弄过CAS,想想这个过程也就是按部就班的事情.在两个系统中we ...

  3. 【Discuz】去除版权信息,标题栏与底部改动

    这篇文章尽管是介绍怎么把Discuz!的版权信息怎么搞得无影无踪,可是还是建议在不影响论坛视觉效果的情况下,保留Discuz的版权信息,毕竟它为我奉献了一个这么出色的开源论坛的phpproject.主 ...

  4. Java-MyBatis:MyBatis

    ylbtech-Java-MyBatis:MyBatis 1.返回顶部 1. MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foun ...

  5. leetcode矩阵与动态规划相关

    目录 54/59螺旋矩阵 62不同路径 64最小路径和 120三角形最小路径和 695岛屿的最大面积 547朋友圈 718最长重复数组 221最大正方形 121/122/123/714/188买卖股票 ...

  6. nodejs安装express

    最近在看<Node.js开发指南>,看到使用nodejs进行web开发的时候,准备创建ejs项目遇到问题了, 书上命令为: 1 express -t ejs microblog 可是执行后 ...

  7. layui富文本编译器后台获取图片路径

    @RequestMapping("add")    public ModelAndView add(News news){        ModelAndView mav = ne ...

  8. android service--delphixe 10.3

    开发中的陷阱: 1. 别放什么 *.wav文件,这个 服务窗口不能随便放东西,不然铁定出现意想不到的结果,比如 无法运行,因为没 ui界面,随意都不知是啥问题. 2. 不能加载 datamodule ...

  9. WinSocket简单聊天程序客户端

    #pragma comment(lib,"Ws2_32.lib") #include <stdio.h> #include <Winsock2.h> SOC ...

  10. python 7:del 列表指定元素、list.pop(索引)、list.remove(元素值)(删除列表指定元素,且不可再使用;默认索引-1,弹出指定列表元素,可再使用;移除列表指定第一个元素)

    bicycles = ['trek', 'cannondale', 'redline', 'specialized'] print(bicycles) del bicycles[0] #删除指定列表元 ...