How to write bash script for creating user on multiple Linux hosts?
I wonder whether someone can help me with what I'm trying to achieve
Basically, the objective is one script to create new user on more than 70 linux hosts if required.
Everything works apart from the highlighted part. It gave me an output passwd: Unknown user name ''. when try to set remote password for new user. It seems like it's trying to set password locally and not remotely.
The idea is all information about username,password,uid,comments is pull out from the file IDENTIFY. That way, if we create new user, people only edit the file than the script.
All fields in the IDENTIFY file separates by white space.
Please help how can I achieve this
Moderator's Comments:
Use code tags!
Last edited by radoulov; 02-18-2013 at 06:30 AM..
Reason: Miss highlight fault line
I would do one ssh per host and ship the scripting to the remote host, for speed and simplicity.
For getting hosts from your config file, just:
For good error handling, echo each host, time and user as you start each, into a log. you can display the log to the terminal, too, with 'tee -a' or 'tail -f log_file &' (just remember to kill $!). Screens can go poof, and you should keep a report.
A user name can be a substring of any field (like roo or oot !), so put some boundaries in the grep:
Count 0/1 is a nicer test.
You can put a whole script on the command line, or just send it into ssh bash. I prefer "echo '...'|ssh ...." but "<<!" is pretty popular, too, just be careful to escape meta like $ from local expansion. No need to leave files lying around or have multiple scripts. Don't forget to set up env on remote shell, like '. ./.profile' or the like.
Your problem is that the right side of the pipe is executed on the calling host.
You need to force it to the remote host, by quoting:
or simpler
Please omit the backticks! They will run the output as commands. If the output would be "reboot" ... guess what happens!
Further, you can combine two adjacent ssh statements in one ssh statement:
The calling host sees one multi-line "string".
Within the "" quotes it replaces each $VAR by its value.
$ROLE should be quoted on the remote host; you need either \"$ROLE\" or '$ROLE'. The '' ticks do not replace $ROLE by its value, but this is meaningless on the remote host, because it is already replaced by the calling host. And the calling host treats a ' tick as simple characters if it appears within a "string".
There are many more possible optimizations; DGPicket pointed out some...
Last edited by MadeInGermany; 02-18-2013 at 06:23 PM..
This User Gave Thanks to MadeInGermany For This Post:
Hi everyone!
Some time ago, I had to check connectivity with a big list of hosts, using different formats (protocol://server:port/path/, server:port, ....).
I developed a script that checks the connectivity using different commands (ping, telnet, nc, curl).
It worked for me so I'm sharing it... (9 Replies)
I have a script to detect if a host is pingable or not. The problem is that I would like it to put the nonpingable hosts in one file and the pingable hosts in another. I have come up with this so far:
for ip in `cat /tmp/testlist2`; do ping -c 3 $ip >/dev/null && echo "$ip is up" || echo "$ip... (5 Replies)
I have a script on about 15 hosts that I need to run for each host whenever I want (not crontab). Problem is, this script takes 5-10 mins to run for each host. Is there a way I can run the script in parallel for all the hosts instead of 1 at a time? Also, I'm remotely running the script on the... (3 Replies)
Hi
I need to create a user who can have access on only one folder.
for example I created a user "test" . he should have access only on folder
/testfolder.
The problem is that the user will mostly use FileZilla to ftp his files in the testfolder.
In the fileZilla , i want him to be... (21 Replies)
Hi,
Can someone help me with creating a bash shell script.
I need to create a script that gets a positive number n as an argument.
The script must create n directories in the current directory with names like map_1, map_2 etcetera. Each directory must be contained within its predecessor. So... (7 Replies)
I have a directory full of zip files.
How would I write a bash script to enumerate all the zip files, remove the ".zip" from the file name, create a directory by that name and unzip each zip file into its corresponding directory?
Thanks!
Siegfried (3 Replies)
I am trying get "df -k" output from multiple hosts along with their hostnames via ssh, my script is appending the "df -k" output from all the nodes to a single file but not getting the hostnames for those nodes, just wondering how to pass more than one command via ssh or may be someone could come... (6 Replies)
I've had Linux when I was young, on one of our first computers and learned it pretty quickly... However, I've been working on Windows for about 7 years now and just made the switch back to Linux, Mandrake 9.1. So I have some problem with creating new user accounts. I could create one, but once I... (3 Replies)
I am trying to think of a way to create user ids on multiple Linux systems in one fell swoop without logging onto each system indivually. Is there a way to do this with ssh commands? I don't want to use NIS/LDAP solution just a simple shell script utilitarian methodoloy would suffice. Also, I am... (1 Reply)