Python 将命令作为子流程中的第一个命令运行

Python 将命令作为子流程中的第一个命令运行,python,python-2.7,command,subprocess,Python,Python 2.7,Command,Subprocess,我有一个打开终端的功能: def open_next_terminal(): import subprocess def rand(path="/tmp"): acc = string.ascii_letters retval = [] for _ in range(7): retval.append(random.choice(acc)) return "{}/{}.sh".format(

我有一个打开终端的功能:

def open_next_terminal():
    import subprocess

    def rand(path="/tmp"):
        acc = string.ascii_letters
        retval = []
        for _ in range(7):
            retval.append(random.choice(acc))
        return "{}/{}.sh".format(path, ''.join(retval))

    file_path = rand()
    with open(file_path, "a+") as data:
        data.write(
'''
#!/bin/bash
"$@"
exec $SHELL
'''
        )
    subprocess.call(["sudo", "bash", "{}".format(file_path)])
    return file_path
我想在这个新打开的终端中运行一个命令,然后再对其执行任何操作。例如:

subprocess.call(["sudo", "bash", "{}".format(file_path)]) #<= is called
ls #<= is run
 #<= some output of files and folders
root@host:~#  #<= the shell is now available

subprocess.call([“sudo”、“bash”、“{}.format(file_path)])#只需将
shell=True
参数传递给
subprocess.call
,您就可以作为单个字符串运行多个命令(由分号或换行符分隔)

subprocess.call('your_first_command.sh; your_real_work.sh', shell=True)

只有当您的命令字符串来自用户输入时,才可能进行命令注入。即使它是从用户输入中派生出来的,你也可以用它来转义用户输入中的部分。所以除此之外,没有办法运行“第一个命令”?不幸的是,我想不出来。