package run.runnable.XShell;
import ch.ethz.ssh2.*;
import javax.swing.*;
import java.io.*;
public class ConnectLinuxCommand {
private static String DEFAULTCHARTSET = "UTF-8";
private static Connection conn;
public static Boolean login(RemoteConnect remoteConnect) {
boolean flag = false;
try {
conn = new Connection(remoteConnect.getIp());
conn.connect();
flag = conn.authenticateWithPassword(remoteConnect.getUserName(), remoteConnect.getPassword());
if (flag) {
System.out.println("认证成功!");
} else {
System.out.println("认证失败!");
conn.close();
}
} catch (IOException e) {
e.printStackTrace();
}
return flag;
}
public static void main(String[] args) {
RemoteConnect remoteConnect = new RemoteConnect("","root","");
Boolean login = login(remoteConnect);
if (login){
System.out.println("登录成功");
}
execute("mkdir /etc/fffffffffff");
}
public static Boolean loginByFileKey(RemoteConnect remoteConnect, File keyFile, String keyfilePass) {
boolean flag = false;
try {
conn = new Connection(remoteConnect.getIp());
conn.connect();
flag = conn.authenticateWithPublicKey(remoteConnect.getUserName(), keyFile, keyfilePass);
if (flag) {
System.out.println("认证成功!");
} else {
System.out.println("认证失败!");
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static Boolean loginByCharsKey(RemoteConnect remoteConnect, char[] keys, String keyPass) {
boolean flag = false;
try {
conn = new Connection(remoteConnect.getIp());
conn.connect();
flag = conn.authenticateWithPublicKey(remoteConnect.getUserName(), keys, keyPass);
if (flag) {
System.out.println("认证成功!");
} else {
System.out.println("认证失败!");
conn.close();
}
} catch (Exception e) {
e.printStackTrace();
}
return flag;
}
public static String execute(String cmd){
String result = "";
try {
Session session = conn.openSession();
session.execCommand(cmd);
result = processStdout(session.getStdout(), DEFAULTCHARTSET);
if (result==null||result.equalsIgnoreCase("")) {
result = processStdout(session.getStderr(), DEFAULTCHARTSET);
}
conn.close();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String executeSuccess(String cmd){
String result = "";
try {
Session session = conn.openSession();
session.execCommand(cmd);
result = processStdout(session.getStdout(), DEFAULTCHARTSET);
conn.close();
session.close();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
public static String processStdout(InputStream in, String charset){
InputStream stdout = new StreamGobbler(in);
StringBuffer buffer = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new InputStreamReader(stdout, charset));
String line = null;
while ((line = br.readLine()) != null) {
buffer.append(line + "\n");
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return buffer.toString();
}
public static boolean connectLinux(String ip,String userName,String password,String commandStr) {
System.out.println("ConnectLinuxCommand scpGet===" + "ip:" + ip + " userName:" + userName + " commandStr:"
+ commandStr);
String returnStr = "";
boolean result = true;
RemoteConnect remoteConnect = new RemoteConnect();
remoteConnect.setIp(ip);
remoteConnect.setUserName(userName);
remoteConnect.setPassword(password);
try {
if (login(remoteConnect)) {
returnStr = execute(commandStr);
System.out.println(result);
}
} catch (Exception e) {
e.printStackTrace();
}
if (returnStr==null||returnStr.equals("")) {
result = false;
}
return result;
}
public static void scpGet(String ip, String userName, String password, String remoteFile, String localDir)
throws IOException {
System.out.println("ConnectLinuxCommand scpGet===" + "ip:" + ip + " userName:" + userName + " remoteFile:"
+ remoteFile + " localDir:" + localDir);
RemoteConnect remoteConnect = new RemoteConnect();
remoteConnect.setIp(ip);
remoteConnect.setUserName(userName);
remoteConnect.setPassword(password);
if (login(remoteConnect)) {
SCPClient client = new SCPClient(conn);
client.get(remoteFile);
conn.close();
}
}
public static void scpPut(String ip, String userName, String password, String localFile, String remoteDir)
throws IOException {
System.out.println("ConnectLinuxCommand scpPut===" + "ip:" + ip + " userName:" + userName + " localFile:"
+ localFile + " remoteDir:" + remoteDir);
RemoteConnect remoteConnect = new RemoteConnect();
remoteConnect.setIp(ip);
remoteConnect.setUserName(userName);
remoteConnect.setPassword(password);
if (login(remoteConnect)) {
SCPClient client = new SCPClient(conn);
conn.close();
}
}
}