俄羅斯:將審查iPhone等外國公司設備 保數據安全
iPhone和特斯拉都屬于在各自領域領頭羊的品牌,推出的產品也也都是數一數二的,但對于一些國家而言,它們的產品可靠性和安全性還是在限制范圍內。近日,俄羅斯聯邦通信、信息技術
JSch 是一個純 Java 實現的 SSH2 客戶端庫,它允許 Java 應用程序通過 SSH 協議連接到 SSH 服務器,并執行命令、傳輸文件等。JSch 是基于 SSH-2 協議的一個開源項目,廣泛用于需要遠程執行命令或文件傳輸的 Java 應用程序中。
<dependency> <groupId>com.github.mwiede</groupId> <artifactId>jsch</artifactId> <version>0.2.19</version> </dependency>
public Session getSession(){ if( this.session != null ){ return this.session; } try { jsch.getSession(property.getUsername(), property.getHost(), property.getPort()); session = jsch.getSession(property.getUsername(), property.getHost(), property.getPort()); session.setPassword(property.getPassword()); session.setConfig("StrictHostKeyChecking","no");// 設置第一次登陸的時候提示 session.setConfig("max_input_buffer_size","1024");// Properties sshConfig = new Properties(); sshConfig.put("StrictHostKeyChecking", "no"); session.setConfig(sshConfig); session.connect(); return session; } catch (JSchException e) { throw new RuntimeException(e); } }
public static ChannelSftp getSftp(Session session){ try { Channel channel = session.openChannel("sftp"); channel.connect(); ChannelSftp sftp = (ChannelSftp) channel; sftp.setFilenameEncoding("UTF-8"); return sftp; } catch (Exception e) { throw new RuntimeException(e); } }
public static ChannelExec getExec(Session session){ try { Channel channel = session.openChannel("exec");// channel.connect(); ChannelExec exec = (ChannelExec) channel; return exec; } catch (Exception e) { throw new RuntimeException(e); } }
public static void execCommand(ChannelExec exec,String command){ try { exec.setCommand(command); InputStream in = exec.getInputStream(); exec.connect(); BufferedReader inputReader = new BufferedReader(new InputStreamReader(in, "UTF8")); String inputLine; while ((inputLine = inputReader.readLine()) != null) { System.out.println(inputLine); } } catch (Exception e) { throw new RuntimeException(e); } finally { exec.disconnect(); } }
public static void fileDownload(ChannelSftp sftp, String path,String dist){ try { InputStream is = sftp.get(path); FileUtils.copyInputStreamToFile(is, FileUtils.getFile(dist,FilenameUtils.getName(path))); is.close(); } catch (SftpException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
public static Session getSession(){ ConnectProperty property = new ConnectProperty(); property.setHost("..."); property.setPort(22); property.setUsername("..."); property.setPassword("..."); ConnectHelper helper = new ConnectHelper(property); return helper.getSession();}
public static void download(Session session){ ChannelSftp sftp = ConnectHelper.getSftp(session); ConnectHelper.fileDownload(sftp,"/home/test/1.txt","E://home//tmp");}
public static void execCommand(Session session){ ChannelExec exec = ConnectHelper.getExec(session); ConnectHelper.execCommand(exec, "pwd");
本文鏈接:http://www.tebozhan.com/showinfo-26-112725-0.html還不會用Java操作遠程服務器?
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com