嗨,所以尝试了一些事情,比如
蟒蛇:
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