ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I apologize if this kind of thing has been covered, but my search turned up empty.
I'm not new to Linux, but new to my role at work which requires me to administer some Linux, HP-UX and AIX servers. I'm attempting to write a script that will pull the users (from the /etc/passwd file), and ftp the results to a central system, allowing me to get a list of all unique users across all of our servers.
So far, I have a working script that grabs the first field of the /etc/passwd, and dumps it to a file. It then FTPs that file to my central system. This works fine as long as I'm logged onto the system itself:
I then deployed that script into my /home/user directory on one of our AIX servers in order to test, and wrote the following script to execute it remotely:
Code:
GETREMOTEUSERS() {
for a in `cat systems.txt 2>/dev/null`; do
printf "Getting users on $a...\n"
ssh USERID@$a "./getUsers.ksh"
done
}
GETREMOTEUSERS
printf "Processing completed.\n"
exit 0
This script runs without error; however the file that gets FTP'd to the CENTRALSERVERNAME, is 0 bytes (empty). If I log directly onto the server, and execute the exact same script, the file comes across with all of the data.
Why do you need two scripts? Something like this should get a list of unique users:
Code:
#!/bin/bash
declare -A usrlist
# please note that while it works here, reading files with
# for is generally *not* a good idea
for srv in $(<systems.txt); do
echo "Getting users on $srv..."
while read line; do
usrlist[${line%%:*}]=
done < <(ssh "$srv" "cat /etc/passwd")
done 2>/dev/null
echo "users: ${!usrlist[@]}"
Why do you need two scripts? Something like this should get a list of unique users:
Hi Millgates, thanks for your reply!
To answer your question, honestly, I probably don't - that's my inexperience showing
I'll see if I can adapt your code to my needs, because ultimately I need to run this against upwards of 100 servers, and I want to be able to take the results from all of the servers, and consolidate it into one master list of account names. That was why I was running the script on each server, and FTP'ing the results for each server to a central location to process them. Could I probably do it in one master script that builds the consolidated file on the fly? Probably - but I'm not overly sure if my scripting skills are there yet I'm an intermediate powershell guy moving to BASH and KSH scripting, so at this point I'm still trying to subst bash/ksh commands for M$ commands.
Now, I don't yet understand every line of the code you've shared with me, but I don't see anything that tells me that it's dropping each listing in a central place, or building a listing of unique users?
Now, I don't yet understand every line of the code you've shared with me, but I don't see anything that tells me that it's dropping each listing in a central place, or building a listing of unique users?
Thanks,
Jay
The script puts the names as keys into an associative array "usrlist":
Code:
usrlist[${line%%:*}]=
The ${line%%:*} construct extracts the first column from the string.
The last line then prints all the keys in the array.
I used the code above, and it works perfectly - thank you.
Just one question. When I dump ${!usrlist[@]} to a file, it comes out as a continuous stream of tab separated values. What is the best way to extract data from an array, into a text file so that each user is on a new line?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.