How to run commands on remote server using ssh password less authentication?

Hi,

I need to run a script located in a directory on remote server by using ssh authentication from my local unix server. Can anyone help me in this.

I have tried the below command. It worked for echo command but when i tried to open a file using cat command it is showing "cat: cannot open the <filename>". I have given the required permissions as well.

ssh <username>@<ipaddress> cd <path>;cat <filename>

thanks in advance.

Have you tried putting those commands into a script?

ie:

echo "cd <path>" >file1.ksh
echo "cat <filename>" >> file1.ksh
chmod +x file1.ksh

make sure it's owned by <username>

then try again:

ssh <username>@<ipaddress> <path_to_file1.ksh>/file1.ksh

??

1 Like

Did the cd <path> succeed, and does <filename> exist in that path?

The problem here is that the cat is running locally. Your local shell is seeing the semicolon as a command separator between the ssh command and the cat command. Try quoting the command string to send it to the remote side eg:

ssh <username>@<ipaddress> "cd <path>;cat <filename>"

or

ssh <username>@<ipaddress> cd <path>\;cat <filename>

If you have a more complex command e.g. involving awk I'd suggest a here-doc:

ssh <username>@<ipaddress> <<"EOF"
    cd /var/log/httpd
    awk '/Invalid method/ { print $5} ' error_log
EOF
1 Like

Have you tried
ssh user@host cat /path-to-file/file ?

1 Like

When i tried the commands above I am getting the below error

"cannot open <filename>"

Is it that there will be no permission to execute/open files using commands on remote server?

Remote server will have same permissions to the filesystem that the "username" user has.

Why not try running the command from an interactive shell you can then debug/sort out required permissions:

$ ssh username@ipaddress
Last login: Fri Jan 30 16:19:27 2015 from your-pc.your.domain

ipaddress $ cat /path-to-file/file
This is the file contents

ipaddress $ exit
Connection to ipaddress closed.

$