Results 1 to 4 of 4
I am trying to bacj up files from one host to another using a bash script. I have set up ssh keygen so I don't have to use passwords. At ...
- 04-13-2008 #1Just Joined!
- Join Date
- Sep 2006
- Posts
- 5
trouble using ssh in scripts
I am trying to bacj up files from one host to another using a bash script. I have set up ssh keygen so I don't have to use passwords. At first I just set up 2 scp commands to copy the directories to the backup host. That worked the first time, but it appears that scp will not work when it is run after that. It does not overlay the directories created by the first run.
So I decided to ssh to the backup machine and use rm to get rid of the directories each time I run the script. Unfortunately, it looks like the ssh command works and I get a shell in the remote host, but then subsequent commands don't run. I have attached the script below, any help would be appreciated. The distro for the host that the script is running on is Debian.
Mike
#!/bin/sh
ssh xxx@yyy.net
rm -r -f /user/filea
rm -r -f /user/fileb
exit
scp -r /var/lib/filea xxx@yyy.net:/user/filea
scp -r /var/fileb xxx@yyy.net:/user/fileb
- 04-13-2008 #2Linux Enthusiast
- Join Date
- Apr 2004
- Location
- UK
- Posts
- 658
I think you want to do it like this
The bit in the quotes is the command to be executed remotely. The quotes aren't strictly required, but I used them to get both rm commands to run through a single ssh session.Code:#!/bin/sh ssh xxx@yyy.net "rm -r -f /user/filea && rm -r -f /user/fileb" scp -r /var/lib/filea xxx@yyy.net:/user/filea scp -r /var/fileb xxx@yyy.net:/user/fileb
Let us know how you get on,
Chris...To be good, you must first be bad. "Newbie" is a rank, not a slight.
- 04-13-2008 #3Linux Engineer
- Join Date
- Feb 2005
- Posts
- 1,044
would do both directories with a single command.Code:"rm -rf /user/file[ab]"
And if you take the "exit" out of the script you've listed the scp commands will run.
- 04-15-2008 #4Just Joined!
- Join Date
- Sep 2006
- Posts
- 5
I used Chris's suggestion and it is working now.Thanks, guys.


Reply With Quote