> shell_cmd = 'ls -l | grep mytest.log' > child = pexpect.spawn ('ssh my at mycomp2') I think you can give the ssh command an option to execute a file remotely. That way, one command would be enough.