gm_cmd - Execute commands at the command prompt from GMod

Again, this is another tool for developers and server owners really. Here come the incredibly short docs.



cmd.exec() - takes one arg, the string to run at cmd. Returns 1 for success, 0 for failure.


Example script:
[lua]
require(“cmd”)
cmd.exec(“ping google.co.uk”)
[/lua]

And the download links:

Source download: http://www.hexxeh.net/code/gm_cmd_src.zip
Binary download: http://www.hexxeh.net/code/gm_cmd.zip

Nice!

New version coming up shortly where you can access the output of the program.

Great DLL, looks fantastic!

Now THAT would be quite a landmark :wink:

I like the stuff you’re doing here, it’s back-end, but very functional! Keep up the excellent work :slight_smile:

Thanks for the positive responses guys!

Edit:



//CMD module for GMod by Hexxeh
#include <string>
#include <sstream>
#include <windows.h>
#include "GMLuaModule.h"
#include "exec-stream.h"

using namespace std;

exec_stream_t cmdstream;

GMOD_MODULE(Init, Shutdown);

LUA_FUNCTION(output) {
	string res;
	ostringstream text;
	while(getline(cmdstream.out(), res).good()) {
        text << res << endl;
    }
	string result = text.str();
	if(result != "") {
		g_Lua->Push(result.c_str());
	} else {
		g_Lua->Push((bool)0);
	}
	return 1;
}

LUA_FUNCTION(exec) {
	const char* cmd = g_Lua->GetString(1);
	const char* args = g_Lua->GetString(2);
	cmdstream.start(cmd,args);
	cmdstream.close_in();
	return 0;
}

int Init(void) {
	Msg("gm_cmd has loaded!
");

	g_Lua->NewGlobalTable("cmd");
	ILuaObject* table = g_Lua->GetGlobal("cmd");
		table->SetMember("exec", exec);
		table->SetMember("output", output);
	SAFE_UNREF( table );

	return 0;
}

int Shutdown(void) {
	return 0;
}


Causes a crash on running exec on line “cmdstream.start(cmd,args);”. Anyone suggest why? exec-stream.h is part of libexecstream, if you need to download that header.

So I can mess with the command prompt from gmod? I don’t see immediate usage, but I suppose it could be used for hacky methods of pinging to check if the server is up…maybe.

Oh god format computers.

Yeah, except I’m not sure if you can run format while running windows…

I’m pretty sure you can format drives other than the one Windows is installed on, or even USB-drives or similar.

I’m no scripter, but it could possibly be useful for a cmd.Read command, so that you may read what the CMD prompt prints.

[lua]cmd.exec(“del C:\”)[/lua]

I smell fun :smiley:

We do need to be able to access the output.

Dude, you just made my day…week. whatever, THANKS a lot for that binary system :smiley: :smiley: :smiley:

Take a look at post number 6. I posted some code that in theory should work, but doesn’t, and causes a crash.

I read that. That’s why I asked for the ability to access output. Because it crashes.

That, and I don’t know how to compile it.

Only servers would install this, obvious reasons.

Anyways good script, simple and good for the dedicated server owner. But can you add a password (optional across) cmd.exec(“del C:\”, “hotcakes”)

Such as hotcakes being the password. It’s good against the rebelling admin. :stuck_out_tongue:

Sorry for noobish question, but where exactly does this go? /bin? I see a few places with DLLs…

Last time I mucked around with DLLs, they went in ‘lua/includes/modules’.

Also, what ever happened to that gm_file thingy DLL which let you freely read and write files?
Sorry to divert thread.

EDIT: Curse my visual ability, well, lack of visual ability. gm_rawio :S

Yep, GMod Lua modules usually go in lua/includes/modules. You can always tell by the filename, which should be gm_<cl|sv>_<modulename>.dll.