Skip to content

Instantly share code, notes, and snippets.

@distributedlife
Last active December 28, 2015 02:39
Show Gist options
  • Select an option

  • Save distributedlife/7429686 to your computer and use it in GitHub Desktop.

Select an option

Save distributedlife/7429686 to your computer and use it in GitHub Desktop.
distributedlife.com blog post code snippets
void DeleteFile (const char* const filename)
{
lr_set (filename, "__DeleteFile_filename") ;
system ("del /S /Q {__DeleteFile_filename}") ;
}
enum EnabledOptions StringIsEmpty (const char* const a)
{
if (strcmp (a, "") == '\0')
{
return Yes ;
}
else
{
return No ;
}
}
enum EnabledOptions StringsAreEqual (const char* const a, const char* const b)
{
if (strcmp (a, b) == '\0')
{
return Yes ;
}
else
{
return No ;
}
}
enum EnabledOptions IsInString (const char* const Source, const char* const Find)
{
if (strstr (Source, Find) == 0)
{
return No ;
}
else
{
return Yes ;
}
}
void CheckRegistryPathExists (const char* const Server, const char* const SubKey)
{
const char LineSeperators[] = "\n" ;
const char LinePartSeperators[] = "\t" ;
char* LineToken;
char* LinePartToken ;
char* NextPartToken ;
lr_set (Server, "__CheckRegistryValue_Server") ;
lr_set (SubKey, "__CheckRegistryValue_SubKey") ;
system
(
lr_get
(
"REG QUERY \"\\\\{__CheckRegistryValue_Server}\\{__CheckRegistryValue_SubKey}\" > {TempFile}"
)
) ;
ReadFile (lr_get ("{TempFile}"), "RegExtract", "UTF-8") ;
if (strstr (lr_get ("{RegExtract}"), "Error: The system was unable to find the specified registry key or value") != '\0')
{
lr_error_message (lr_get ("The supplied registry path ({__CheckRegistryValue_Server}\\{__CheckRegistryValue_SubKey}) does not exist")) ;
}
}
void CheckRegistryValue (const char* Server, const char* SubKey, const char* Property, const char* ExpectedValue)
{
const char LineSeperators[] = "\n" ;
const char LinePartSeperators[] = "\t" ;
char* LineToken;
char* LinePartToken ;
char* NextPartToken ;
lr_set (Server, "__CheckRegistryValue_Server") ;
lr_set (SubKey, "__CheckRegistryValue_SubKey") ;
lr_set (Property, "__CheckRegistryValue_Property") ;
system
(
lr_get
(
"REG QUERY \"\\\\{__CheckRegistryValue_Server}\\{__CheckRegistryValue_SubKey}\" /v {__CheckRegistryValue_Property} > {TempFile}"
)
) ;
ReadFile (lr_get ("{TempFile}"), "RegExtract", "UTF-8") ;
//Format for Reg output
// blank line
// declaration
// blank line
// path
// property type value
LineToken = (char *)strtok(lr_get ("{RegExtract}"), LineSeperators); // Get the first token
if (!LineToken)
{
lr_error_message ("Registry Value Not Found (No tokens found in string!)") ;
return ;
}
while (LineToken != NULL)
{
// Find line that contains the property
if (strstr (LineToken, lr_get ("{__CheckRegistryValue_Property}")) > 0)
{
//The format of the line is "property[\t]type[\t]value"
LinePartToken = (char *)strtok(LineToken, LinePartSeperators); // Get the first token
if (!LinePartToken)
{
lr_error_message ("Registry Value Not Found (No tokens found in string!)") ;
return ;
}
while (LinePartToken != NULL)
{
NextPartToken = LinePartToken ;
LinePartToken = (char *) strtok (NULL, LinePartSeperators) ;
}
//we have the last token in 'Next' so use that
Trim (NextPartToken, "ActualValue") ;
TrimNewLine (lr_get ("{ActualValue}"), "ActualValue") ;
CompareStrings (lr_get ("{ActualValue}"), ExpectedValue) ;
return ;
}
LineToken = (char *) strtok (NULL, LineSeperators) ;
}
lr_error_message ("Registry Value Not Found") ;
}
void DeleteRemoteFile (const char* const filename)
{
//Create batch file
long FileHandle = 0 ;
lr_set (filename, "__RenameFile_filename") ;
FileHandle = fopen("del.bat", "w") ;
if (!FileHandle)
{
lr_error_message ("Unable to create the \"del.bat\" file.") ;
return
}
fprintf (FileHandle, lr_get ("del /S /Q {__RenameFile_filename}")) ;
fclose (FileHandle) ;
//Xcopy batch to remote
system (lr_get ("xcopy /Y /Q \"del.bat\" \\\\{Server}\\c$\\temp\\*")) ;
//psexec batch on remote
system (lr_get ("\"psexec.exe\" \\\\{Server} C:\\temp\\del.bat")) ;
}
void RenameRemoteFile (const char* const Path, const char* const OldFilename, const char* const NewFilename)
{
//Create batch file
long FileHandle = 0 ;
lr_set (Path, "__RenameFile_Path") ;
lr_set (OldFilename, "__RenameFile_OldFilename") ;
lr_set (NewFilename, "__RenameFile_NewFilename") ;
FileHandle = fopen("ren.bat", "w") ;
if (!FileHandle)
{
lr_error_message ("Unable to create the \"ren.bat\" file.") ;
//return Result ;
}
fprintf (FileHandle, lr_get ("ren {__RenameFile_Path}{__RenameFile_OldFilename} {__RenameFile_NewFilename}")) ;
fclose (FileHandle) ;
//Xcopy batch to remote
system (lr_get ("xcopy /Y /Q \"ren.bat\" \\\\{Server}\\c$\\temp\\*")) ;
//psexec batch on remote
system (lr_get ("\"psexec.exe\" \\\\{Server} C:\\temp\\ren.bat")) ;
}
void GetEvent (const char* EventId, const char* Event)
{
if (!ExceptionReportingEnabled ())
{
//only check for exceptions if we are supposed to
return ;
}
lr_save_string (EventId, "__EventId") ;
Create_GetEvent_BatchFile (EventId) ;
system ("ServiceTest.bat") ;
ReadFile (lr_eval_string("{TempFile}"), "FileData", "ISO-10646-UCS-2") ;
lr_xml_extract
(
"XML={FileData}",
"FastQuery=ROOT/ROW",
"XMLFragmentParam=__Event",
"NotFound=Continue",
LAST
);
lr_save_string (lr_eval_string ("{__Event}"), Event) ;
}
void Create_GetEvent_BatchFile (const char* EventId)
{
long FileHandle = 0 ;
lr_set (EventId, "__EventId") ;
lr_set
(
lr_get
(
"\"\\\\vcclfs02-data1\\Wessupp\\Applications Test Team\\ServiceTest\\Tools\\Log Parser 2.2\\LogParser.exe\" -i:EVT -o:xml -schemaType:0 -compact -oCodepage:0
\"SELECT
EventLog,
RecordNumber,
TimeGenerated,
TimeWritten,
EventId,
EventType,
EventTypeName,
EventCategory,
EventCategoryName,
SourceName,
ComputerName,
SID,
Message
INTO
{TempFile}
FROM
\\\\{Server}\\{EventLogSource}
WHERE
RecordNumber = '{__EventId}'\""
),
"__Create_GetEvent_BatchFile"
) ;
lr_set (lr_get ("{__Create_GetEvent_BatchFile}"), "__ServiceTest_LastBatchBody") ;
FileHandle = fopen("ServiceTest.bat", "w") ;
if (!FileHandle)
{
lr_error_message ("Unable to create the \"ServiceTest.bat\" file.") ;
return ;
}
fprintf (FileHandle, lr_get ("{__Create_GetEvent_BatchFile}")) ;
fclose (FileHandle) ;
}
lr_set
(
lr_get
(
"\"\\\\vcclfs02-data1\\Wessupp\\Applications Test Team\\ServiceTest\\Tools\\Log Parser 2.2\\LogParser.exe\" -i:EVT -o:xml -schemaType:0 -compact -oCodepage:0
\"SELECT
MAX(RecordNumber)
INTO
{TempFile}
FROM
\\\\{Server}\\{EventLogSource}
WHERE
SourceName = '{FullServiceName}'\""
),
"__Create_GetMostRecentEventRecordNumber_BatchFile"
) ;
void GetMostRecentEventRecordNumber (HpParameter StartEventCount)
{
if (!ExceptionReportingEnabled ())
{
//only check for exceptions if we are supposed to
return ;
}
Create_GetMostRecentEventRecordNumber_BatchFile () ;
system ("ServiceTest.bat") ;
ReadFile (lr_eval_string("{TempFile}"), "FileData", "UTF-8") ;
lr_xml_get_values
(
"XML={FileData}",
"FastQuery=ROOT/ROW/MAX_ALL_RecordNumber_",
"ValueParam=__GetMostRecentEventRecordNumber_StartEventCount",
"NotFound=Continue",
LAST
);
Trim (lr_get("{__GetMostRecentEventRecordNumber_StartEventCount}"), StartEventCount) ;
}
enum GetStringOfLengthTypes
{
AlphaOnly,
NumericOnly,
AlphaNumeric
} ;
const char* GetStringOfLength (const unsigned int length, const unsigned int Type)
{
unsigned int i = 0 ;
static const char Alpha[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
static const char Numeric[] = "0123456789" ;
static const char AlphaNumeric[] = "abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"0123456789" ;
char* Value = (char*) malloc ( (sizeof(char) * length) + 1) ;
if (SrandSingleton == 0)
{
srand (time (NULL)) ;
SrandSingleton = 1 ;
}
for (i = 0; i < length; i++)
{
switch (Type)
{
case 0:
Value[i] = Alpha[rand() % (sizeof (Alpha) - 1)] ;
break ;
case 1:
Value[i] = Numeric[rand() % (sizeof (Numeric) - 1)] ;
break ;
case 2:
Value[i] = AlphaNumeric[rand() % (sizeof (AlphaNumeric) - 1)] ;
break ;
}
}
Value[length] = '\0' ;
lr_get (Value, "__GetStringOfLength_Total") ;
free (Value) ;
return lr_get ("{__GetStringOfLength_Total}") ;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment