• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Cleaning up a simple bash script

jaydee

Diamond Member
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!
 
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.
 
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.
 
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.
 
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.
 
Back
Top