Skip to content

Instantly share code, notes, and snippets.

@osirisgothra
Created December 9, 2022 19:06
Show Gist options
  • Save osirisgothra/276ebf99eff5dcd90cf5ec92554c114e to your computer and use it in GitHub Desktop.
Save osirisgothra/276ebf99eff5dcd90cf5ec92554c114e to your computer and use it in GitHub Desktop.
Program to run as shim, for windows 10 clients who want to soft-bypass cmd checking from windows, their own scripts
#!/usr/bin/python3
#
# dothat.py (name it what your .cmd file is called, plus .py of course instead!)
#
# simple dirty program to launch a batch command (cmd) file of the same name
# but not same file extension, in the same directory
#
# to use: name this program whatever you're script is called, but use .py of course instead of .cmd
#
# example: this file, saved as "dothat.py" when run from "c:\somewhere\dothat.py" will attempt to run
# this command line:
# c:\windows\system32\cmd.exe /c c:\somewhere\dothat.cmd
#
# notes:
# this directory is first discerned from the path passed with argv[0]. If there is no valid path
# it will fall back to the current directory specified by the operating system
# if the filename.cmd cannot be found, it will simply return an error.
#
# return codes:
#
# 0 - executed OK (does not return error code of program, you will need to get this yourself) or tryexec_override was set to 1 (see below)
# 12 - failed, did not find filename.cmd, and tryexec_override was set to 0
#
# special variables
#
# tryexec_override possible values: 0 = normal execution success and failures
# 1 = try execute even if file is not found, and return no error either way
#
# written by Gabriel Sharp, Dec 9, 2022, 2:04PM
# https://github.com/osirisgothra
# as a gist: gist url =
#
from sys import argv
from os import execv as run, _exit as end
from os.path import dirname, abspath, curdir, exists, splitext, basename, extsep, join, isfile
tryexec_override = 0
thispath = argv[0]
thisdir = dirname(thispath)
abscurdir = abspath(curdir)
if thisdir == "" or not exists(thisdir):
if thisdir == "":
thisdir = "No directory given and therefore it"
print("\"%s\" does not exist, using current directory \"%s\" instead..."%( thisdir, abscurdir ))
thisdir=abscurdir
else:
print("using %s as target directory"%( thisdir ))
thisfile = splitext(basename(thispath))
thisfilename = thisfile[0]
thisfileext = thisfile[1]
thisfileext = thisfileext.replace(extsep,'')
cmdname = 'c:\windows\system32\cmd.exe'
target = join(thisdir,thisfilename + ".cmd")
args = [ '/c' , target ]
if isfile(target) or tryexec_override == 1:
print("Targetting \"%s\" ok"%( target ) )
run(cmdname,args)
end(0)
else:
print("Can not target \"%s\" because it doesnt exist, exiting with fail code 12!"%( target ))
end(12)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment