import pathlib
import pexpect
import shlex


password = pathlib.Path("/home/aaron/.secrets/beget-sftp-password.txt").read_text().strip()
php = r"""
foreach ([299, 694] as $page_id) {
    $html = (string) get_field("block_6", $page_id);
    echo "\n===== PAGE " . $page_id . " " . get_the_title($page_id) . " =====\n";
    echo mb_substr($html, 0, 24000);
}
"""
remote = (
    "php8.2 /usr/local/bin/wp-cli.phar "
    "--path=/home/m/max9581h/max9581h.beget.tech/public_html "
    "eval "
    + shlex.quote(php)
    + " 2>/dev/null"
)
command = (
    "ssh -o StrictHostKeyChecking=accept-new "
    "max9581h_aaronseo@max9581h.beget.tech "
    + shlex.quote(remote)
)
child = pexpect.spawn("/bin/bash", ["-lc", command], encoding="utf-8", timeout=120)
match = child.expect(["[Pp]assword:", pexpect.EOF, pexpect.TIMEOUT])
if match == 0:
    child.sendline(password)
    child.expect(pexpect.EOF)
print(child.before)
