0

Im trying to change the password with python on a router with a linux OS. The problem is im stuck in the input field which i've to complete with return.

My code looks like this:

root_password_bytes = subprocess.check_output(["pwgen", "-1", "32"])
root_password = root_password_bytes.decode('utf-8')

#root_password = subprocess.check_output(["pwgen", "-1", "32"])
#print(f'printing root passwd: {root_password}')

# Try 1: with decode and no quotation marks
subprocess.call([f"ssh [email protected] printf {root_password}\n{root_password}\n | passwd"], shell=True)
# error: Syntax error: "|" unexpected

# Try 2: with decode and with quotation marks
subprocess.call([f"ssh [email protected] printf '{root_password}\n{root_password}\n' | passwd"], shell=True)
# error: it tries to execute the command on my machine and not on the router

# Try 3: WITHOUT decode and no quotation marks
#subprocess.call([f"ssh [email protected] printf {root_password}\n{root_password}\n | passwd"], shell=True)
# error: Syntax error: "|" unexpected

# Try 4: WITHOUT decode and with quotation marks
#subprocess.call([f"ssh [email protected] printf '{root_password}\n{root_password}' | passwd"], shell=True)
# error: it tries to execute the command on my machine and not on the router
# passwd: Bearbeitungssfehler des Legitimierungszeichens

# Try 5: Without passwd as var
subprocess.call([f"ssh [email protected] echo 'onetwothree\nonetwothree\n' | passwd"], shell=True)
# error: it tries to execute the command on my machine and not on the router
# passwd: Bearbeitungssfehler des Legitimierungszeichens
subprocess.call([f"ssh [email protected] 'printf onetwothree\n' | passwd"], shell=True)

# Try 6: different quotation marks most success it types the first password in
# but after using new-line (\n) the pipe will be unexpected
subprocess.call([f"ssh [email protected] 'printf onetwothree123 | passwd'"], shell=True)
# so this will not work
subprocess.call([f"ssh [email protected] 'printf onetwothree123\nonetwothree123\n | passwd'"], shell=True)
0

2 Answers 2

2

You run 2 different and unrelated sub processes, they don't share input/output. You should either use communicate() to send characters to the running process (also see examples at the bottom of the doc).

Or, you can modify the command to contain the password so you don't have to communicate with it: ssh root@ip "echo 123 | passwd --stdin root"

Sign up to request clarification or add additional context in comments.

2 Comments

i have edited the code at the 1. post
you missed the --stdin, also notice the quotes "" around the echo | passwd
0

I now use the following solution

    root_password_bytes = subprocess.check_output(["pwgen", "-1", "32"])
    root_password = root_password_bytes.decode('utf-8')

    cmds = [
        f' rm -f /etc/dropbear/authorized_keys\n',
        f' printf "{root_password}{root_password}" | passwd\n',
        f' reboot'
    ]

    proc = subprocess.Popen(["ssh", "[email protected]"], stdin=subprocess.PIPE)
    for cmd in cmds:
        proc.stdin.write(f'{cmd}'.encode())
    proc.stdin.flush()
    proc.stdin.close()
    proc.wait()

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.