Mail Archives: cygwin/2001/08/01/03:34:47
> Hello all,
>
> I need to reboot a win2k box from remote. I'm using ssh to connect to
> the machine... is there a command that I can send through cygwin to
> reboot the machine?
I wrote the following tiny code in 1998. If you call it `shutdown.exe'
it will shutdown, call it `reboot.exe' and it will reboot. Otherwise
use the flags.
Hope, that helps,
Corinna
------------------------------ SNIP -------------------------------
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <getopt.h>
#include <windows.h>
int reboot = FALSE;
int force = FALSE;
char *myname;
int usage(void)
{
fprintf(stderr, "usage: %s [-fr] secs|\"now\"\n", myname);
return 1;
}
int main(int argc, char **argv)
{
int c;
unsigned int secs;
HANDLE token;
TOKEN_PRIVILEGES privs;
if ((myname = strrchr(argv[0], '/')) ||
(myname = strrchr(argv[0], '\\')))
++myname;
else
myname = argv[0];
if (strrchr(myname, '.'))
*strrchr(myname, '.') = '\0';
if (! strcmp(myname, "reboot"))
reboot = TRUE;
while ((c = getopt(argc, argv, "fr")) != EOF)
switch (c) {
case 'f':
force = TRUE;
break;
case 'r':
reboot = TRUE;
break;
default:
return usage();
}
if (optind >= argc)
return usage();
if (! strcmp(argv[optind], "now"))
secs = 0;
else if (! isdigit(argv[optind][0])) {
usage();
fprintf(stderr, "%s: secs must be numerical or the word \"now\"\n",
myname);
return 2;
} else
secs = atoi(argv[optind]);
privs.PrivilegeCount = 1;
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &privs.Privileges[0].Luid);
privs.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
if (! OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &token)) {
int ret = GetLastError();
fprintf(stderr, "%s: insufficient privileges\n", myname);
return 3;
}
if (! AdjustTokenPrivileges(token, FALSE, &privs, 0, NULL, NULL)) {
fprintf(stderr, "%s: insufficient privileges\n", myname);
return 3;
}
printf("WARNING!!! System is going down ");
if (secs)
printf("in %d seconds\n", secs);
else
printf("NOW\n");
while (secs)
secs = sleep(secs);
if (! InitiateSystemShutdown(NULL, NULL, 0, force, reboot)) {
fprintf(stderr, "%s: insufficient privileges\n", myname);
return 3;
}
return 0;
}
--
Corinna Vinschen Please, send mails regarding Cygwin to
Cygwin Developer mailto:cygwin AT cygwin DOT com
Red Hat, Inc.
--
Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple
Bug reporting: http://cygwin.com/bugs.html
Documentation: http://cygwin.com/docs.html
FAQ: http://cygwin.com/faq/
- Raw text -