in reply to System call to ssh stalling.

Why would you expect the loop to exit immediately? The only way to escape the loop is via last, of which you have only one. And that is only encountered if you hit return after being prompted "enter cmd:".