I have to transfer a file from server A to B and then needs to trigger a script at Server B. Server B is a Load balance server which will redirect you either Server B1 or B2 that we dont know.
I have achieved this as below.
ssh user@Server "script.sh"
ssh user@server "Command for sftp; sh script.sh"
if you're just copying a file up and then executing a script, and it can't happen as two separate commands you can do:
gzip -c srcfile | ssh user@remote 'gunzip -c >destfile; script.sh'
gzips srcfile, sends it through ssh to the remote end,
gunzips it on that side, then executes
If you want more than one file, you can use
tar rather than gzip:
tar czf - <srcfiles> | ssh user@remote 'tar xzf -; script.sh'
if you want to get the results back from the remote end and they're files, you can just replicate the tar after the script…
tar czf - <srcfiles> | ssh user@remote 'tar xzf -; script.sh; tar czf - <remotedatafiles>' | tar xzf -
i.e. create a new pipe from ssh back to the local environment. This only works if
script.sh doesn't generate any output. If it generates output, you have to redirect it, for example to /dev/null in order to prevent it messing up the tar:
tar czf - <srcfiles> | ssh user@remote 'tar xzf -; script.sh >/dev/null; tar czf - <remotedatafiles>' | tar xzf -