I need to automate a way to distribute files to many servers. The problem of course is I need to use a secure protocol (SSH or SCP) and the username / password on each server is different.
The scenario is we have a master server a, with user a_prod and we need to send updated scripts / configs, etc. to servers b, c, d, ... and on these servers the usernames are say b_dev, c_test, d_prod with each having unique passwords.
The usernames need to be unique across environments for a few reasons, dealing with DB2 and corporate security.
Shared keys will not work in this scenario, so I need to pass the usernames and passwords via a script. It is an AIX environment, and I do not have the ability to install expect.
Any ideas?
Shared keys are mentioned in multiple responses below: I have tried a few ways to do this, I think the main issue is the remote user ID's do not exist on any other host, so I can not do an ssh-keygen for them on the host I want to ssh from to do a shared key implementation with the main user (a_prod) having multiple identities depending upon the target host (b_dev, c_test_d_prod). The private key for these users needs to be generated on host a for the remote users, and then their public keys need to be copied to the target hosts.
"Corporate S(tupidity)" :-)
SSH / SCP generally lives with the realm of UNIX philosophy I think. It is very rare that the designers of Unix applications intentionally make it so the administrator can't easily do something stupid. There will be generally be a warning like: "You probably don't want to do this because ... but, if you really want to, fine!".
In the case of passing a password to ssh in a script, they intentionally make this difficult, because it just such a bad idea.
At this point, I wouldn't even bother with SSH. You really need to talk with the corporate security people and come up with a real solution for the scenario. If they don't want to use keys, ask them what you should do. If that doesn't work, tell your managers what they want can't be done due to corporate security restrictions. If the managers do insist that you do this, get it in writing, or it is your ass on the line
But we all work in the real world where sometimes it doesn't make sense. Soo.. There are many tools that will let you do this. Shell does not have a built-in way to get this working. My weapon of choice for very quick and dirty scripts of this nature is Expect. It is rediculously easy to get running.
Obviously this may or may not work for you, but you can see from the syntax just how simple it is to get working.
First a warning: be careful that you don't create a security issue in the name of expediency. Kevin and Kyle bring up good points. Storing a bunch of username/password combinations in a file (even an encrypted database) is probably a very bad idea and may violate corporate policy.
The solution to your problem may be a client side certificate. See the below articles for more information:
Assuming that a public private key pair is allowed by the corporate policy this is not that hard to achieve. To get a username different from the user running ssh (the script) use
username@host
. The harder part is to get the proper private keys working. Assuming that because of some kind of false sense of security each host must use a different key pair (otherwise it is simpler) you should create a configuration file that tells the client which pair to use for which host (you can also actually specify the username in the file).For openssh the file would look like
etc.
Just remember that this setup is not actually more secure. If someone can get access to this user on this computer he can read all private keys (that can't have a password if things must be secure, or you must hack around with ssh_agent) as well as hardcoded passwords. As there is no reason for the private key files to live anywhere else but in the ssh dir of this user (when lost, one can easilly create a new key) having four files is not different from having one from a security perspective.
OK - I finally got this working as follows:
ssh -i b_dev_id_dsa_2048_a b_dev@b
and viola, it finally works.
Thanks you everyone for the help, and finally niXar for that final Homer Simpson DUH moment as doing this is exactly backwards to what you do if the user is the same across all of the hosts.
Without private keys or Expect or something like it, I don't see how you can do this automagically. Something's got to give.
You do have something to work with, do you? I supposed you don't have Perl around since you could then use Expect.pm? Python? Anything? Just plain old ugly ksh? Tell us more.
If you're not allowed to use a programming language, then you need to enter the pass by hand. In that case you can use:
If batches are involved, consider using SFTP instead. You could then use lftp, and not have to type the password, here straight from the manpage:
But if you can't get Expect I doubt you can use lftp. Can you?
/ Oh I hate proprietary Unix crap
It's not a good idea, but if you really need to do it sshpass can help you: http://sourceforge.net/projects/sshpass/
I had to do something similar, only backwards. We're pulling files off of a bunch of servers, and this is the way we did it.
Create a file called config and in it put the lines you want to run. (please note: you need to insert a blank line to start with).
The way it's setup right now, you would put
[email protected]|(folder on remote server)|(folder on local server)
into config, and it will rsync the two. You will want to switch the rsync command in the awk command to do the opposite.This would work well with ssh-key auth, which is what we use.
I would as well suggest creating SSH-Keys for the authentication and use a tool like unison or lsyncd (depending on your need) to keep files in sync.
If the key based authenitication is not an option, you can use "expect" (http://linux.die.net/man/1/expect) to script it. Expect checks for a defined outupt (lets say the question for the password) and related to that enters a defined string (in that example the password).
But again as suggested, i would prefer the ssh-key solution.