Cleaning up a simple bash script

jaydee

Diamond Member
May 6, 2000
4,500
2
81
Very newb-ish question, I'm writing a simple bash script on linux to execute a few commands that are helpful to me. I've got a linux book that's walking me through the individual commands and bash shell scripting. Previously I was using PuTTY to do this, but I'd rather streamline this process via the local terminal. Here's what I'm trying to accomplish:

1. Move a local file to a remote computer (rsync)
2. Remove a symbolic link file on the remote computer (ssh)
3. Re-create the symbolic link on the remote computer (ssh)
4. Restart a service on the remote computer (ssh)

The script is working right now (I have '&&' between each command), but I have to enter the root password four times (once per command) to get through the whole script. How can I execute this script while only having to enter the password once?

The script looks like this:

Code:
rsync [location/file] root@192.168.180.153:/[location/file]  && ssh root@192.168.180.153 rm [location/file] && ssh root@192.168.180.153 ln -s [location/file] [location/sym. link] && ssh root@192.168.180.153 'service [program] restart'

Thanks!
 

xalos

Senior member
May 31, 2002
292
0
71
Generate ssh keys (if you haven't done so already) and run the rsync with ssh options.
 

jaydee

Diamond Member
May 6, 2000
4,500
2
81
Thanks, following your suggestion, I googled "ssh keys terminal" and found instructions, working fine now without any password prompt. I was aiming for one password, but this is a closed network in a lab, security isn't an issue anyway.

Thanks for the help.
 

xalos

Senior member
May 31, 2002
292
0
71
The default ssh keys are fairly secure at default settings. But, you can change the format for the ssh key generation if you're inclined. If you're talking about security for the actual script, you can just set the owner to root which would require root access to read,write, and execute.
 

Aluvus

Platinum Member
Apr 27, 2006
2,913
1
0
If you're talking about security for the actual script, you can just set the owner to root which would require root access to read,write, and execute.

Just to clarify, it will only be readable/writable/executable to root if you set those permissions. It's perfectly possible to have a file that belongs to root but is readable/etc to other users.

@OP: for future reference, ssh can execute multiple commands, you just need to wrap them in quotes so they are treated as a single argument, ex:

Code:
ssh user@host 'command; command; command'

You can also knock together a small shell script and have ssh execute that, if it's more convenient.
 

jaydee

Diamond Member
May 6, 2000
4,500
2
81
Just to clarify, it will only be readable/writable/executable to root if you set those permissions. It's perfectly possible to have a file that belongs to root but is readable/etc to other users.

@OP: for future reference, ssh can execute multiple commands, you just need to wrap them in quotes so they are treated as a single argument, ex:

Code:
ssh user@host 'command; command; command'

You can also knock together a small shell script and have ssh execute that, if it's more convenient.

Ahh, thank you, that was more along the lines of what I was looking for, wrapping all the commands into a single ssh session. Fixed my script to reflect this.
 
sale-70-410-exam    | Exam-200-125-pdf    | we-sale-70-410-exam    | hot-sale-70-410-exam    | Latest-exam-700-603-Dumps    | Dumps-98-363-exams-date    | Certs-200-125-date    | Dumps-300-075-exams-date    | hot-sale-book-C8010-726-book    | Hot-Sale-200-310-Exam    | Exam-Description-200-310-dumps?    | hot-sale-book-200-125-book    | Latest-Updated-300-209-Exam    | Dumps-210-260-exams-date    | Download-200-125-Exam-PDF    | Exam-Description-300-101-dumps    | Certs-300-101-date    | Hot-Sale-300-075-Exam    | Latest-exam-200-125-Dumps    | Exam-Description-200-125-dumps    | Latest-Updated-300-075-Exam    | hot-sale-book-210-260-book    | Dumps-200-901-exams-date    | Certs-200-901-date    | Latest-exam-1Z0-062-Dumps    | Hot-Sale-1Z0-062-Exam    | Certs-CSSLP-date    | 100%-Pass-70-383-Exams    | Latest-JN0-360-real-exam-questions    | 100%-Pass-4A0-100-Real-Exam-Questions    | Dumps-300-135-exams-date    | Passed-200-105-Tech-Exams    | Latest-Updated-200-310-Exam    | Download-300-070-Exam-PDF    | Hot-Sale-JN0-360-Exam    | 100%-Pass-JN0-360-Exams    | 100%-Pass-JN0-360-Real-Exam-Questions    | Dumps-JN0-360-exams-date    | Exam-Description-1Z0-876-dumps    | Latest-exam-1Z0-876-Dumps    | Dumps-HPE0-Y53-exams-date    | 2017-Latest-HPE0-Y53-Exam    | 100%-Pass-HPE0-Y53-Real-Exam-Questions    | Pass-4A0-100-Exam    | Latest-4A0-100-Questions    | Dumps-98-365-exams-date    | 2017-Latest-98-365-Exam    | 100%-Pass-VCS-254-Exams    | 2017-Latest-VCS-273-Exam    | Dumps-200-355-exams-date    | 2017-Latest-300-320-Exam    | Pass-300-101-Exam    | 100%-Pass-300-115-Exams    |
http://www.portvapes.co.uk/    | http://www.portvapes.co.uk/    |