java链接linux服务器,命令操作
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服务器,命令操作的更多相关文章
- linux学习(2):linux服务器常用操作和命令
linux服务器常用操作和命令 目录 1. 什么是linux服务器load average?2. 如何查看linux服务器负载3. 服务器负载高怎么办?4. 如何查看服务器内存使用率?5. 如何查看单 ...
- Go连接到Linux服务器进行操作-执行shell命令&&上传文件
Go连接到Linux服务器进行操作 使用密码连接Linux服务器 package main import ( "fmt" "golang.org/x/crypto/ssh ...
- Linux服务器命令行模式安装Matlab2014a
Linux服务器命令行模式安装Matlab2014a,有需要的朋友可以参考下. 0.下载安装包 下载Matlab2014a for Linux安装包的ISO镜像文件(感谢万能的度娘)以及破解包(下载地 ...
- mac电脑iTerm2链接linux服务器断线解决方案
问题 用windows链接linux服务器,方式很多,我最经常用的是xshell.但是xshell没有mac版,那用mac电脑,比较好的命令行软件是什么呢?我用的是iTerm2 ,这个功能确实蛮强大, ...
- linux的命令操作
linux的命令操作 1.日常操作命令 **查看当前所在的工作目录pwd **查看当前系统的时间 date **查看有谁在线(哪些人登陆到了服务器)who 查看当前在线last 查看最近的登陆历史记录 ...
- linux服务器常用操作和命令
1. 什么是linux服务器load average? Load是用来度量服务器工作量的大小,即计算机cpu任务执行队列的长度,值越大,表明包括正在运行和待运行的进程数越多.参考资料:http://e ...
- 在windows上通过ssh远程链接linux服务器[转]
本文分别转自 [http://jingyan.baidu.com/article/6d704a130de40e28db51cab5.html] [http://www.cnblogs.com/mliu ...
- Linux服务器命令大全
快捷提示键: table 查看文件夹: ls , ls –all ,ls –l,ll 进入某个文件夹: cd usr/local 回到root 目录 : cd /root/ 回到根目录:cd / 回 ...
- 常用Linux 服务器命令--各种性能指标命令
如果你想知道你的服务器正在做干什么,你就需要了解一些基本的命令,一旦你精通了这些命令,那你就是一个专业的 Linux 系统管理员. 监控命令## iostat### iostat命令用来显示存储系统的 ...
随机推荐
- 利用keepalive和timeout来推断死连接
问题是这样出现的. 操作:client正在向服务端请求数据的时候,突然拔掉client的网线. 现象:client死等.服务端socket一直存在. 在网上搜索后,须要设置KEEPALIVE属性. 于 ...
- 【LeetCode OJ 232】Implement Queue using Stacks
题目链接:https://leetcode.com/problems/implement-queue-using-stacks/ 题目:Implement the following operatio ...
- ASP原码加密工具介绍
ASP原码加密工具介绍 总是会有非常多方法暴露ASP的原程序.造成数据库的password 路径都能够轻易被其它人搞到,所以对ASP程序实行加密处理是个不错的解决方法.以下来介绍一个工具假设大家感兴趣 ...
- ios添加麦克风访问权限
不然程序崩溃: This app has crashed because it attempted to access privacy-sensitive data without a usage d ...
- Advapi32.dll 函数接口说明
Advapi32.dll 函数接口说明 函数原型 说明 AbortSystemShutDown ...
- 通过命令行升级git for windows
git update-git-for-windows 配置了正确的代理,就可以通过命令行直接升级.最好是可以访问谷歌的代理,否则国内的网络通过命令行升级,下载到一半,就会失败.
- poj 1061(扩展欧几里得定理求不定方程)
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...
- C Tricks(十六)—— 复制字符串
while (*s++ = *t++); // target ⇒ source // 对于 C 语言而言,赋值运算符返回左值
- bzoj 3231 [ Sdoi 2008 ] 递归数列 —— 矩阵乘法
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=3231 裸矩阵乘法. 代码如下: #include<iostream> #incl ...
- php 关于使用七牛云存储
1.首先注册七牛云存储账号 http://www.qiniu.com/ 2.获得密钥 3.仔细查看文档 http://developer.qiniu.com/docs/v6/sdk/php-sdk.h ...