Get Permissions and save to data

Hi all;

I have the following code which gives me kind of what I need:

#!/usr/bin/perl
use Fcntl ':mode';
#
if ($ARGV[0] ne "") {
$filename = $ARGV[0];
} else {
print "Please specify a file!\n";
exit;
}
#
if (($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = lstat($filename)) {
$user = getpwuid($uid);
$group = getgrgid($gid);
#
$ftypes[S_IFDIR] = "d";
$ftypes[S_IFCHR] = "c";
$ftypes[S_IFBLK] = "b";
$ftypes[S_IFREG] = "-";
$ftypes[S_IFIFO] = "p";
$ftypes[S_IFLNK] = "l";
$ftypes[S_IFSOCK] = "s";
#
$permissions = sprintf "%04o", S_IMODE($mode);
$filetype = S_IFMT($mode);
#
$ftype = $ftypes[$filetype];
#
print "File: $filename\n";
printf "File mode: %o (%d)\n", $mode, $mode;
printf "File type: %s (%o)\n", $ftype, $filetype;
print "File permissions: $permissions\n";
print "File size: $size\n";
print "File owner user: $user\n";
print "File group: $group\n";
} else {
print "Please specify an EXISTING file!\n";
}
 

And this is how it works:

root@ubuntu:/usr/include# /tmp/stat.pl /tmp/stat.pl
File: /tmp/stat.pl
File mode: 100755 (33261)
File type: - (100000)
File permissions: 0755
File size: 918
File owner user: greys
File group: greys

But what I really need is to:

  1. find only directories and files with 777 permission on HP and Linux servers; so I want one script where I can ssh to each server and grab a listing of all 777 dir\files and put them in a comma delimited file
    2.After getting the data into the file I need to then write it to a MySQL DB called audit into a table called myperms; I need to do this for about 25 servers
  2. In Type; if it's a dir the script does put a "d" and that's what I want written to the table but I want "f" if it's a file not an "-".

I am failry new to perl; however I know how to connect to my MySQL and open a file to get data. My shorcomings in Perl though is that I do not know how to write data into a table. Also I do not know how I can modify the abaove script to start checking from the root "/" and go through the entitre server...how to get all server (a loop maybe?)

Here's a sample of some server names that you can use.

gmpsrv, mmmsrv, fidsrv

Hope someone can help me out.
Thanks all in advance

Regards
Giuliano

If you want to search the entire file system on each of a set of hosts, then this small bit of a script will generate a (potentially long) comma separated list of files/directories which have a permission setting of 777. The first field of the list is the hostname.

for host in host1 host2 host3
do
    printf "%s " $host
    ssh $host find / -perm 0777 | awk '{printf( ",%s", $0 );} END { printf( "\n" ); }'
done >/tmp/list_777

I don't do SQL, so I cannot help with getting this loaded into any kind of DB, but this might get you started.

Thanks..I will give it a try...can anyone else please help with the MySQL part?

Regards
G