Thanks a lot Chubler_XL. This worked perfect for me but my doubts seems to have no end
Now after I can login to server2 and execute code. But After I login to server2 I have to sudo as another user (because only this user has permission to execute commnds I want to run).
I tried to add the sudo command in code after line
chmod +rx temp_script.sh
But it prompts for password and do sudo as the user and stops there. It is not executing next lines after it does sudo.
How can I continue executing the commands after I sudo as different user than what I have logged in as in ssh in first line of code?
ssh user1@server2 "cat > temp_script.sh <<temp_script_EOF