前段时间比较无聊,一下子入手了很多台VPS,现在突然觉得管理维护起来好麻烦,作为程序猿,麻烦的事情相信程序能搞定,SO,简单调查了下。
主要的实现,当然是OpenSource的 http://www.jcraft.com/jsch/
这套工具非常强大,差异于我居然以前使用过他的JZlib
源代码可以直接下载
http://sourceforge.net/projects/jsch/files/jsch/0.1.54/jsch-0.1.54.zip/download
我这边的调用代码
public String remoteDskInfo(String strUser, String strPass, String strHost, int intPort)
throws JSchException, IOException {
String strResult = "";
JSch js = new JSch();
BufferedReader reader;
String line;
Session s = js.getSession(strUser, strHost, intPort);
s.setPassword(strPass);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
s.setConfig(config);
s.connect();
Channel c = s.openChannel("exec");
ChannelExec ce;
ce = (ChannelExec) c;
ce.setCommand("df -h");
ce.setErrStream(System.err);
ce.connect();
//Output
reader = new BufferedReader(new InputStreamReader(ce.getInputStream()));
while ((line = reader.readLine()) != null) {
strResult = strResult + line + "\r\n";
}
ce.disconnect();
s.disconnect();
//System.out.println("Exit code: " + ce.getExitStatus());
return strResult;
}
调用执行的结果
total used free shared buff/cache available
Mem: 524288 139036 200884 33684 184368 247868
Swap: 524288 204440 319848
Filesystem Size Used Avail Use% Mounted on
/dev/ploop19812p1 9.8G 2.4G 6.9G 26% /
devtmpfs 256M 0 256M 0% /dev
tmpfs 256M 0 256M 0% /dev/shm
tmpfs 256M 132K 256M 1% /run
tmpfs 256M 0 256M 0% /sys/fs/cgroup
tmpfs 52M 0 52M 0% /run/user/0