10

I implemented a small java sniffer-tool with jpcap. So far its functioning fine, but it needs root privileges to get access to the network-devices. In case of I export my project to a runnable jar, I can run it by using sudo in terminal:

$ sudo java -jar littleSniffer.jar 

Does anyone knows a "one click"-solution to run my runnable jar with root privileges. I want to give that tool to my workmates, and it would be nice I they could start it without using the terminal. Maybe by using the automator app?

Pro Backup
  • 729
  • 14
  • 34
Philipp Li
  • 499
  • 6
  • 22

2 Answers2

9

Try something like this:

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

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

and save the file as blah.command (the .command extension runs the sh file in terminal on double click).

If you would like something a bit nicer, you could use the osascript command like this:

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

Which will use a GUI request for the root password.

NOTE: If your .command file isn't running, you may need to:

chmod +x blah.command

to make it runnable.

Ed M
  • 155
  • 1
  • 5
  • Looks good. The osascript approach is described in the answer as http://stackoverflow.com/a/2714651/274350 and there are some other good suggestions on that question as well. – Richard Neish Jan 16 '15 at 10:38
0

Try embedding the JAR into a shell script by 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.commandin order to embed the JAR inside the script.

Mitsos101
  • 551
  • 3
  • 15
  • 1
    Probably it is just me, but your script just does not make sense. Could you elaborate what it does? Thanks. – Gábor Bakos Jan 16 '15 at 14:58
  • 1
    @GáborBakos It removes any previous JAR files (for example if the script was interrupted), decodes itself (uudecode doesn't decode the entire script, there are 'begin' and 'end' marks), runs the JAR file, and then removes it. – Mitsos101 Jan 16 '15 at 15:39