嗨,我正在寻找一个 bash 或 pyton 循环来运行 10-20 个命令,每次需要 root 密码时,它都会传递硬编码密码

问题描述 投票:0回答:0

嗨,所以尝试了一些事情,比如

蟒蛇:

import subprocess

commands = ['sudo -s', 'cd /', 'ssh user@host']

password = 'myRootPassword'

for command in commands:
    process = subprocess.Popen(command.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)

    # Check if the command prompts for a password
    output, error = process.communicate()
    if 'password' in output.decode('utf-8').lower():
        # Send the password to the process
        process.stdin.write(password.encode('utf-8'))
        process.stdin.write(b'\n')
        process.stdin.flush()

    # Execute the command
    output, error = process.communicate()
    print(output.decode('utf-8'))
    print(error.decode('utf-8'))

此代码会引发错误

    process.stdin.write(password.encode('utf-8'))
    process.stdin.write(b'\n')
    process.stdin.flush()

试过上面的代码,但它抛出错误 ValueError

python bash loops subprocess command
© www.soinside.com 2019 - 2024. All rights reserved.