[Python] Using python wexpect to run powershell command and start connect to a Linux AWS...

Discussão em 'Python' iniciado por Stack, Julho 23, 2025.

  1. Stack

    Stack Membro Participativo

    I want to using python to connect to an AWS Linux instance and run some code. Here is my chain of commands ran in a windows terminal:

    powershell "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force; H:\\\\scripts\\\\workstation\\\\headnode.ps1 -myUser username"
    sudo su - ubuntu
    sbatch -J job.sbatch


    This works but I cannot manage to reproduce with python, I've tried

    import expect
    child = wexpect.spawn('powershell "Set-ExecutionPolicy -Scope Process -ExecutionPolicy Bypass -Force; H:\\\\scripts\\\\workstation\\\\headnode.ps1 -myUser username"')

    # Wait for Linux prompt
    child.expect([r'\$', r'#', 'ubuntu@'], timeout=60)

    # Run the commands
    child.sendline('sudo su - ubuntu')
    child.expect([r'#', 'ubuntu@'], timeout=30)
    child.sendline('sbatch -J job.sbatch')


    This give a EOF error here: child.expect([r'$', r'#', 'ubuntu@'], timeout=60)

    Continue reading...

Compartilhe esta Página