5

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 brjones@server.com
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.

user123661
  • 51
  • 1
  • 1
  • 3
  • If you insist on executing commands over ssh you will have to prefix each command with a `ssh user@server exec ` phrase. However, what i want to know is -- are you using the above "get file" example as a reference? and, you have a more complex sequence of commands to execute this way? – nik Jun 16 '09 at 14:20
  • public key authorization can be setup using ssh-keygen on your client and authorized_keys file on the server (i gave a reference link in my answer below). – nik Jun 16 '09 at 14:22

10 Answers10

14

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

RichieHindle
  • 272,464
  • 47
  • 358
  • 399
  • 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? – user123661 Jun 16 '09 at 13:27
  • 3
    scp uses ssh behind the scenes. You can use the -o option to pass whatever parameters you want to ssh (like PreferredAuthentication) – itsadok Jun 16 '09 at 13:43
14

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
  • 509
  • 2
  • 6
5

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 brjones@server.com << EOT
cd ~/folder
echo "hello" > hello.txt
...
EOT
Eric Darchis
  • 24,537
  • 4
  • 28
  • 49
3

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
  • 13,254
  • 3
  • 41
  • 57
  • 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. – user123661 Jun 16 '09 at 13:27
  • 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 Jun 16 '09 at 14:11
  • @brjones, this reference should help, http://www.ssh.com/support/documentation/online/ssh/adminguide/32/Public-Key_Authentication-2.html – nik Jun 16 '09 at 14:15
2

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 brjones@server.com 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
  • 74,820
  • 18
  • 121
  • 166
2

In addition to the answer by Tiemen, of course you can pipe the commands from your local script with:

ssh thehost < commands.sh
1

scp? Remote is an appliance with a read only file system. I don't want to leave the script behind, or use multiple invocations of ssh to clean it up after or multiple invocations to execute it one line at a time. The list goes on...

great solution here by Yarek T:

You might have to play around a bit with quoting or escapes but it gets the job done, with a single connection to the remote and the script remaining local.

Community
  • 1
  • 1
J. Piel
  • 101
  • 1
  • 2
0

Why not use scp?

Nathan Fellman
  • 122,701
  • 101
  • 260
  • 319
laginimaineb
  • 8,245
  • 1
  • 22
  • 14
0

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 --- ex-moderator kitten
  • 98,632
  • 24
  • 142
  • 234
0

If you want to invoke a script on remote host, which present on localhost

ssh remote Password@remoteHostname < localScript.sh

If you want to invoke a script which is already on remote host

ssh remote Password@remoteHostname "$PATH_OF_SCRIPT/remoteScript.sh"

Piyush Baijal
  • 281
  • 4
  • 13