Windows系统下如何使用Java代码操作Linux
Windows系统下如何使用Java代码操作Linux?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!
创新互联公司是一家集网站建设,芗城企业网站建设,芗城品牌网站建设,网站定制,芗城网站建设报价,网络营销,网络优化,芗城网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。
一、场景描述:
主项目(Web)部署在Windows下,算法项目(TensorFlow)部署在Linux环境下。
二、依赖环境(Jar)
ch.ethz.ganymed ganymed-ssh3 build210 sshtools j2ssh-core 0.2.9 commons-io commons-io 2.4
三、后端代码
package cn.virgo.audio.utils; import ch.ethz.ssh3.ChannelCondition; import ch.ethz.ssh3.Connection; import ch.ethz.ssh3.Session; import ch.ethz.ssh3.StreamGobbler; import com.sshtools.j2ssh.SshClient; import com.sshtools.j2ssh.authentication.AuthenticationProtocolState; import com.sshtools.j2ssh.authentication.PasswordAuthenticationClient; import com.sshtools.j2ssh.sftp.SftpFile; import org.apache.commons.io.IOUtils; import java.io.*; import java.nio.charset.Charset; import java.util.ArrayList; import java.util.List; public class RemoteShellExecutor { private Connection conn; private String ip; private String userName; private String password; private String charset = Charset.defaultCharset().toString(); private static final int TIME_OUT = 1000 * 5 * 60; /** * 构造函数 * * @param ip * @param userName * @param password */ public RemoteShellExecutor(String ip, String userName, String password) { this.ip = ip; this.userName = userName; this.password = password; } /** * 链接远程桌面 * * @return * @throws IOException */ private boolean login() throws IOException { conn = new ch.ethz.ssh3.Connection(ip); conn.connect(); return conn.authenticateWithPassword(userName, password); } /** * 执行shell * * @param cmds * @return * @throws Exception */ public int exec(String cmds) throws Exception { InputStream stdOut = null; InputStream stdErr = null; int ret = -1; try { if (login()) { Session session = conn.openSession(); session.execCommand(cmds); stdOut = new StreamGobbler(session.getStdout()); processStream(stdOut, charset); stdErr = new StreamGobbler(session.getStderr()); processStream(stdErr, charset); session.waitForCondition(ChannelCondition.EXIT_STATUS, TIME_OUT); ret = session.getExitStatus(); } else { throw new Exception("远程链接失败:" + ip); } } catch (Exception e) { e.printStackTrace(); } finally { if (conn != null) { conn.close(); } IOUtils.closeQuietly(stdOut); IOUtils.closeQuietly(stdErr); } return ret; } /** * 获取执行过程输出 * * @param in * @param charset * @return * @throws IOException */ private void processStream(InputStream in, String charset) throws IOException { byte[] buf = new byte[1024]; while (in.read(buf) != -1) { System.out.println(new String(buf, charset)); } } /** * 获取Linux下某个文件数据,将其拷贝到本地tmpPath下 */ public ListgetCaleResByFileFromSSH(String filePath, String filename, String tmpPath) { List resList = new ArrayList<>(); SshClient client = new SshClient(); try { client.connect(this.ip); //设置用户名和密码 PasswordAuthenticationClient pwd = new PasswordAuthenticationClient(); pwd.setUsername(this.userName); pwd.setPassword(this.password); int result = client.authenticate(pwd); if (result == AuthenticationProtocolState.COMPLETE) {//如果连接完成 List list = client.openSftpClient().ls(filePath); for (SftpFile f : list) { if (f.getFilename().equals(filename)) { OutputStream os = new FileOutputStream(tmpPath + f.getFilename()); client.openSftpClient().get(f.getAbsolutePath(), os); //以行为单位读取文件start File file = new File(tmpPath + f.getFilename()); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(file)); String tempString = null; int line = 1;//行号 //一次读入一行,直到读入null为文件结束 while ((tempString = reader.readLine()) != null) { //显示行号 System.out.println("line " + line + ": " + tempString); resList.add(tempString); line++; } reader.close(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e1) { } } } //以行为单位读取文件end } } } } catch (IOException e) { e.printStackTrace(); } return resList; } }
感谢各位的阅读!看完上述内容,你们对Windows系统下如何使用Java代码操作Linux大概了解了吗?希望文章内容对大家有所帮助。如果想了解更多相关文章内容,欢迎关注创新互联行业资讯频道。
网页标题:Windows系统下如何使用Java代码操作Linux
链接URL:http://azwzsj.com/article/gecpoe.html