-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kc
committed
Jul 26, 2023
1 parent
630de32
commit 49402ea
Showing
2 changed files
with
38 additions
and
38 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { exec } from 'child_process'; | ||
import * as os from 'os'; | ||
|
||
export enum OsType { | ||
LINUX = 'linux', | ||
DARWIN = 'darwin', | ||
WINDOWS_NT = 'windows_nt', | ||
} | ||
|
||
/** | ||
* Executes an osquery command and returns the output. | ||
* @param query The osquery command to be executed. | ||
* @param osTypes The list of operating systems on which the command can be executed. | ||
* @return A Promise that resolves with the command output, or rejects with an error message. | ||
*/ | ||
export function runOsquery(query: string, osTypes: OsType[]): Promise<string> { | ||
return new Promise((resolve, reject) => { | ||
const currentOsType = os.type().toLowerCase(); | ||
|
||
if (!osTypes.includes(currentOsType as OsType)) { | ||
reject(`Unsupported operating system: ${currentOsType}`); | ||
return; | ||
} | ||
|
||
exec( | ||
`osqueryi --json "${query}"`, | ||
(error: Error | null, stdout: string, stderr: string) => { | ||
if (error) { | ||
reject(`Execution error: ${error.message}`); | ||
} else if (stderr) { | ||
reject(`Command error: ${stderr}`); | ||
} else { | ||
resolve(stdout); | ||
} | ||
}, | ||
); | ||
}); | ||
} |