Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I would like to have a (bash) script to rename a file on multiple remote linux servers any idea how to do it? thanks.

share|improve this question

3 Answers 3

You can use ssh for that. For example:

for server in server1 server2 server3; do ssh $server mv oldfilename newfilename; done

You may want to have a list of servers stored in a environment variable:

export MYLISTOFSERVERS="server1
server2
server3
...
servern
"

and a function (for example) in your bashrc (or a dedicated script):

runforeachserver () {
for server in $MYLISTOFSERVERS; do
ssh $server "$@"
done
}

so you can call it whenever you want to do tasks for each of your servers. For example rename files as you wanted:

runforeachserver mv oldfilename newfilename

or (just to show you how to scape the command to pass through ssh):

runforeachserver date -d \"month ago\" +\"%Y-%m-%d\"
2011-04-04
2011-04-04
2011-04-04
2011-04-04
2011-04-04
2011-04-04

Obviously this can be as robust as you wish (enabling arrays of servername/sshport), syntax checks, etc...

share|improve this answer
    
For this to work, you might need to have ssh keys configured so that the bash script does not prompt for a username or password. –  RussellBallestrini May 4 '11 at 19:47
    
Of course otherwise you'll need lots of patience to enter the password for each of the servers LOL –  hmontoliu May 4 '11 at 19:49

While not a bash script, you might also like to try ClusterSSH. It will help you to execute the same command on multiple servers at the same time.

share|improve this answer

You can use a one-liner perl:

perl -lne 'system("/usr/bin/ssh -l username $_ command")' computers

where computers is a file with one IP-adress on each line and command is the actuall command you want to run. (modify path to your ssh binary as needed, for security reasons use an absolute path)

You should setup SSH-keys aswell. You can use ssh-copy-id for that so you wont need to enter a password everytime you run the script, for each server.

ssh-keygen
ssh-copy-id username@hostname
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.