无法使用子进程执行sudo命令

时间:2020-06-09 13:14:01

标签: python subprocess sudo tee

我正在尝试通过使用Python子进程编辑intel_backlight文件夹中的亮度文件来更改亮度。

无法使用sudo priv运行脚本。

几乎尝试了所有一切,但还是没有运气... 关于我要去哪里,请您帮我一下。

尝试以下操作:

  1. 使用xbacklight包不起作用。

  2. xrandr的颜色变暗,但亮度变暗。

  3. 更改文件的值(使用sudo在shell中工作,但希望通过使用sudo调用python文件通过python进行操作。)

command = "sudo  echo " + "'" + str(new_brightness_level) + "'" + ">" + "/sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/brightness"
    subprocess.Popen(command,shell=True)

command2 = "echo {} | sudo -S   tee /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/brightness ".format(new_brightness_level).split()

 c2 =subprocess.Popen(command2,shell=True,stdin=subprocess.PIPE,universal_newlines=True)
        res = c2.communicate(sudo_pass + '\n')[1]
command = "echo " + "'" + str(new_brightness_level) + "'" + "sudo -S > /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/brightness"
cmd2 = subprocess.Popen(['sudo', '-S'] + command.split(),stdin=subprocess.PIPE,stdout=subprocess.PIPE,stderr=subprocess.PIPE)

感谢您的提前帮助!

1 个答案:

答案 0 :(得分:0)

尝试了不同的方法,终于找到了可行的解决方案:

command3 =  "echo {} | tee /sys/devices/pci0000:00/0000:00:02.0/drm/card0/card0-eDP-1/intel_backlight/brightness ".format(new_brightness_level)
command4 =  " sudo -S  bash -c '{}' ".format(command3)
c1 =subprocess.Popen(command4,shell=True,stdin=subprocess.PIPE,universal_newlines=True)
res = c1.communicate(sudo_pass + '\n')[1]

在命令3中创建了没有Sudo的命令

在命令4中首先用-S添加了sudo(用于从 通讯功能),然后使用了将作为sudo执行的bash shell,因此所有命令都将使用sudo

运行

请纠正我,并在有任何感觉的情况下添加更多信息 非常感谢