Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to write a batch file to kill of clients?

Status
Not open for further replies.

bensheares1

IS-IT--Management
Sep 12, 2003
6
0
0
SG
Hi All,

Anyone ideas how to write a simple batch file to kill off the clients abnormally(eg. we use "Ctrl C" to kill off clients abnormally)? The idea here is:

1. Start client 1
2. Sleep for 2 secs
3. Kill off client abnormally using "Ctrl C"
4. Start client 2
5. Sleep for 2 secs
6. Kill off client abnormally using "Ctrl C"
.....
....
...
7. Kill off client 50 abnormally using "Ctrl C"

This will continue until all the 50 clients are kill off. My server is also starting at the same time and I need to generate as many concurrent clients to access the server objects. I need to run this windows bactch file for my clients side.

Thanks in advance for any suggestions.

Best regards,
Sheares
 
The following is a small command line utility I wrote to terminate a remote machine. You can alter this as necessary and then use a simple batch file to run the program repeatedly, once for every machine you want to shutdown (obviously you need administrator rights on the machines you want to shut down)

#include <stdio.h>
#include <stdos.h>
#include <windows.h>

LRESULT CALLBACK EXPORT wincallback (HWND hwnd, MESSAGE message, WPARAM warg, LPARAM larg);

HWND applnwinhdl = NULL;
HBRUSH bkbrush = NULL;
HINSTANCE applninstance = NULL;
CCHAR *applntitle = &quot;Remote Terminationn&quot;;
BOOL parsecmdline(CHAR *cmdline, CHAR *machine, CHAR *message);
BOOL killmachine (CHAR *machine, CHAR *closemsg);


INT WINAPI WinMain (HINSTANCE currinstance, HINSTANCE previnstance, LPSTR cmdline, INT showmode)
{ MSG msg;
INT msgret;
INT ok = 1;
DWORD err;
WNDCLASS WC;
CHAR machine[100];
CHAR message[500];
const CHAR *winclassname = &quot;SHUTDOWN&quot;;
applninstance = currinstance;

// Register class:
bkbrush = CreateSolidBrush (GetSysColor(COLOR_BTNFACE));
WC.style = CS_HREDRAW | CS_VREDRAW;
WC.lpfnWndProc = (LPVOID) wincallback;;
WC.cbClsExtra = 0;
WC.cbWndExtra = 0;
WC.hInstance = applninstance;
WC.hIcon = NULL;
WC.hCursor = LoadCursor (NULL, IDC_ARROW);
WC.hbrBackground = bkbrush;
WC.lpszMenuName = NULL;
WC.lpszClassName = winclassname;
RegisterClass (&WC);

// Create window - We need a window and a message pump but need do nothing with them :
if ((applnwinhdl = CreateWindowEx (0, winclassname, applntitle, WS_OVERLAPPEDWINDOW | DS_3DLOOK, 0,0,700,350, NULL, NULL, applninstance, NULL)) == NULL)
MessageBox(applnwinhdl,&quot;Initialisation Failure - window not created.&quot;, NULL,MB_OK|MB_SYSTEMMODAL|MB_ICONHAND);

// Parse the command line args :
if ((ok = parsecmdline(cmdline,machine,message)) == FALSE)
MessageBox(applnwinhdl,&quot;Incorrect command line.\n Usage: 'shutdown -c:<computer name> -m:<message>'&quot;, NULL,MB_OK|MB_SYSTEMMODAL|MB_ICONHAND);
else
{ // Call System Shutdown :
if ((ok = killmachine(machine,message)) == FALSE)
MessageBox(applnwinhdl,&quot;Unable to remotely close computer.\n Permissions are too high or computer is already off.&quot;, NULL,MB_OK|MB_SYSTEMMODAL|MB_ICONHAND);
}

// Send close message to kill the window etc. :
SendMessage(applnwinhdl,WM_CLOSE,0,0);

// Default Message Loop:
if (applnwinhdl != NULL)
{ while( (msgret = GetMessage( &msg, NULL, 0, 0 )) != 0)
{ if (msgret == -1)
{ // error
MessageBox(applnwinhdl,&quot;Unrecognised or malformed Message - error occurred in pump while processing message.&quot;, NULL,MB_OK|MB_SYSTEMMODAL|MB_ICONHAND);
}
else
{ TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}

// Unregister the window class and exit :
UnregisterClass (WC.lpszClassName, currinstance);
return 0;
}

LRESULT CALLBACK EXPORT wincallback (HWND hwnd, MESSAGE message, WPARAM warg, LPARAM larg)
{ switch (message)
{ case WM_CLOSE :
DestroyWindow(hwnd);
PostQuitMessage(0);
return 0;
default :
return DefWindowProc (hwnd,message,warg,larg);
}
}

BOOL parsecmdline (CHAR *cmdline, CHAR *machine, CHAR *message)
// Parse Command Line Arguments to determine the network computer name and
// the display message.
// Note that args must be in order : -c:<computer name> -m:<message>.
{ CHAR cmd[500];
INT size = 0;
CHAR *cptr = NULL;
CHAR *space = NULL;

// First take a copy of the command line:
strcpy(cmd,cmdline);

// Find the start and end of the computer name :
if ((cptr = strstr(cmd,&quot;-c:&quot;)) == NULL)
return FALSE;
if ((space = strchr(cmd,' ')) == NULL)
return FALSE;

// Copy and null terminate:
size = space - (cptr+3);
strncpy(machine,cptr+3,size);
*(machine+size) = '\0';

// Advance command line pointer and find message:
cptr += size;
if ((cptr = strstr(cmd,&quot;-m:&quot;)) == NULL)
return FALSE;

// Copy the message and return:
strcpy(message,cptr+3);
return TRUE;
}


BOOL killmachine (CHAR *machine, CHAR *closemsg)
// Terminate the local machine or any network machine. We need to first acquire
// shutdown privilages on the machine in order to successfully force a close :
{ HANDLE token; // handle to process token
TOKEN_PRIVILEGES tkp; // pointer to token structure
BOOL result; // system shutdown flag

// Get the current process token handle so we can get shutdown privilege.
if (!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &token))
return FALSE;

// Get the LUID for shutdown privilege.
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);

// One privilege to set:
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;

// Get shutdown privilege for this process.
AdjustTokenPrivileges(token, FALSE, &tkp, 0,(PTOKEN_PRIVILEGES) NULL, 0);

// Cannot directly test the return value of AdjustTokenPrivileges.
if (GetLastError() != ERROR_SUCCESS)
return FALSE;

// Display the shutdown dialog box and start the countdown.
result = InitiateSystemShutdown(machine,closemsg,0,FALSE,TRUE);

if (!result)
return FALSE;

// Disable shutdown privilege.
tkp.Privileges[0].Attributes = 0;
AdjustTokenPrivileges(token, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES) NULL, 0);
return TRUE;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top