pexpect not executing command by steps
<p>I have this Python3 code which use Pexpect.</p>

<pre><code>import pexpect
import getpass
import sys

def ssh(username,password,host,port,command,writeline):
    child = pexpect.spawn("ssh -p {} {}@{} '{}'".format(port,username,host,command))
    child.expect("password: ")
    child.sendline(password)
    if(writeline):
        print(child.read())

def scp(username,password,host,port,file,dest):
    child = pexpect.spawn("scp -P {} {} {}@{}:{}".format(port,file,username,host,dest))
    child.expect("password: ")
    child.sendline(password)

try:
    filename = sys.argv[1]
    print("=== sendhw remote commander ===")
    username = input("Username: ")
    password = getpass.getpass("Password: ")
    ssh(username,password,"some.host.net","22","mkdir ~/srakrnSRV",False)
    scp(username,password,"some.host.net","22",filename,"~/srakrnSRV")
    ssh(username,password,"some.host.net","22","cd srakrnSRV &amp;&amp; sendhw {}".format(filename),True)

except IndexError:
    print("No homework name specified.")
</code></pre>

<p>My aim is to:</p>

<ul>
<li>SSH into the host with the <code>ssh</code> function, create the directory <code>srakrnSRV</code>, then</li>
<li>upload a file into the <code>srakrnSRV</code> directory, which is previously created</li>
<li><code>cd</code> into <code>srakrnSRV</code>, and execute the <code>sendhw &lt;filename&gt;</code> command. The <code>filename</code> variable is defined by command line parameteres, and print the result out.</li>
</ul>

<p>After running the entire code, Python prints out</p>

<pre><code>b'\r\nbash: line 0: cd: srakrnSRV: No such file or directory\r\n'
</code></pre>

<p>which is not expected, as the directory should be previously created.</p>

<p>Also, I tried manually creating the <code>srakrnSRV</code> folder in my remote host. After running the command again, it appears that <code>scp</code> function is also not running. The only runnning pexpect coomand was the last <code>ssh</code> function.</p>

<p>How to make it execute in order? Thanks in advance!</p>

<p>You may lack permission for executing commands through ssh. Also there is possibility that your program sends scp before prompt occurs.</p>

python ssh pexpect