rewrite to run commands in one ssh connection instead of multiple