Unable to run command after ssh

Hello,

I am trying to create a ksh script to login to server and collect gather output of some command to troubleshoot some issue.

DATE=`date +%b.%d.%Y.%M.%H`
echo " Enter emp id to login to server"
read Eid
Eid=$Eid
echo " Enter hostname of the system"
read HOST
HOST=$HOST
/usr/bin/ssh $Eid@$HOST   <===== It is working upto here
OSVALUE=`uname -s|tail -1`
if [ $OSVALUE = Linux ]
then
/usr/bin/sudo su - root
else
/prod/prop/tools/bin/sudo su - root
fi

issue) My script is working upto marked arrow and it is prompting me for password and i am able to login to server, BUT command after that are not running.

pls gelp

I am beginner in scripting. This is ksh

If you want to run some commands on a remote machine using ssh, then you have to use below syntax:-

ssh $user@$host "command1; command2; ..."

E.g.

ssh $Eid@$HOST "uname; ls"

running 'ssh' does not feed the rest of the script into ssh. It's a separate shell on a completely different system -- it's not reading your script.

If you want to feed a script into it, you have to feed a script into it. You can use what's called a "here-document" to effectively feed a file full of text into a program:

MYVAR="asdf"
ANOTHERVAR="qwertyuiop"

ssh username@host exec /bin/sh -s "$MYVAR" "$ANOTHERVAR" <<"EOF"

MYVAR="$1"
ANOTHERVAR="$2"

echo "MYVAR is $MYVAR"
echo "ANOTHERVAR is $ANOTHERVAR"

OSVALUE=`uname -s|tail -1`
if [ $OSVALUE = Linux ]
then
/usr/bin/sudo su - root
else
/prod/prop/tools/bin/sudo su - root
fi
EOF

That's a complicated but thorough way to do what bipinajith is doing, except with complete, unaltered, unescaped scripts.

Note the ending EOF must be at the very beginning of the line, do not indent.

If it was <<EOF instead of <<"EOF", the shell would substitute variables inside for you. That's bad -- $OSVALUE would become a blank string immediately, before it even gets sent to the other side.

Also note that variables don't cross. It's a new program on another system, it won't know your variables if you don't tell them it. Note how I pass two variables into the remote script. You can have more of those, and they can be anything you want. They become $1, $2, and so forth.