Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

TEXT FILES MANIPULATION VIA SHELL OR PERL SCRIPTING - NEED FEEDBACK

Posted on 2008-06-26
5
Medium Priority
?
412 Views
Last Modified: 2013-12-26
CAN SOMEONE PROVIDE SAMPLE CODE ON HOW TO HANDLE TWO (2) TEXT FILES WITH RECORDS, SUCH AS PASSWORD FILES FROM TWO (2) HOSTS AND BE ABLE TO COMPARE ENTRIES ON BOTH OF THEM AND UPDATE THE SECONDARY PASSWORD FILE WITH ENTRIES FROM THE PRIMARY MASTER PASSWORD FILE?

HOST1 "/etc/passwd" file contains entries such as :
root:!:0:0::/:/usr/bin/ksh
daemon:!:1:1::/etc:
bin:!:2:2::/bin:
sys:!:3:3::/usr/sys:
adm:!:4:4::/var/adm:
uucp:!:5:5::/usr/lib/uucp:
guest:!:100:100::/home/guest:
nobody:!:4294967294:4294967294::/:
lpd:!:9:4294967294::/:
lp:*:11:11::/var/spool/lp:/bin/false
invscout:*:200:1::/var/adm/invscout:/usr/bin/ksh
nuucp:*:6:5:uucp login user:/var/spool/uucppublic:/usr/sbin/uucp/uucico
paul:!:201:1::/home/paul:/usr/bin/ksh
jdoe:*:202:1:John Doe:/home/jdoe:/usr/bin/ksh
paul:!:201:1::/home/paul:/usr/bin/ksh                          <== user acct to populate to HOST2
jdoe:*:202:1:John Doe:/home/jdoe:/usr/bin/ksh          <== user acct to populate to HOST2

HOST2 "/etc/passwd" file contains entries such as:

root:!:0:0::/:/usr/bin/ksh
daemon:!:1:1::/etc:
bin:!:2:2::/bin:
sys:!:3:3::/usr/sys:
adm:!:4:4::/var/adm:
uucp:!:5:5::/usr/lib/uucp:
guest:!:100:100::/home/guest:
nobody:!:4294967294:4294967294::/:

What I would like to do is to compare HOST2 password file against HOST1 password file entries, and harmonize HOST2 password file with missing entries from HOST1 password file, being that UID and GID are unique.

I appreciate your insight and recommendation/code samples on how to address this,

Mike McDonald

 
0
Comment
Question by:maldonmi
  • 3
5 Comments
 
LVL 85

Expert Comment

by:ozo
ID: 21875812
perl -F: -ane 'print unless $seen{"@F[2,3]"}' host2password host1password
0
 

Author Comment

by:maldonmi
ID: 21875851
OZO:

Thanks for your prompt response. But could you please explain to me a bit the logic of this PERL one-liner.

And thanks so much.!,

Mike
0
 
LVL 6

Accepted Solution

by:
RSLE earned 500 total points
ID: 21885463
The problem with the one liner above is you loose conflicting entries.

As you're trying to merge password files, I'm going to change your parameters... you say UID & GID are unique, however the same GID can be used by more than one user.  Instead I'm going to say UID & username are the unique fields.  With that change, my script here will merge the 2 files and for entries that use the same UID with different usernames we'll change one of the UID's.
#!/usr/bin/perl
 
$file1 = "pass1";
$file2 = "pass2";
$newfile = "pswd-combined";
 
### read in the first file and get the unique identifiers
open(HOST1, $file1);
foreach $line (<HOST1>) {
	($user, $uid) = (split(":", $line))[0,2];
	$HOST{$user} = $line;
	$UID1{$uid} = $user;
}
close(HOST1);
 
## load the UID's from file1 into another hash for unique UID
## this needs copied so the orig can be used to find duplicates
%UID = %UID1;
 
## get the UID's used and read the second file into memory
## the main goal here is to get the UID's
open(HOST2, $file2);
foreach $line (<HOST2>) {
	($user, $uid) = (split(":", $line))[0,2];
	$UID{$uid} = $user;
 
	## load the file into an array to parse later
	push(@HOST2, $line);
}
close(HOST2);
 
## work with the data in the array now
foreach $line (@HOST2) {
	($user, $uid) = (split(":", $line))[0,2];
 
	## check for duplicate UID's w/ different users
	if ($UID1{$uid} && $UID1{$uid} ne $user) {
		## find the next available UID
		$n_uid = 500;  ## where to start looking
		while ($UID{$n_uid}) { $n_uid++; }
 
		## replace the duplicate UID w/ the open one
		$line =~ s/:$uid:/:$n_uid:/;
 
		## add the new UID to the list of used UID's
		$UID{$n_uid} = $user;
	}
	## this lets host2 overwrite duplicate
	## entries or simply add any new entries
	$HOST{$user} = $line;
}
close(HOST2);
 
## now output the new combined data to the new file
open(NEW, "> $newfile");
foreach $user (keys (%HOST)) {
	print NEW $HOST{$user};
}
close(NEW);

Open in new window

0
 

Author Comment

by:maldonmi
ID: 21885659
RSLE:

I'll test out your PERL script, and will get back to you ASAP.

Thankyou so much for your help,

Mike
0
 

Author Closing Comment

by:maldonmi
ID: 31471014
BIG THANKYOU.!!!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
Suggested Courses
Course of the Month12 days, 9 hours left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question