Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to execute sudo commands in java

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the knowledge of "how to execute sudo commands in java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

* 1. Modify the / etc/sudoers file by adding a line: * username ALL= (ALL) NOPASSWD:ALL * where "username" is the user name under which the program needs to be run. / bin/bash-c echo password | sudo-S command

Shell (JSCH) utility class:

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 {/ / remote host ip address private String ip; / / remote host login user name private String username / / the login password of the remote host private String password; / / sets the remote port of the ssh connection public static final int DEFAULT_SSH_PORT = 22; / / the container private ArrayList stdout that saves the output Public Shell () {} / * * initialize login information * * @ param ip * @ param username * @ param password * / public Shell (final String ip, final String username, final String password) {this.ip = ip; this.username = password; stdout = new ArrayList () } / * execute the shell command * * @ param command * @ return * / / originally execute (final String command) public int execute (String command) {int returnCode = 0; JSch jsch = new JSch (); MyUserInfo userInfo = new MyUserInfo () Try {/ / create the session and open the connection, because after creating the session, you need to actively open the connection Session session = jsch.getSession (username, ip, DEFAULT_SSH_PORT); session.setPassword (password); session.setUserInfo (userInfo); session.connect () / / Open the channel, set the channel type, and execute the command 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); / / receives the result of the command executed by the remote server String line; while ((line = input.readLine ())! = null) {stdout.add (line);} input.close () / / get returnCode if (channelExec.isClosed ()) {returnCode = channelExec.getExitStatus (); / / close channel channelExec.disconnect (); / / close 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;}}

Execute the code:

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"); ArrayList graphicsCardNames = shell.getStandardOutput (); graphicsCardNames.forEach (e-> {System.out.println (e);});}

Delete files at a specified time

Public class TestShell {public static void main (String [] args) {Shell shell = new Shell ("192.168.96.136", "developer", "de183 examples!"); shell.execute ("/ bin/bash-c echo de183 examples! | sudo-S find / home/developer/temp-type f-mmin + 1-exec rm-rfv {}\; > / home/developer/temp/nohup.out;"); ArrayList graphicsCardNames = shell.getStandardOutput () GraphicsCardNames.forEach (System.out::println);}} "how to execute sudo commands in java" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report