Home > Back-end >  Shell script to login to remote VM and run commands from remote VM
Shell script to login to remote VM and run commands from remote VM

Time:07-15

I need to login to a remote VM and run some commands. I am able to login successfully but echo command does not return list of files from the remote VM instead it returns output from local machine. Can anyone suggest how can I achieve this?

ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<EOF
cd /var;
echo "$(ls)";
EOF
exit

CodePudding user response:

It worked after removing echo. PFB solution:


    ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1 << EOT
    cd /var;
    ls -ltr;
    EOT
    exit

CodePudding user response:

You have to escape $ in EOF sequence like this:

ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<EOF
cd /var;
echo "\$(ls)";
EOF

Or escape whole EOF sequence like this:

ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<'EOF'
cd /var;
echo "$(ls)";
EOF

CodePudding user response:

alternatively, if you quote the marker, then substitution doesn't take place in the local shell:

ssh -o IdentitiesOnly=yes -o StrictHostKeyChecking=no root@$1<<'EOF'
cd /var;
echo "$(ls)";
EOF
exit
  • Related