[SOLVED] Running sudo command to be executed on dmenu

I'm using "sudo grub-reboot 2". What is the best way to run this command with dmenu, considering that sudo is necessary, without sacrificing security?
Last edited by Quatro (2014-08-19 00:22:24)

My dmenu power menu script looks like this.
power_menu.sh
#!/bin/bash
# dmenu2 power menu
OPT=$(echo -e "suspend\npoweroff\nreboot" | dmenu -f -h 28 -fn "Ubuntu Mono-12" -x 460 -y 370 -w 360 -nb black -nf white -sb white -sf black )
case $OPT in
suspend) sudo systemctl suspend ;;
poweroff) sudo systemctl poweroff ;;
reboot) sudo systemctl reboot ;;
esac
And the following file allows me to run those commands without being prompted for a password.
/etc/sudoers.d/include
slithery ALL=(ALL) NOPASSWD:/usr/bin/systemctl suspend
slithery ALL=(ALL) NOPASSWD:/usr/bin/systemctl poweroff
slithery ALL=(ALL) NOPASSWD:/usr/bin/systemctl reboot
Just use these as an example and you should get what you're after.
Last edited by slithery (2014-08-18 11:52:31)

Similar Messages

  • Applescript for running sudo commands in terminal

    I'm a newbie when it comes to Applescript and was wondering if someone could help with a basic request. I need to write a small script to do the following:
    1. Launch terminal
    2. Run the 'Sudo -s' command
    3. Enter the administrator password (in plain text)
    and then
    4. run some sudo commands like "sudo defaults write /Library/Preferences/com.apple.loginwindow HiddenUsersList -array-add administrator"
    I actually WANT the administrator password to be in the script in plain text even though I understand the security risks (I will literally be the only person to ever see the script). I've trawled forums all over but can't seem to find what I am looking for and cannot get it working by patching together the various commands I have found. Thanks in advance for any help you can give!
    Sean

    I've included two examples.  The preferred way & the hacker way.
    with administrator
    It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
        Author: rccharles
        For testing, run in the Script Editor.
          1) Click on the Event Log tab to see the output from the log statement
          2) Click on Run
        For running shell commands see:
        http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
    on run
        -- Write a message into the event log.
        log "  --- Starting on " & ((current date) as string) & " --- "
        --  debug lines
        set unixDesktopPath to POSIX path of "/System/Library/User Template/"
        log "unixDesktopPath = " & unixDesktopPath
        set quotedUnixDesktopPath to quoted form of unixDesktopPath
        log "quoted form is " & quotedUnixDesktopPath
        try
            set fromUnix to do shell script "sudo ls -l  " & quotedUnixDesktopPath with administrator privileges
            display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
        on error errMsg
            log "ls -l error..." & errMsg
        end try
    end run
    This version has an inline password.
    Notice the echo 'password' |
    The single quotes are no accident.
    It is easier to diagnose problems with debug information. I suggest adding log statements to your script to see what is going on.  Here is an example.
        Author: rccharles
        For testing, run in the Script Editor.
          1) Click on the Event Log tab to see the output from the log statement
          2) Click on Run
        For running shell commands see:
        http://developer.apple.com/mac/library/technotes/tn2002/tn2065.html
    on run
        -- Write a message into the event log.
        log "  --- Starting on " & ((current date) as string) & " --- "
        --  debug lines
        set unixDesktopPath to POSIX path of "/System/Library/User Template/"
        log "unixDesktopPath = " & unixDesktopPath
        set quotedUnixDesktopPath to quoted form of unixDesktopPath
        log "quoted form is " & quotedUnixDesktopPath
        try
            set fromUnix to do shell script "echo 'password' | sudo ls -l  " & quotedUnixDesktopPath
            display dialog "ls -l of " & quotedUnixDesktopPath & return & fromUnix
        on error errMsg
            log "ls -l error..." & errMsg
        end try
    end run

  • Problem running sudo command in script

    Hello,
    I try to run this script.
    #!/bin/bash
    sudo /bin/cat /dev/input/event1 >> /home/tom/.log/log.bin &
    The sudoers entry
    %users ALL= NOPASSWD: /bin/cat /dev/input/event1 >> /home/tom/.log/log.bin
    I get the following error!
    $ Passwort: sudo: pam_authenticate: Kommunikationsfehler
    Kommunikationsfehler = eng: communication error
    What's wrong with that?
    Thank you!
    clementis

    Sorry, for being not precise.
    I created the alias in .bashrc in my $home directory. In sudoers all users are allowed to key in this command without a passwort entry.
    I know how to autostart in xfce and so created an autostart entry but unfortunatly it doesn't work for my "special" command.
    What I did:
    1. I wrote a script (first post) and tried to autostart it in xfce. For that command I created an entry in sudoers in order to avoid enter root passwort. The script didn't start.
    Then...
    2. I created an autostart entry (deleted the first one - see point 1) and entered the whole command (cat .../event1 >> $home...) in the xfce autostart input window. The idea was to run the command directly and not through a script. But it didn't work.
    Therefore I tried to run alter the command to "bash cat .../event1 >> $home ...." Didn't work either.
    3. I also deleted the autostart entry no 2. and created the alias in .bashrc in my home directory. Now created a new autostart entry trying to start the alias. Didn't work.
    I also tried to set the Terminal option in the file $home/.config/autostart/log.desktop to "true".
    So it looked like this:
    [Desktop Entry]
    Encoding=UTF-8
    Version=0.9.4
    Type=Application
    Name=log
    Comment=event1
    Exec= sudo cat /dev/input/event1 >> /home/tom/.cblog/log.bin &
    StartupNotify=false
    Terminal=true
    Hidden=false
    The alias for example works well if I enter it in xterm.
    So, I don't know why it isn't working! :-(
    Thank you for any help
    Clementis

  • How to create script that run sudo-command, via automator?

    Hi
    I'd want to create script to automate one command I need quite often. I just can't get this to work.
    I'm not using english OS, but I think this is what I do. In automator I choose Utilities -> Run script
    Here's the script I try to run:
    sudo "/Library/Application Support/VMware Fusion/boot.sh --restart"
    I found some tip to do it like this:
    do shell script "sudo /Library/Application Support/VMware Fusion/boot.sh --restart password "pass" with administrator privileges
    Where pass is my admin password.
    This does not work, either.
    Could anybody help me on this?
    Thanks...
    Tomi Toivonen
    Message was edited by: Tomi

    This is not working. What's wrong?
    What's wrong is that the '--restart' is a parameter for boot.sh, and therefore needs to be included within the quotes.
    Additionally, the shell uses spaces to divide parameters, so the spaces in the command will make it appear as multiple commands - '/Library/Application', 'Support/VMware' and 'Fusion/boot.sh'. You need to escape them using a backslash:
    <pre class=command>do shell script "/Library/Application\ Support/VMware\ Fusion/boot.sh --restart" password "pass" with administrator privileges</pre>
    If it's outside of the quotes it would be interpreted as a parameter to 'do shell script' and it won't know what to do with that.

  • How to create script that run sudo-command?

    I often need to run command:
    sudo "/Library/Application Support/VMware Fusion/boot.sh" --restart
    This needs to be run as admin.
    Can anybody tell me how to create script that will login as admin and run that command in terminal?
    Or from where can I get help how to add commands to a script?
    Thanks
    Tomi

    Your best bet on getting a cogent answer is to post to the Unix forum under OS X Technologies.

  • [SOLVED] Running Two Commands at Boot

    I've tried adding to the Startup Manager in Gnome and since that didn't work, I tried adding to rc.local to no avail. The only way I can run these scripts is manually through the terminal. The commands are to enable wireless and compiz:
    wireless.sh
    sudo wicd -foe
    /usr/bin/wicd-client
    compiz.sh
    LIBGL_ALWAYS_INDIRECT=1 compiz --replace ccp & emerald --replace &
    Here's what I put in rc.local
    /etc/rc.local
    #!/bin/bash
    # /etc/rc.local: Local multi-user startup script.
    sh ~/Scripts/wireless.sh
    LIBGL_ALWAYS_INDIRECT=1 compiz --replace ccp & emerald --replace &
    Last edited by vahnx (2010-08-31 05:30:34)

    They didn't work in rc.local since that loads before you log in (I think right after it finishes loading all the daemons). You'll need to run the full path (not using ~). Also, that won't help you with running GUI apps (like compiz, or the wicd tray icon). Also, if your sudo asks for a password that's probably why it doesn't work from Gnome startup (not sure about the compiz one, maybe try putting env before the command like "env LIBGL_ALWAYS_INDIRECT=1 compiz --replace ccp && emerald --replace" And oh yeah, to run another command it's "&&", not "&" (you can also use a
    Last edited by doorknob60 (2010-08-31 05:21:31)

  • [solved] Running a command in background (bash script)

    Salut,
    as netcfg2 does not work with my wireless connection, I have to set up the connection manually. For not having to type in the commands every time, I created a bash script.
    #!/bin/bash
    iwconfig wlan0 mode managed essid mynetwork channel 6
    ifconfig wlan0 up
    wpa_supplicant -D wext -i wlan0 -c /etc/wpa_supplicant.conf -dddd &
    dhcpd wlan0
    This works fine till the wpa_supplicant line. wpa_supplicant is not started in the background (as I thought, the ampersand at the end of the line would.
    So how can I get wpa_supplicant run in the background?
    Thanks in advance,
    Stefan
    Last edited by vbtricks (2008-05-11 09:13:36)

    Ramses de Norre wrote:How do you know it isn't? What exactly does happen?
    The script does not return to the user-prompt. Is an ampersand at the end of the line the correct solution, or are you unsure yourself?
    bender02 wrote:On thing is that even if it starts, it does take it a couple of seconds to connect, so it's probably not very good to run dhcpcd right after wpa_supplicant. Another thing is that you probably have a typo up there, shouldn't it be 'dhcpcd' instead of 'dhcpd'?
    Well, as the wpa_supplication command is not really run in the background the script did never execute the dhcpcd command. After having solved the above, correcting the spell-mistake will be a smaller problem. Even calling the dhcpcd command myself would be no unworkable way, the open root-shell (as I have to use another as the one calling the script is blocked) is a far greater problem...

  • PLEASE HELP making a program that uses sudo commands

    hey
    is there anyway to make a program run sudo commands
    i have a problem because in a terminal it would ask you to input a password.
    is there a way to use a fake keyboard program to input the password in the back ground
    of my java application or some how run sudo commands? my application relies on some outputs of these commands.
    please help thanks.

    ill tell you a bit about my program im making.
    i have a wireless usb that i have to switch back and forth to get my paticullar drivers to
    do certain things e.g. one is for web browsing and the other one has packet injection.
    by typing sudo modprobe ect.. i can switch through a terminal. which requires me to type in a password.
    iv made a application which uses the
    Runtime.getRuntime().exec("sudo ...."); it has 2 buttons to switch from diffrent drivers and always runs at start up.
    just a big problem i cant use sudo it just freezes when i start and click on the buttons. and doesnt ask me for a password.
    can anyone help thanks

  • Error executing sudo command

    When I try to execute a sudo command I get the following message:
    "sudo: /etc/sudoers is mode 0644, should be 0440
    Segmentation fault"
    What does it mean?
    Thanks for all the help.

    When I try to execute a sudo command I get the following message:
    "sudo: /etc/sudoers is mode 0644, should be 0440
    Segmentation fault"
    What does it mean?
    It means someone has incorrectly modified the /etc/sudoers file, and as a side effect its permissions were changed to an insecure form, so sudo refuses to use the file. It is also possible that the /etc/sudoers file syntax is also invalid, which might also cause the segmentation fault.
    'visudo' should be used to edit the /etc/sudoers file, which would have properly preserved the permissions, as well as verified that the updated /etc/sudoers file contained valid syntax so that sudo would continue to work. See 'man visudo'.
    You now have a problem, in that you can no longer use sudo to fix the permissions.
    You should first try booting your installation DVD. Via the Menu, you can run Disk Utility and run Repair Permissions. Hopefully this will repair the /etc/sudoers file. You could try running Applications -> Utilities -> Disk Utility, however, if it wants to use sudo to elevate its privileges, that would fail, as a broken /etc/sudoers file is still a broken /etc/sudoers file.
    If you have by any chance enabled the 'root' account, you could try logging in as root and fixing the /etc/sudoers file's permissions.
    You should also run visudo, so that the file's syntax is valid.
    I think you can use the Installation DVD to enable the root account. that might be another way to login as root.
    If repair permissions does not work, then maybe you can put your Mac into firewire target mode, mount your Mac's disk on another Mac via firewire, and repair the /Volume/Macintosh HD/etc/sudoers permissions from the working Mac.
    If that failes, the next step is booting your Mac in Single User mode. Search for "Single User Mode" in the forums, and you should find instructions on both booting, as well as how to make the root file system writable so you can change the permissions from single user mode.
    And if that does not work, I think you are left with reinstalling the OS.
    Message was edited by: BobHarris

  • Terminal automatically executes sudo command on opening?

    I am running Lion 10.7.5. I tried to execute a sudo command but remembered i did not have sudo privleges on my account so i logged onto the root user and executed it there. Getting back on my regular account i opened terminal but it automatically executed the sudo command again and if i exceeded the password attemp limit it just re executed the command and looped infinently. I now cannot use terminal on my account. I have deleted com.apple.Terminal.plist from its location and still nothing.

    Did you accidentally put a sudo command into one of the following files?
    .bash_profile
    .bash_login
    .profile
    .bashrc
    Or does one of the above files run a script that tries to execute sudo?
    If you cannot get a command prompt you can use Terminal -> Shell -> New Command to issue one command at a time.
    Message was edited by: BobHarris

  • Run a sudo command on system startup via Applescript?

    Hey, I'm trying to hide the Spotlight icon on my menu bar using the sudo command shown here:
    http://osxdaily.com/2011/12/12/hide-spotlight-menu-icon-mac-os-x-lion/
    It works fine, but when I reboot my Mac (I tend to turn it off at night since it has a habit of waking from sleep by itself) the icon has re-appeared, and I need to run the command again.
    I'd ideally like to write an Applescript to run on launch to execute the two commands (which also requires entering my password), but I've not been successful, I get various errors talking about authentication and such.
    Is there an easier way to get this icon permanently hidden? I use Alfred, so while I make use of the Spotlight index, I never touch the magnifying glass icon, and I like to keep a clean menu bar!

    You don't need applescript. What you need to create is a LaunchAgent. Lingon (available through the App Store) can help you write it. But, there are other sources that can show you how to write the raw file and put it in the LaunchAgents folder, like here: http://www.devdaily.com/mac-os-x/launchd-examples-launchd-plist-file-examples-ma c

  • Running Shell Commands (not Executable) in Unix from Java

    What are my options to run shell commands from Java?
    My goal is to change my existing shell environment variables to some new ones provided by .anotherProfile.
    Using an executable from Java is not an option because it does not work i.e. ( exec(". /home/.profile") ) brings up errors.
    Someone has suggested that I start a child shell with that profile and work from there, but I'm unfamiliar with that sort of syntax and programming in general.
    Any good help equals duke dollars :)

    Well there are some possibilities. In the original thread you mentioned that you wanted the shell script to be executed to change some enviroment parameters of the shell the JVM is executing in.
    If so, and you are able to rewrite the profile so you can parse it manually. Then you can change some environment setting by writing the JNI wrappers for the getenv and setenv system calls. (Check your man pages)
    That will change the environment. I am just wondering what good it will do for you? What's use of sourcing the profile in a JVM?

  • [solved] sudo command in a Bash function?

    Hello there,
    I need to use sudo inside of a Bash function in order to allow my laptop to hibernate after a certain time using "systemctl hibernate".
    However, I cannot figure out what's the correct way to include sudo inside of my function snippet:
    function hibernate {
    echo "System is going to hibernate in $1 minute(s)..."
    sleep $1m ; systemctl hibernate
    alias hibernate_timer=sudo hibernate # sudo misplaced here
    Last edited by orschiro (2013-06-26 20:33:47)

    you'd need sudo's -E parameter for that, otherwise that function is not defined in the su environment.
    EDIT: -E is not sufficient.
    man sudoers wrote:In all cases, environment variables with a value beginning with () are
         removed as they could be interpreted as bash functions.  The list of environ‐
         ment variables that sudo allows or denies is contained in the output of “sudo
         -V” when run as root.
    You cannot pass functions to sudo - the alias is not relevant, you couldn't do this directly: `sudo hibernate` would fail.
    I'd also recommend putting the sudo as "deep" as possible: right on the systemctl command.  There is a reason bash functions are not allowed to be 'sudo'ed - and running sudo on a function that includes a sleep command seem particularly bad to me.
    Last edited by Trilby (2013-06-26 17:59:18)

  • Running following command gives,0403-006 Execute permission denied

    Dear
    According to metalink doc id: 422419.1 for enabling & collect debugging files for HTTP, OC4J and OPMN. But when i run this command using applmgr (apps owner) in AIX gives '0403-006 Execute permission denied'. Though applmgr have full permissions on this mount point where instance is hosted, giving this error.What is going wrong does not know?
    Any help, highly appreciated.

    The command is
    zip -r /tmp/`uname -n`_`date +%m%d%y.%H%M`_iAS_CONFIG.zip \
    $ORA_CONFIG_HOME/10.1.3/Apache/Apache/conf \
    $ORA_CONFIG_HOME/10.1.3/config \
    $INST_TOP/pids/10.1.3/Apache \
    $ORA_CONFIG_HOME/10.1.3/j2ee/ \
    $ORA_CONFIG_HOME/10.1.3/javacache/admin \
    $ORA_CONFIG_HOME/10.1.3/network/admin \
    $ORA_CONFIG_HOME/10.1.3/opmn
    When i copy & paste in telnet it was adding some spaces. Removing spaces resloved the issue. Thanking you.

  • Can't run bash or sudo commands

    I have a Mac Pro with OS X 10.8.2 and also an older iMac running OS X 10.6.8. On neither Mac can I run any bash or sudo commands! I always get command not found.
    Both Macs have fresh installs of OS X, did I forget to install something? Is X11 needed? or Xcode?
    Thanks for any help.

    Back up all data.
    Select
    Shell ▹ New Command
    from the Terminal menu bar. Copy and paste the following line into the text box that opens:
    mkdir disabled_shell_files; mv .profile .bash* $_
    Your old shell initialization and history files will be saved in a directory with the indicated name at the top level of your home directory.

Maybe you are looking for

  • IPod touch 4th generation will not connect to Uverse internet

    When Uverse internet was setup in my home my 64 gb IPOD touch 3rd generation stopped connecting to internet.  I've turned my wifi off and on (that doesn't work), I've tried rebooting the IPOD by turning it completely off then turning back on.  I've m

  • Skype getting disabled

    I have skype set up to auto start when my computer starts up. However it will randomly get disabled in my start up menu in the task manager in windows 8.1. Anyone know why this keeps happening? Solved! Go to Solution.

  • Problems with XMLType and namespace

    Dear subscribers: I have been using XMLType successfully with simple XMLs - without namespaces. Now I must handle messages with "xmlns" and it seems XMLType is failing. I tested the following XML, extracted from w3c.org examples: <?xml version="1.0"?

  • ACH and WIRE PAyments

    Hi All, Could any one throw some light on the paymnet method and the form details for effecting ACH and Wire Payment? Can I use the method under F110 Program ? Thanks, Chitra

  • How do I get started using Essbase?

    Hi all, I'm a newbie to Essbase, and Business Intelligence. I went to a training/demo in March but have just done my first install to try it out for myself. (I'm a Java developer by training.) So, I've installed Hyperion EPM 11.1.1.1 on my Windows XP