I use the module paramiko to execute the commands in sequence like this:
ssh.connect(IP, username=\'root\', password=\'xxxx\', port=22) #xxxx stands for the passw