本篇內(nèi)容介紹了“怎么在java中執(zhí)行sudo 命令”的有關(guān)知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠?qū)W有所成!
讓客戶滿意是我們工作的目標,不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:國際域名空間、網(wǎng)絡(luò)空間、營銷軟件、網(wǎng)站建設(shè)、藤縣網(wǎng)站維護、網(wǎng)站推廣。
* 1. 修改/etc/sudoers文件,在其中添加一行: * username ALL=(ALL) NOPASSWD:ALL * 其中,"username"是需要運行這個程序的用戶名。
/bin/bash -c echo password |sudo -S command
Shell(JSCH)工具類:
package cn.bywin.cbvsp.common.utils; import cn.bywin.cbvsp.DAL.BO.po.MyUserInfo; import com.jcraft.jsch.*; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; /** * @author linbin * @create 2019-03-19 10:06 */ public class Shell { //遠程主機的ip地址 private String ip; //遠程主機登錄用戶名 private String username; //遠程主機的登錄密碼 private String password; //設(shè)置ssh連接的遠程端口 public static final int DEFAULT_SSH_PORT = 22; //保存輸出內(nèi)容的容器 private ArrayListstdout; public Shell() { } /** * 初始化登錄信息 * * @param ip * @param username * @param password */ public Shell(final String ip, final String username, final String password) { this.ip = ip; this.username = username; this.password = password; stdout = new ArrayList (); } /** * 執(zhí)行shell命令 * * @param command * @return */ //原本是 execute(final String command) public int execute(String command) { int returnCode = 0; JSch jsch = new JSch(); MyUserInfo userInfo = new MyUserInfo(); try { //創(chuàng)建session并且打開連接,因為創(chuàng)建session之后要主動打開連接 Session session = jsch.getSession(username, ip, DEFAULT_SSH_PORT); session.setPassword(password); session.setUserInfo(userInfo); session.connect(); //打開通道,設(shè)置通道類型,和執(zhí)行的命令 Channel channel = session.openChannel("exec"); ChannelExec channelExec = (ChannelExec) channel; channelExec.setCommand(command); channelExec.setInputStream(null); BufferedReader input = new BufferedReader(new InputStreamReader (channelExec.getInputStream())); channelExec.connect(); System.out.println("The remote command is :" + command); //接收遠程服務(wù)器執(zhí)行命令的結(jié)果 String line; while ((line = input.readLine()) != null) { stdout.add(line); } input.close(); // 得到returnCode if (channelExec.isClosed()) { returnCode = channelExec.getExitStatus(); // 關(guān)閉通道 channelExec.disconnect(); //關(guān)閉session session.disconnect(); } } catch (JSchException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); } return returnCode; } public static ChannelExec getCommandResult(Session session, String command) throws JSchException, IOException { ChannelExec channelExec = (ChannelExec) session.openChannel("exec"); channelExec.setCommand(command); channelExec.setInputStream(null); channelExec.setErrStream(System.err); channelExec.connect(); /*if (null != session) { session.disconnect(); }*/ return channelExec; } /** * get stdout * * @return */ public ArrayList getStandardOutput() { return stdout; } }
執(zhí)行代碼:
public static void main(String[] args) { Shell shell = new Shell("192.168.161.152","root","linbinbhs7"); shell.execute("/bin/bash -c echo linbinbhs7 |sudo -S ls"); ArrayListgraphicsCardNames = shell.getStandardOutput(); graphicsCardNames.forEach(e->{ System.out.println(e); }); }
刪除指定時間的文件
public class TestShell { public static void main(String[] args) { Shell shell = new Shell("192.168.96.136","developer","de183!!!"); shell.execute("/bin/bash -c echo de183!!! |sudo -S find /home/developer/temp -type f -mmin +1 -exec rm -rfv {} \\; > /home/developer/temp/nohup.out;"); ArrayListgraphicsCardNames = shell.getStandardOutput(); graphicsCardNames.forEach(System.out::println); } }
“怎么在java中執(zhí)行sudo 命令”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實用文章!