Run jar with root privileges on mac os x with one click

I implemented a small java sniffer tool with jpcap . So far, his work has been excellent, but root privileges are required to access network devices. In case I export the project to an executable jar, I can run it using sudo in the terminal:

 $ sudo java -jar littleSniffer.jar 

Does anyone know a โ€œone-clickโ€ solution to run my runnable jar with root privileges. I want to give this tool to my friends, and it would be nice if they could run it without using the terminal. Maybe using the automator app?

+6
source share
2 answers

Try something like this:

If you are happy with the terminal, just create a new file and add something like this:

 #!/bin/sh sudo java -jar littleSniffer.jar 

and save the file as blah.command (the .command extension launches the sh file in the terminal with a double click).

If you need a little better, you can use the osascript command as follows:

 #!/bin/sh osascript -e "do shell script \"java -jar littleSniffer.jar\" with administrator privileges" 

What will use the GUI request for the root password.

NOTE. If your .command file is not running, you may need to:

 chmod +x blah.command 

to make it doable.

+7
source

Try embedding the JAR in a shell script using uuencode / uudecode. Create a shell script called littleSniffer.command with the following lines in it:

 #!/bin/bash rm -f littleSniffer.jar uudecode $0 sudo java -jar littleSniffer.jar rm -f littleSniffer.jar exit 

Then run uuencode -m littleSniffer.jar littleSniffer.jar >> littleSniffer.command to insert the JAR inside the script.

0
source

Source: https://habr.com/ru/post/951507/


All Articles