我需要自动化一些 linux 控制台任务。可以通过基于 ssh 密码的身份验证访问这些框。
我有我的本地机器 (A)。一个 linux 跳转盒 (B)。和一个linux生产盒(C)。框“C”只能从跳转框访问。跳转框和生产框没有 java 或 Expect 模块。所以我选择从我的本地机器“A”进行自动化。
我尝试以下方法通过框“B”在框“C”上执行命令。但它没有用。
spawn.send("ssh " + strDestinUser + "@" + strDestinHost + " \"echo hello world | awk '{print \$1}'\"" + "\n");
spawn.expect("password:");
spawn.send(strDestinPass + "\n");
我的完整代码块如下
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.StringWriter;
import java.security.Security;
import java.util.Properties;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import expectj.ExpectJ;
import expectj.Spawn;
import expectj.TimeoutException;
public class App {
public static void main(String[] args) {
App objApp = new App();
String strJumpHost = "127.234.123.12";
String strJumpUser = "root_jump";
String strJumpPwd = "passforjump";
objApp.sshExecPassword(strJumpHost, strJumpUser, strJumpPwd);
}
public void sshExecPassword(String host, String USERNAME, String PASSWORD) {
App objApp = new App();
int port = 22;
try {
JSch jsch = new JSch();
java.util.Properties config = new java.util.Properties();
config.put("StrictHostKeyChecking", "no");
Session session = jsch.getSession(USERNAME, host, port);
session.setConfig(config);
session.setConfig("PreferredAuthentications", "keyboard-interactive,password"); // Skip
// kbros
// authentication
Security.insertProviderAt(new BouncyCastleProvider(), 1);
session.setPassword(PASSWORD);
session.connect();
Channel channel = session.openChannel("shell");
objApp.expectExec(channel);
session.disconnect();
System.out.println("Script completed");
} catch (Exception e) {
System.out.println("SSH connection failed");
final StringWriter sw = new StringWriter();
final PrintWriter pw = new PrintWriter(sw, true);
e.printStackTrace(pw);
System.out.println(sw.getBuffer().toString());
System.exit(0);
}
}
public void expectExec(Channel channel) {
App objApp = new App();
try {
ExpectJ ex = new ExpectJ(30);
Spawn spawn = ex.spawn(channel);
String strDestinUser = "destin_user";
String strDestinPass = "destin_pass";
String strDestinHost = "127.45.23.12";
spawn.send("cd /var/www/html/poc/" + "\n");
spawn.send("ssh " + strDestinUser + "@" + strDestinHost + "" + "\n");
spawn.expect("password:");
spawn.send(strDestinPass + "\n");
spawn.send("sh /var/www/html/poc/expect.sh" + "\n");
spawn.send("\n");
spawn.send("exit\n");
spawn.send("ssh " + strDestinUser + "@" + strDestinHost + " \"echo hello world | awk '{print \$1}'\"" + "\n");
spawn.expect("password:");
spawn.send(strDestinPass + "\n");
spawn.send("\n");
spawn.send("exit\n");
String lsResults = spawn.getCurrentStandardOutContents();
objApp.writeFile(lsResults);
String[] lsRows = lsResults.split("\r\n");
for (int i = 0; i < lsRows.length; i++) {
System.out.println(i + ":" + lsRows[i]);
}
spawn.send("exit\n");
} catch (IOException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
}
}
public void writeFile(String strContent) {
File fout = new File("out.txt");
FileOutputStream fos = null;
try {
fos = new FileOutputStream(fout);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
try {
bw.write(strContent);
bw.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如何通过jumb box“B”执行linux命令到prod box“C”。以及如何将输出输出到 java 变量?