Linux – SSH Pipe File and Execute

bashlinuxssh

I am attempting to execute a binary on my ssh server as soon as I log in. However I may want to push the update of my binary with a single ssh login and execute aswell. My current process for do this is

cat exec | ssh user@host 'cat - > exec; chmod +x exec; ./exec'

This does not work as when I hit the accutal execution it appears to just freeze. I get no response from the server after this. The strange thing is once I have uploaded the file I can simply execute

ssh user@host './exec'

and the file is executed just fine. I know it is reaching the execution process of the binary and the chmod is correctly setting the executable bit of the file it just will not execute in a single command. Does anyone have any suggestions.

Best Answer

Use tar instead of cat, and tar preserves mode as well if exec has x bit on.

tar cf - ./exec | ssh user@server 'tar xf -; ./exec'