I am developing a small shutdown scheduler project in which i have to put the computer in "Stand By" mode. The command that i am using is
Runtime.getRuntime().exec("cmd /c Powrprof.dll,SetSuspendState ");
This command requires Admin rights which i don't know how to get. Also while searching for previous answers i found i can use elevate.exe as
Runtime.getRuntime().exec("c:/elevate Rundll32.exe Powrprof.dll,SetSuspendState ");
Elevate.exe is doing the task but is consuming too much of time i.e. making the software slow. Is there any other speedy way? I am using Netbeans IDE.
Runtime.getRuntime().exec("runas /profile /user:Administrator \"cmd.exe /c Powrprof.dll,SetSuspendState\"");
Also plz see comments
Running as admin without Admin rights
You have a few options
A. Create a shortcut with admin priv.
The shortcut will run cmd /c Rundll32.exe Powrprof.dll,SetSuspendState
Your Java code will run the shortcut:
Runtime rt = Runtime.getRuntime();
rt.exec("cmd /c start \"\" \"myshortcut.lnk\"")
Right click the shortcut icon > properties > advanced > run as administrator
B. Run the java process as administrator
Again, create a shortcut and set to run as administrator. Any processes spawned will also have admin privileges. Your java code will run:
rt.exec("cmd /c Powrprof.dll,SetSuspendState")
C. Use JNA to directly call SetSuspendState routine. The Java process will require admin priv (like B), but you won't have to spawn a process. If you like this, I can provide source code.
D. Use wizmo utility: wizmo quiet standby
Add parameter /savecred
runas /profile /user:Administrator /savecred
Input the password one times. In future OS will not ask you password.
I'm using Windows 10. IDK why but runas isn't working and isn't reporting any errors.
I found this answer on superuser.com:
powershell -Command "Start-Process 'cmd.exe /c Powrprof.dll,SetSuspendState ' -Verb runAs"
No password required if you have permission to elevate.
No shortcut required on client machine
No dependency on runas
Requires powershell
Powershell is installed by default on Windows since Windows 8 and Windows Server 2008 R2, according to an answer found on serverfault.com.
Related
I was trying to execute Runtime.getRuntime().exec("taskkill /PID ... /F");, but I keep getting permission denied on error stream. I know it normally requires opening cmd as root, but I was wondering if there's a way to achieve that with code (something in Windows like adding sudo in the beginning).
I'd suggest you to use the runas functionality in Windows.
Runas allows a user to run specific tools and programs with different
permissions than the user's current logon provides.
Please try something like:
Runtime.getRuntime().exec("cmd /c \"runas /savecred /user:theDomain\\administrator yourCommand\"")
Runas Documentation
You can open CMD with administrator privileges and run your Java program with Administrator privileges too. When invoking to shell it should inherit those permissions and be able to exec with Administrator privileges.
I researched this question and all answers suggest visudo to add:
Defaults:user !requiretty
It does not work!
I have two Linux boxes (RedHat). I have a Java class which uses ProcessBuilder/Process to execute commands. The class runs under a system account.
On both boxes, I run
su other-user -c 'some-command'
and provide the password for other-user in an input stream to the Process object.
On one box, the command succeeds and on the other I get the error above.
The sudoers files on both machines did not have exclusions for requiretty ('Defaults requiretty' is set on both files).
I tried adding the exclusion as suggested by the answers I found. I tried both the system user running the process and 'other-user'...
Does not work.
What else can be the issue? Do I need to restart the system after adding the requiretty exceptoin(s)?
sudoers is for sudo rather than su so you should use sudo.
According to su manual:
-c, --command COMMAND
Specify a command that will be invoked by the shell using its -c.
The executed command will have no controlling terminal. This option cannot be used to execute interactive programs which need a controlling TTY.
you can use a TTY spawning if you are trying to avoid using sudo or you don't have a sudo privileges.
Just invoke one of the following codes before running the code which giving you the error you mentioned.
here are some examples of codes you can use, depends on the code or the system you are using:
python -c 'import pty; pty.spawn("/bin/sh")'
echo os.system('/bin/bash')
/bin/sh -i
perl —e 'exec "/bin/sh";'
perl: exec "/bin/sh";
ruby: exec "/bin/sh"
lua: os.execute('/bin/sh')
(From within IRB)
exec "/bin/sh"
(From within vi)
:!bash
(From within vi)
:set shell=/bin/bash:shell
(From within nmap)
!sh
the first three choices up are my common used ones, and I am trusting their results.
I am using them while pentesting.
I executed the netsh command from the CMD that was manually opened by me by right clicking the CMD icon from the start and then selecting run as administrator from the options.It worked fine.Now I tried to run the netsh command through my java code,then it is not working.Nothing is happening when i run that code.I want to ask that I can run applications like notepad.exe from the cmd by calling appropriate methods of the runtime class from my java code,But how can I open the same application with the administrator priviliges from my java code.r.exec("notepad"); where r is an object reference to the runtime class will run the application,but the notepad so opened will not be in administrator mode.Actually I guess that learning to run the application in administrator mode from CMD will be enough to do the work done as The corresponding CMD command will be passed as the argument to the exec() method of the Runtime class.So my questions are:
How to run any application from CMD in windows 8 with administrator privilliges?
The way i want to implement the use of netsh is a good thing to practise or there is some other way out i must use these commands from my java code.
I have seen some commands while googling but they where not working out for me,like runas /user:administrator "notepad.exe" etc.
Thanks
You cannot use the runas /user:administrator approach, as that requires a password input which you cannot provide from an external source (such as a Java application) for security reasons.
I had a similar issue to you in the past, and I solved it using PsExec, running the process on localhost with an administrator username and password allowed me to execute external applications as an administrator.
Using your example you could run:
PsExec.exe \\\\127.0.0.1 /accepteula -u USER -p PASSWORD notepad.exe
The "/accepteula" flag prevents the requirement to accept the EULA interactively when run on a machine for the first time.
This approach may require a bit of tweaking to get working with your setup, but hope it gives you a starting point.
I have created a small program in java which reads the UninstallString of an application from registry and then using
Process p = Runtime.getRuntime.exec("command")
to uninstall this application. But this task require administrator priviledge.
On Windows Vista, 7 and 8 this work fine by creating a bat file with the following command "java -jar java_app.jar", right click on the bat file and run as administrator.
But on XP, my account is an administrator account but still it fails to run the java program as administrator.
Can anyone provide me with some solutions which can solve this problem?
You can use RunAs command:
RUNAS /TrustLevel:Unrestricted notepad.exe
You can find trust levels available on you system:
RUNAS /ShowTrustLevels
The following liks explains that in more details http://blog.johnmuellerbooks.com/2011/04/26/simulating-users-with-the-runas-command.aspx
I try to run a java application through scheduled task in XP :
at 11:00 /every:m,t,w,th,f,s,su cmd /c "C:\Projects\piko\dist\piko.jar" arg
I realize in piko.jar, it always read empty for
System.getProperty("user.home")
If I run it as normal application through command prompt,
java -jar C:\Projects\piko\dist\piko.jar arg
The environment variable is there.
May I know how can I resolve this?
Have you tried:
cmd /c "java -jar C:\Projects\piko\dist\piko.jar arg"
Otherwise you probably have to specify a username and password to use when you schedule the task... it might be that XP is running it under some internal/system account which has no associated "user", per se.