Create same account in a multiple linux server


I need to run this command in a multiple linux server :

id -u user2 &>/dev/null || useradd -c "SSH account" -d /home/tools user2; echo 'password2' | passwd user2 --stdin

The idea is to create a text file with some IPs one per line and running the above command for each IP

But in somes linux servers, I can directly login as root@ip without password, but others linux servers I need to log as specific user@ip with password and switch to root

Any idea to do that ?
HenryIT consultantAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mihai BarbosTrying to tame bits. They're nasty.Commented:
Maybe using expect. Here is a short tutorial on how to use it.
nociSoftware EngineerCommented:
if ipfile contains:

Open in new window

for ip in $( cat ipfile ) 
  if ssh root@$ip  bash -c 'id -u user2 &>/dev/null || useradd -c "SSH account" -d /home/tools user2; echo "password2" | passwd user2 --stdin'
     echo $ip done through root
      if ssh user@$ip  sudo bash -c 'id -u user2 &>/dev/null || useradd -c "SSH account" -d /home/tools user2; echo "password2" | passwd user2 --stdin'
         echo $ip done though user / sudo
         echo $ip failed

Open in new window

This might need some adjustment here and there, but it can be a start for your updates..
If this need to be done often, then please look into manageing server farms using a root account that can only do passwordless logon to other roots within your cloud of systems using certificates.
David FavorLinux/LXD/WordPress/Hosting SavantCommented:
Easiest way to do this is to first fix every machine so you can login as root with an empty passphrase ssh key.

Trying to do this where some machines require logging in as non-root + switching to root can be done + writing all the shell code would take a good bit of time... which is why most people use root ssh logins to accomplish root tasks.

If you must login as non-root, you're going to have to play some serious games with ssh-agent or other tech.

Doing this can eat up hours of dev time, so you may require hiring someone to write this code for you as... code written will be somewhere unique to your specific runtime environments on initiating machine + all machines requiring this logic.
Duncan RoeSoftware DeveloperCommented:
Using Expect you could quite easily define which hosts need what. Expect is a Tcl extension and the host defining stuff would be in Tcl: array indexed by host name where the contents of each element are the user name to use (or could include password as well)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.