LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-01-2013, 11:42 AM   #1
billinja
LQ Newbie
 
Registered: Nov 2004
Location: Newmarket, Ontario - Canada
Distribution: Ubuntu
Posts: 9

Rep: Reputation: 0
Problem with Remote Script Execution via SSH


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:

Code:
HOSTNAME=$(hostname)
cat /etc/passwd | cut -f1 -d":" > $HOSTNAME.users
ftp -n -v CENTRALSERVERNAME << EOT
ascii
user USERID PASSWORD
prompt
put *.users
bye
EOT
sleep 5
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.

Does anyone see what may not be obvious to me?

Thanks!
 
Old 05-01-2013, 01:22 PM   #2
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
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[@]}"
 
1 members found this post helpful.
Old 05-01-2013, 01:33 PM   #3
blingham
LQ Newbie
 
Registered: Jul 2009
Location: Canada
Distribution: Mint, Fedora, Ubuntu Server
Posts: 28

Rep: Reputation: 3
Quote:
Originally Posted by millgates View Post
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?

Thanks,
Jay
 
Old 05-01-2013, 02:21 PM   #4
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
Quote:
Originally Posted by blingham View Post
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.
 
1 members found this post helpful.
Old 05-02-2013, 06:56 AM   #5
blingham
LQ Newbie
 
Registered: Jul 2009
Location: Canada
Distribution: Mint, Fedora, Ubuntu Server
Posts: 28

Rep: Reputation: 3
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?

Thanks again for your help!
Jay
 
Old 05-02-2013, 07:07 AM   #6
blingham
LQ Newbie
 
Registered: Jul 2009
Location: Canada
Distribution: Mint, Fedora, Ubuntu Server
Posts: 28

Rep: Reputation: 3
Quote:
Originally Posted by blingham View Post
What is the best way to extract data from an array, into a text file so that each user is on a new line?
I figured it out....


Code:
for j in "${!usrlist[@]}"; do
        echo $j >>users.txt
done
Thanks for all of your help, I very much appreciate it!

Jay
 
Old 05-02-2013, 07:16 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,359

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Try the last line as
Code:
echo "${!usrlist[@]}"|sed -r 's/\s+/\n/g' >somefile

Last edited by chrism01; 05-02-2013 at 07:30 AM. Reason: alternate sed -r; cleaner code
 
1 members found this post helpful.
Old 05-02-2013, 10:59 AM   #8
millgates
Member
 
Registered: Feb 2009
Location: 192.168.x.x
Distribution: Slackware
Posts: 852

Rep: Reputation: 389Reputation: 389Reputation: 389Reputation: 389
You can also use printf:

Code:
printf "%s\n" "${!usrlist[@]}"
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Issue with remote script execution Orange Sunshine Linux - Newbie 7 05-09-2011 04:54 AM
[SOLVED] SSH for remote execution? Noobux Linux - Security 8 02-18-2011 10:29 AM
Escape command for remote ssh execution brianmcgee Linux - Software 2 06-14-2010 06:34 AM
Remote execution over SSH - incomplete environment echa Linux - Server 5 10-06-2009 10:14 AM
Automated execution of a command on a remote system via SSH GUIPenguin Linux - General 1 02-28-2006 11:23 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration