Windows系统下如何使用Java代码操作Linux ?这个问题可能是我们日常学习或工作经常见到的。希望通过这个问题能让你收获颇深。下面是小编给大家带来的参考内容,让我们一起来看看吧!
一、场景描述:
主项目(Web)部署在Windows下,算法项目(TensorFlow)部署在Linux环境下。
二、依赖环境(Jar)
& lt; !——Java SSH插件——比; & lt; dependency> & lt; groupId> ch.ethz.ganymed & lt; artifactId> ganymed-ssh3 & lt; version> build210 & lt;/dependency> & lt; dependency> & lt; groupId> sshtools & lt; artifactId> j2ssh-core & lt; version> 0.2.9 & lt;/dependency> & lt; dependency> & lt; groupId> commons-io & lt; artifactId> commons-io & lt; version> 2.4 & lt;/version> & lt;/dependency>
三,后端代码
包cn.virgo.audio.utils; 进口ch.ethz.ssh3.ChannelCondition; 进口ch.ethz.ssh3.Connection; 进口ch.ethz.ssh3.Session; 进口ch.ethz.ssh3.StreamGobbler; 进口com.sshtools.j2ssh.SshClient; 进口com.sshtools.j2ssh.authentication.AuthenticationProtocolState; 进口com.sshtools.j2ssh.authentication.PasswordAuthenticationClient; 进口com.sshtools.j2ssh.sftp.SftpFile; 进口org.apache.commons.io.IOUtils; 进口. io . *; 进口charset; 进口java.util.ArrayList; 进口并不知道; 公开课RemoteShellExecutor { 私人康涅狄格州连接; 私人字符串ip; 私人字符串的用户名; 私人密码字符串; .toString私人字符串charset=Charset.defaultCharset () (); 私有静态最终int TIME_OUT=1000 * 5 * 60;/* * *构造函数 * * @param ip * @param用户名 * @param密码 */公共RemoteShellExecutor (ip的字符串,字符串的用户名,字符串密码){ 这一点。ip=ip; 这一点。用户名=用户名; 这一点。密码=密码; }/* * *链接远程桌面 * * @return * @throws IOException */私人布尔登录()抛出IOException { 康涅狄格州=new ch.ethz.ssh3.Connection (ip); conn.connect (); 返回conn.authenticateWithPassword(用户名、密码); }/* * *执行壳 * * @param cmds * @return * @throws例外 */公共int exec (String cmds){抛出异常 InputStream stdOut=零; InputStream stdErr=零; int ret=1; 尝试{ 如果(登录()){ 会议会话=conn.openSession (); session.execCommand (cmds); stdOut=new StreamGobbler (session.getStdout ()); processStream (stdOut,字符集); stdErr=new StreamGobbler (session.getStderr ()); processStream (stdErr,字符集); session.waitForCondition (ChannelCondition。EXIT_STATUS TIME_OUT); ret=session.getExitStatus (); 其他}{ 抛出新的异常(“远程链接失败:“+ ip); } }捕捉(异常e) { e.printStackTrace (); 最后}{ 如果(康涅狄格州!=null) { conn.close (); } IOUtils.closeQuietly (stdOut); IOUtils.closeQuietly (stdErr); } 返回受潮湿腐烂; }/* * *获取执行过程输出 * * @param * @param字符集 * @return * @throws IOException */私人空间processStream (InputStream,字符串字符集)抛出IOException { byte [] buf=新字节[1024]; 而(in.read (buf) !=1) { system . out。println(新的字符串(buf字符集)); } }/* * *获取Linux下某个文件数据,将其拷贝到本地tmpPath下 */公共List文件名filePath getCaleResByFileFromSSH(字符串,字符串,字符串tmpPath) { List ,resList=new ArrayList<的在(); SshClient客户=new SshClient (); 尝试{ client.connect (this.ip);//设置用户名和密码 PasswordAuthenticationClient pwd=new PasswordAuthenticationClient (); pwd.setUsername (this.userName); pwd.setPassword (this.password); int结果=client.authenticate (pwd); 如果结果==AuthenticationProtocolState.COMPLETE){//如果连接完成 List 列表=client.openSftpClient () .ls (filePath); (SftpFile f:列表){ 如果(f.getFilename () .equals(文件名)){ OutputStream os=new FileOutputStream (tmpPath + f.getFilename ()); client.openSftpClient () . get (f.getAbsolutePath(),操作系统);//以行为单位读取文件开始 文件文件=新文件(tmpPath + f.getFilename ()); BufferedReader读者=零; 尝试{ 读者=new BufferedReader(新FileReader(文件); 字符串tempString=零; int行=1;//行号//一次读入一行,直到读入零为文件结束 在((tempString=reader.readLine ()) !=null) {//显示行号 System.out.println(“行”;+线+“:“;+ tempString); resList.add (tempString); 线+ +; } reader.close (); }捕捉(IOException e) { e.printStackTrace (); 最后}{ 如果(读者!=null) { 尝试{ reader.close (); }捕捉(IOException e1) { } } }//以行为单位读取文件 } } } }捕捉(IOException e) { e.printStackTrace (); } 返回resList; } } Windows系统下如何使用Java代码操作Linux