Refactor the code in remotecommand to better represent the structure of what is common between portforward and exec.