Copy a file on remote server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Copy a file on remote server
# 1  
Old 06-30-2008
Copy a file on remote server

I have ssh keys setup and running properly between two servers. I have a Korn shell script that is logging into the remote server and needs to backup the authorized_keys and/or authorized_keys2 files.

The following syntax works perfectly
-------------------------------------
ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; cp -p authorized_keys authorized_keys.orig"

Now, what I want to do is actually check for the existence of authorized_keys or authorized_keys2 (because either or both could be on the system). I am attempting this using the following code:

ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; if [[ -f authorized_keys ]]; then cp -p authorized_keys authorized_keys.orig fi"

When I run the command above, I get the following error:

ksh: syntax error at line 1 : `then' unmatched


I have tried the command both with and without the brackets, etc. and nothing seems to work. Can anyone help me with getting this command to run remotely over ssh from ServerA against ServerB ?

Thanks much for any assistance on this Smilie
sunsysadm2003
# 2  
Old 06-30-2008
I think I just figured out the answer to my own question. I came up with the following, if someone else has a better method, please feel free to post, otherwise, hopefully this will help others out.

ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; if [[ -f authorized_keys ]]; then "cp -p authorized_keys authorized_keys.orig" ; fi"

ssh ${LOGINUSER}@${HOSTNAME} "cd ~/.ssh; if [[ -f authorized_keys2 ]]; then "cp -p authorized_keys2 authorized_keys2.orig" ; fi"
sunsysadm2003
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to copy files from remote server to local?

Hi experts, I 'm newbie to unix world, now I have task to copy the latest files from remote server to my local. I believe this must be very common request in this community. I want you do it one more time for me please. My requirement is something like this: I receive files in the below... (3 Replies)
Discussion started by: parpaa
3 Replies

2. UNIX for Dummies Questions & Answers

Checking files in remote server and decide to copy file or not

Hi there, I have a problem in my script, I need to check whether file exists in remote server or not, if the file exists, then stop copy else copy the file to the server.. my code is something like this while read $server do if ssh $server "cd $directory_name; if ; then echo "Error:... (2 Replies)
Discussion started by: beezy
2 Replies

3. Shell Programming and Scripting

Copy and run that script in Remote server

Hi All, I need script to perform below task. 1. I have a script in one server and need to copy this script to remote server 2. login in to remote server 3. run the script which i copied to this server. #!/bin/bash read a scp /tmp/script.sh user@hostname:/tmp ssh user@$a ./scirpt.sh ... (2 Replies)
Discussion started by: bapu1981
2 Replies

4. UNIX for Advanced & Expert Users

How to copy a file from remote server and preserve timestamp

Hi How to copy a file from remote server and preserve timestamp. Please not, i have to pass username and password to connect to the remote server in the shell script. Can this be achieved with simple ftp ? are there any options in ftp ? Thanks (4 Replies)
Discussion started by: skumar75
4 Replies

5. UNIX for Dummies Questions & Answers

Can I copy files on remote server with ftp ?

I just realize the only way is to download and upload again.. is not possible to copy them remotely with the ftp protocol ? thanks (2 Replies)
Discussion started by: aneuryzma
2 Replies

6. UNIX for Dummies Questions & Answers

Copy files from remote server

Hi Friends, Could you please help me as per my requirement mentioned below ? I have to copy files from one unix server to another unix server, and the files that i need to copy from the remote server are only those which are modified/created Today from abc directory on the remote server (1 Reply)
Discussion started by: ramask
1 Replies

7. UNIX for Advanced & Expert Users

How to copy folder from one server to remote one using SSH

ok guys. I have a Centos server running a large website I own. Its is severely struggling under the load and I am having to move to a new load balanced setup. The servers are both remote (to me) and although I can do most things admin wise in nix, I am struggling a bit to come up with a way... (1 Reply)
Discussion started by: anderow
1 Replies

8. Shell Programming and Scripting

copy the latest file in the remote server's directory

Hi Expert Team, I performed the below piece of code to copy the latest file in the remote server's directory to the same server's other directory. But it is not working properly. How can i handle this? Can you please help me..? ssh ${REMOTE_USERID}@${REMOTE_HOSTNAME} "cp -p `ssh... (3 Replies)
Discussion started by: spkandy
3 Replies

9. Shell Programming and Scripting

copy files from remote server (B) to target server (A)?

Hi All, what is the comand to log off the remote server? I have 2 servers A, B. I need to find all files older than 7 days on server B and copy over to server A. My logic is: login the remote server: ================= ssh hostB cd /data/test find . -mtime -7 -ls | awk '{print... (4 Replies)
Discussion started by: Beginer0705
4 Replies

10. UNIX for Advanced & Expert Users

Commands to copy a tar.gz file from a Remote Unix Server to Local Desktop.

Hi, Just wanted to know, how can I ftp/transfer/copy a (design.tar.gz) archive from a Unix Server (sdmc222.sdmc.cp-srv.com) which is at a remote location, to my Windows Desktop. Obviously, it is not possible at cmd prompt on my Windows using the following commands :- ftp... (3 Replies)
Discussion started by: marconi
3 Replies
Login or Register to Ask a Question