这是我的代码。我正在尝试通过SSH连接到服务器列表,并使用' df'来获取所有服务器的磁盘空间信息。命令。
import com.jcraft.jsch.*;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class SSHClient {
public static void main(String[] args) throws JSchException, IOException, InterruptedException{
for(String arg : args){
getDiskSpace(arg);
}
}
public static void getDiskSpace(String host) throws JSchException, IOException, InterruptedException {
System.out.println(host);
String endLineStr = ">";
String user = "";
String password = "";
int port = 22;
JSch shell = new JSch();
// get a new session
Session session = shell.getSession(user, host, port);
// set user password and connect to a channel
session.setUserInfo(new SSHUserInfo(password));
session.setConfig("PreferredAuthentications",
"publickey,keyboard-interactive,password");
session.connect();
Channel channel = session.openChannel("shell");
channel.connect();
DataInputStream dataIn = new DataInputStream(channel.getInputStream());
DataOutputStream dataOut = new DataOutputStream(channel.getOutputStream());
while(dataIn.readLine().length()!=1)
// send dk command to the server
dataOut.writeBytes("df -k\r\n");
dataOut.flush();
// and print the response
dataIn.readLine();
String line = dataIn.readLine();
while(!line.endsWith(endLineStr) && !line.endsWith(endLineStr+(Character.toString((char) 0)))) {
System.out.println(line);
line = dataIn.readLine();
}
dataIn.close();
dataOut.close();
channel.disconnect();
session.disconnect();
System.out.println("_______________________________________________________________________\n\n\n");
}
// this class implements jsch UserInfo interface for passing password to the session
static class SSHUserInfo implements UserInfo {
private String password;
SSHUserInfo(String password) {
this.password = password;
}
public String getPassphrase() {
return null;
}
public String getPassword() {
return password;
}
public boolean promptPassword(String arg0) {
return true;
}
public boolean promptPassphrase(String arg0) {
return true;
}
public boolean promptYesNo(String arg0) {
return true;
}
public void showMessage(String arg0) {
System.out.println(arg0);
}
}
}
但是其中一个服务器不显示该信息。相反,它输出命令很多次(就像任何命令而不仅仅是df一样):
<SERVER NAME>
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
df -k
所有其他服务器的输出正如您所期望的那样:
<SERVER NAME>
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/zeus-root
322M 133M 173M 44% /
tmpfs 943M 0 943M 0% /lib/init/rw
udev 938M 160K 938M 1% /dev
tmpfs 943M 0 943M 0% /dev/shm
/dev/sda1 228M 16M 201M 8% /boot
/dev/mapper/zeus-home
58G 191M 55G 1% /home
/dev/mapper/zeus-tmp 368M 11M 339M 3% /tmp
/dev/mapper/zeus-usr 8.3G 544M 7.3G 7% /usr
/dev/mapper/zeus-var 2.8G 349M 2.3G 14% /var
/dev/sdb1 151G 97G 47G 68% /storage
无法正常运行的服务器与其他服务器之间的唯一区别是服务器正在运行Solaris 10操作系统。
关于如何解决此问题的任何想法?感谢。