views:

6346

answers:

8

I would like to create a script that automatically logs into a remote server and grabs a file on the server. The script already logs into the server however the commands are not run on that machine. Once I disconnect from the remote server, the commands are run on the client machine.

!/bin/sh
ssh -o PreferredAuthentications=publickey [email protected]
cd ~/folder
"i would like to grab file and copy it to client machines folder"

EDIT: I am not sure if you noticed but I am used a passwordless connection to the remote server using ssh and keygeneration. I appreciate the ideas but I am looking to possibly pipe commands or run commands on the remote server with the script on the client. I realize that the remote server does not have access to the client script, however I was curious if it was possible to pipe the commands through the ssh connection.

+11  A: 

You should be using scp ("secure copy") rather than ssh ("secure shell").

RichieHindle
i am using ssh to connect remotely while not entering a password, if I use scp, it will ask for a password...is there a way to use the scp command with key authorization?
scp uses ssh behind the scenes. You can use the -o option to pass whatever parameters you want to ssh (like PreferredAuthentication)
itsadok
A: 

Why not use scp?

laginimaineb
+1  A: 

ssh in a script doesn't work like that. However it does have the capability to execute a command on a remote server by specifying the command as an argument to ssh, like:

ssh [email protected] do_foo.sh

will run the do_foo.sh script on the server

However for your situation it looks like what you are really looking for is SCP.

Tyler McHenry
+2  A: 

You should use ssh in this way to execute scripts on remote machines,

ssh user@server exec /path/to/script/script.sh

where script.sh is available on the server on the given path from the user login.


If the script is not present on the server but available on your local machine (and you do not have common NFS shared space between the two machines), you should send the script with scp like this,

scp script.sh user@server:/path/to/script/


Now, for your specific case of getting a server file you should just execute,

scp user@server:/path/to/file/filename .

like some other answers already suggest.

nik
I was wondering if you could look at my edit and tell me what you think or if it is possible to use key authorization with SCP.
public key authorization can be done with two steps. (1) use ssh-keygen to create a key pair and then, (2) copy the public key line into the `authorized_keys` file on the server; present in the `~/.ssh/` directory.
nik
@brjones, this reference should help, http://www.ssh.com/support/documentation/online/ssh/adminguide/32/Public-Key_Authentication-2.html
nik
A: 

Several people have already explained how to do the particular example you give better and easier.

If you really do need to script a remote interaction, you might consider using expect.

dmckee
+3  A: 

Also, if you don't want to have a script available on the remote server, try the following:

ssh thehost 'cd /tmp; ls; echo Hello world, I am `hostname`'

and for SCP-less copying:

ssh localhost 'cat /bin/bash' > local_bash
Tiemen
+1  A: 

If your goal is only to transfer files, you should use scp. But to execute some commands on the remote host without having a specific script on that remote host, you can simply use the stdin like this:

!/bin/sh
ssh -o PreferredAuthentications=publickey [email protected] << EOT
cd ~/folder
echo "hello" > hello.txt
...
EOT
Eric Darchis
A: 

Hi all,

I have a similar problem, I´m trying to execute a local script on a remote host.

when I do

ssh user@host < /path/script

everything works fine the script gets executed on the remote host and I get the remote hostname. content of the script is just hostname

But if I activate command=/myuser/bin/ssh_cmd in .authorized_keys

ssh user@host < /path/script

doesen´t work anymore and I have no idea why.

ssh_log.log is empty - only a empty line is logged.

content of /user/bin/ssh_cmd

#!/bin/bash

echo "$SSH_ORIGINAL_COMMAND" >>/tmp/ssh_log.log

case "$SSH_ORIGINAL_COMMAND" in

hostname*) $SSH_ORIGINAL_COMMAND ;; *) echo ERROR: invalid command "${SSH_ORIGINAL_COMMAND}" is not allowed. ;; esac

Keep the scripts on the remote machine is not an option ;)

Any hints ?

TIA izac

izac