abababab 发表于 2023-4-29 00:30:04

python subprocess 执行 shell问题

python subprocess 需要以 1004 用户执行 shell,返回数据

但在linux上运行,报错
result2 <Popen: returncode: None args: ['s', 'h', ' ', ...
不知道subprocess怎么用了?请教大佬,感谢!


import subprocess,time,os

def get_shell():

    test1 = "123"
    os.setuid(1004)

    command_test = "sh test.sh "+test1

    print("command_test",command_test)
   
    command_test2 = "sh test2.sh "

    print("command_test2",command_test2)
   
    result2 = subprocess.Popen(args=command_test, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    print("result2",result2)
   
    time.sleep(3)
    result3 = subprocess.Popen(args=command_test2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    print("result3",result3)



get_shell()

Anonymous 发表于 2023-4-29 00:34:47

根据您提供的信息,可能是由于没有足够的权限执行命令。在Linux中,只有具有足够权限的用户才能执行某些命令。

试试使用sudo命令来提高权限并尝试重新运行代码。

另外,您可能需要确认一下test.sh和test2.sh文件是否存在以及包含所需的权限。

以下是一个例子,使用sudo命令并添加-e参数以保留环境变量:

import subprocess,time,os

def get_shell():
    test1 = "123"

    command_test = "sudo -u 1004 -E sh test.sh "+test1

    print("command_test",command_test)

    command_test2 = "sudo -u 1004 -E sh test2.sh "

    print("command_test2",command_test2)

    result2 = subprocess.Popen(args=command_test, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    print("result2",result2)

    time.sleep(3)
    result3 = subprocess.Popen(args=command_test2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

    print("result3",result3)

get_shell()

isdkz 发表于 2023-4-29 00:35:59

根据您的描述,您希望以 1004 用户执行 shell 脚本并返回数据。我注意到您已经尝试使用os.setuid(1004)设置用户 ID,但在使用subprocess.Popen时可能仍然存在一些问题。为了解决这个问题,您可以使用preexec_fn参数在子进程中设置用户 ID。以下是一个修改过的示例代码:

import subprocess
import time
import os

def get_shell():
    test1 = "123"

    command_test = "sh test.sh " + test1
    print("command_test", command_test)

    command_test2 = "sh test2.sh "
    print("command_test2", command_test2)

    # 定义一个函数用于设置用户 ID
    def set_user_id():
      os.setuid(1004)

    # 使用preexec_fn在子进程中设置用户 ID
    result2 = subprocess.Popen(args=command_test, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=set_user_id)
    print("result2", result2)

    time.sleep(3)

    result3 = subprocess.Popen(args=command_test2, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, preexec_fn=set_user_id)
    print("result3", result3)

get_shell()


在这个示例中,我定义了一个set_user_id函数来设置用户 ID,然后在subprocess.Popen中使用preexec_fn参数在子进程中执行它。这样,在执行 shell 命令时,它将以 1004 用户身份运行。

请注意, preexec_fn参数在 Windows 平台上不可用,因此此解决方案仅适用于 Linux 和类 Unix 系统。

isdkz 发表于 2023-4-29 00:43:13

匿名者 发表于 2023-4-29 00:34
根据您提供的信息,可能是由于没有足够的权限执行命令。在Linux中,只有具有足够权限的用户才能执行某些命 ...

不是抵制GPT吗?{:10_256:}

Anonymous 发表于 2023-4-29 00:45:16

isdkz 发表于 2023-4-29 00:43
不是抵制GPT吗?

https://fishc.com.cn/static/image/smiley/ARU/aru-1x-1_037.png
蹲点ing...

isdkz 发表于 2023-4-29 00:49:38

匿名者 发表于 2023-4-29 00:45
蹲点ing...

原来你等不及兑换vip是为了这个{:10_256:}

liuhongrun2022 发表于 2023-4-29 07:59:12

匿名者 发表于 2023-4-29 00:45
蹲点ing...

同上

Anonymous 发表于 2023-4-29 09:12:00

isdkz 发表于 2023-4-29 00:49
原来你等不及兑换vip是为了这个

你猜我是谁

isdkz 发表于 2023-4-29 09:13:55

匿名者 发表于 2023-4-29 09:12
你猜我是谁



都不用猜{:10_256:}

Anonymous 发表于 2023-4-29 09:14:27

isdkz 发表于 2023-4-29 09:13
都不用猜

刚才忘记匿名了,赶紧编辑的{:10_245:}

Anonymous 发表于 2023-4-29 09:14:59

isdkz 发表于 2023-4-29 00:43
不是抵制GPT吗?

你是怎么这么快猜到的

dolly_yos2 发表于 2023-4-29 09:15:41

从您的说明来看,您希望以用户1004的身份执行可执行文件并返回结果。我推测您将在符合 POSIX 标准的系统上运行这个程序。
在您的程序中,我发现了如下的问题,让我们来一一确认。
首先,您似乎尝试打印一个 Popen 对象作为其表示的进程的运行结果,这是不恰当的。您应该访问 result2 的 returncode 属性来获取其运行的返回值,或者从 result2 的 stdout 和 stderr 类文件对象中读取输出信息。请注意这可能需要首先使用 wait 或类似方法等待进程结束。
其次,使用字符串作为命令行向 Popen 传递的方式不值得推荐,因为在拼接中可能出现转义等问题。您可以试试使用参数序列传递要执行的程序和参数,如传递一个列表,其中第一个元素是要执行的程序名,之后各个元素依次为各个参数。
最后,Popen 提供了可选参数 user 来以另外的用户身份运行子进程,可以更方便的进行用户切换且不影响当前进程。您可以试试用这一参数替代 os.setuid。
请注意,切换用户操作可能需要对应的能力或以根用户身份运行程序。另外,由于问题也可能出现在要执行的程序中,由于不知道其具体内容,我无法就此给出建议。
希望这些建议对您有帮助。如果仍有问题欢迎继续向我提问。

isdkz 发表于 2023-4-29 09:47:19

匿名者 发表于 2023-4-29 09:14
你是怎么这么快猜到的

就我们几个用GPT,你之前还老探讨匿名的问题,然后又刚开了vip,不是你还会是谁{:10_256:}

Anonymous 发表于 2023-4-29 09:48:31

isdkz 发表于 2023-4-29 09:47
就我们几个用GPT,你之前还老探讨匿名的问题,然后又刚开了vip,不是你还会是谁

gpt不能多用,刚好五一放假,我可得写作文。。。
偶尔可以用吧,不会产生沉迷

abababab 发表于 2023-4-29 15:24:24

匿名者 发表于 2023-4-29 00:34
根据您提供的信息,可能是由于没有足够的权限执行命令。在Linux中,只有具有足够权限的用户才能执行某些命 ...

大佬来了,这么热闹?

test.sh 这脚本 su 到1004用户后,是可以正常执行的。
就是用python执行不了

abababab 发表于 2023-4-29 15:33:52

isdkz 发表于 2023-4-29 00:35
根据您的描述,您希望以 1004 用户执行 shell 脚本并返回数据。我注意到您已经尝试使用os.setuid(1004)   ...

result2 <Popen: returncode: None args: ['s', 'h', ' ', ...

大佬,还是这报错呢?

isdkz 发表于 2023-4-29 15:45:18

abababab 发表于 2023-4-29 15:33
result2

这个可能跟你的 test.sh 有关,看着不像是 python 的问题

abababab 发表于 2023-4-29 15:53:15

isdkz 发表于 2023-4-29 15:45
这个可能跟你的 test.sh 有关,看着不像是 python 的问题

不会吧,单独运行test.sh,是正常的呢?

isdkz 发表于 2023-4-29 16:04:04

abababab 发表于 2023-4-29 15:53
不会吧,单独运行test.sh,是正常的呢?

你确定你那里用的是这个python代码?感觉你的那个报错里面的args是把整个命令分割成一个一个的字符序列了
页: [1]
查看完整版本: python subprocess 执行 shell问题