我想在python中使用ctypes.execve调用外部脚本。我已经弄清楚了如何调用脚本以及如何传递参数列表。 但是正如您所看到的,列表或参数是静态的,我无法弄清楚如何动态地做到这一点。
参数列表应为python列表
例如:argv_list = ["./tmp/arg_test.sh", "arg_1", "arg_2"]
需要以某种方式转换此python列表以匹配char * const argv[]
,以便将其传递给execve。 <-这就是我在努力的地方。
cmd = "./tmp/arg_test.sh"
cmd_byte = cmd.encode('utf-8') # create byte objects from the strings
### works ### ### works ### ### works ### ### works ###
argv_0 = "argv_0"
argv_0_byte = argv_0.encode('utf-8')
argv_1 = "argv_1"
argv_1_byte = argv_1.encode('utf-8')
argv_2 = "argv_2"
argv_2_byte = argv_2.encode('utf-8')
argv_3 = "argv_3"
argv_3_byte = argv_3.encode('utf-8')
b_argv = (ctypes.c_char_p * 5)(argv_0_byte, argv_1_byte, argv_2_byte, argv_3_byte)
### works ### ### works ### ### works ### ### works ###
# int execve(const char * filename, char * const argv[], char * const envp[]);
libc.execve(ctypes.c_char_p(cmd_byte), b_argv, 0)
谢谢!
答案 0 :(得分:0)
这是一种动态生成参数的方法。这是针对Windows msvcrt
编写的,但也应适用于libc
:
test.cpp
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char* argv[])
{
for(int i = 0; i < argc; ++i)
printf("argv[%d] = \"%s\"\n",i,argv[i]);
printf("TEST1=%s\n",getenv("TEST1"));
printf("TEST2=%s\n",getenv("TEST2"));
return 0;
}
test.py
from ctypes import *
dll = CDLL('msvcrt')
dll._execve.argtypes = c_char_p,POINTER(c_char_p),POINTER(c_char_p)
dll._execve.restype = c_ssize_t
cmd = b'test.exe'
args = [b'arg1',b'arg2',b'arg3']
env = [b'TEST1=hello',b'TEST2=goodbye']
# Convert Python lists to c_char_p arrays.
# Arrays should be null-terminated so adds None to the end.
cargs = (c_char_p * (len(args) + 2))(cmd,*args,None)
cenv = (c_char_p * (len(env) + 1))(*env,None)
dll._execve(cmd,cargs,cenv)
输出:
argv[0] = "test.exe"
argv[1] = "arg1"
argv[2] = "arg2"
argv[3] = "arg3"
TEST1=hello
TEST2=goodbye