Sponsored Content
Top Forums Shell Programming and Scripting Get Permissions and save to data Post 302602173 by gvolpini on Sunday 26th of February 2012 03:40:06 PM
Old 02-26-2012
Get Permissions and save to data

Hi all;

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

Code:
#!/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:
Code:
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
3. 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.
Code:
gmpsrv, mmmsrv, fidsrv

Hope someone can help me out.
Thanks all in advance

Regards
Giuliano

Last edited by Franklin52; 02-27-2012 at 03:27 AM.. Reason: Please use code tags for code and indent your code, thank you
 

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

SCO Unix : how to read and save data from cd

Dear All How to read the data on my cd and save these data in a dpecial folder Regards (0 Replies)
Discussion started by: sak900354
0 Replies

2. Filesystems, Disks and Memory

Can ZFS Save my Data

Hello, Our RAID and server recently crashed and we are trying to recover our data. The problem appears to be that the Veritas File System/Logical Volume Manger became corrupt on our RAID. We are down to our last option, which is to run some Veritas commands that "may" result in data loss. ... (1 Reply)
Discussion started by: stringman
1 Replies

3. Shell Programming and Scripting

Shell Script to save/restore files and dir permissions

Hello all: I need found script to save and restore file permissions and owner:group... this is for backup my server... I have one problem in my server and file permissions and owner:group, mess up.. For this reason i need found one way to secure this aspect of the server... Thanks! (1 Reply)
Discussion started by: joangopan
1 Replies

4. Homework & Coursework Questions

Option to save data thats displayed?

Im trying to come up with a simple shell script to display info about the current user.`pwd`; `date` ; `who am i` I want to give the option to save all the data thats display by the commands to a log file. With a timestamp as the name of the file This is where im stuck echo "Would you like to... (3 Replies)
Discussion started by: eddieq20
3 Replies

5. UNIX Desktop Questions & Answers

Why can't I save a VI file after entering data?

Hi I am new to linux, when I typed "vi FILE1" I was able to open VI editor. I added some data and I want to save the file and I tried :w but it threw me an error. "file1" E212: Can't open file for writing Press ENTER or type command to continue why I am not able to save it? I read it... (16 Replies)
Discussion started by: chinnanji
16 Replies

6. Shell Programming and Scripting

Save log data in excel sheet

Hello, I have the following data format in a log file : a : x1 b : x2 c : x3 d : x4 -------- a : x5 b : x6 c : x7 d : x8 so the same fields ( a ,b ,c,d) repeated many times in the same log file but with different "x" values (x5,x6,x7,x8). I need a script to save this data in an... (6 Replies)
Discussion started by: mm00123
6 Replies

7. UNIX for Advanced & Expert Users

How to save a data of a file into a variable?

My requirement is to read a column data from a file & save it in a variable for each row & process it. I am using the below code- Leadlines="$TGTFILE/Error.txt">>$log_file while read line do id = ` echo $line | cut -d "," -f1 ` email = ` echo $line | cut -d "," -f2 ` ----------- done My... (2 Replies)
Discussion started by: saga20
2 Replies

8. Shell Programming and Scripting

Script to scrape page for and save data

Hi All I would like to get cars DB from this web site link removed , and I am trying to right script to go and parse the web page and save the data for each cars into a file, type of cars, mark, model, but when I look to the source page I found out that the first car type is preselected and the... (3 Replies)
Discussion started by: molwiko
3 Replies

9. UNIX for Dummies Questions & Answers

Get a data and save

If I have a A.log 1 Air Flow Monitor : 34.070 Degrees C 2 Air Flow Monitor : 41.730 Degrees C 3 Air Flow Monitor : 35.340 Degrees C 4 Air Flow Monitor : 33.370 Degrees C 5 Air Flow Monitor : 36.770 Degrees C 6 Air Flow Monitor : 45.910 Degrees C 7 Air Flow Monitor ... (1 Reply)
Discussion started by: sabercats
1 Replies

10. UNIX for Beginners Questions & Answers

How to save data and run the server again / Ubuntu16.04?

Hello, I am running ubuntu 16.04 and I have rented a server three days ago. I am with OVH and during image installation I did not merge hdd partitions, just proceed with custom installation configuration. Today I rebooted and server went offline. Company says: "Connect in rescue mode and check... (4 Replies)
Discussion started by: baris35
4 Replies
File::stat(3pm) 					 Perl Programmers Reference Guide					   File::stat(3pm)

NAME
File::stat - by-name interface to Perl's built-in stat() functions SYNOPSIS
use File::stat; $st = stat($file) or die "No $file: $!"; if ( ($st->mode & 0111) && $st->nlink > 1) ) { print "$file is executable with lotsa links "; } if ( -x $st ) { print "$file is executable "; } use Fcntl "S_IRUSR"; if ( $st->cando(S_IRUSR, 1) ) { print "My effective uid can read $file "; } use File::stat qw(:FIELDS); stat($file) or die "No $file: $!"; if ( ($st_mode & 0111) && ($st_nlink > 1) ) { print "$file is executable with lotsa links "; } DESCRIPTION
This module's default exports override the core stat() and lstat() functions, replacing them with versions that return "File::stat" objects. This object has methods that return the similarly named structure field name from the stat(2) function; namely, dev, ino, mode, nlink, uid, gid, rdev, size, atime, mtime, ctime, blksize, and blocks. As of version 1.02 (provided with perl 5.12) the object provides "-X" overloading, so you can call filetest operators ("-f", "-x", and so on) on it. It also provides a "->cando" method, called like $st->cando( ACCESS, EFFECTIVE ) where ACCESS is one of "S_IRUSR", "S_IWUSR" or "S_IXUSR" from the Fcntl module, and EFFECTIVE indicates whether to use effective (true) or real (false) ids. The method interprets the "mode", "uid" and "gid" fields, and returns whether or not the current process would be allowed the specified access. If you don't want to use the objects, you may import the "->cando" method into your namespace as a regular function called "stat_cando". This takes an arrayref containing the return values of "stat" or "lstat" as its first argument, and interprets it for you. You may also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. (Note that this still overrides your stat() and lstat() functions.) Access these fields as variables named with a preceding "st_" in front their method names. Thus, "$stat_obj->dev()" corresponds to $st_dev if you import the fields. To access this functionality without the core overrides, pass the "use" an empty import list, and then access function functions with their full qualified names. On the other hand, the built-ins are still available via the "CORE::" pseudo-package. BUGS
As of Perl 5.8.0 after using this module you cannot use the implicit $_ or the special filehandle "_" with stat() or lstat(), trying to do so leads into strange errors. The workaround is for $_ to be explicit my $stat_obj = stat $_; and for "_" to explicitly populate the object using the unexported and undocumented populate() function with CORE::stat(): my $stat_obj = File::stat::populate(CORE::stat(_)); ERRORS
-%s is not implemented on a File::stat object The filetest operators "-t", "-T" and "-B" are not implemented, as they require more information than just a stat buffer. WARNINGS
These can all be disabled with no warnings "File::stat"; File::stat ignores use filetest 'access' You have tried to use one of the "-rwxRWX" filetests with "use filetest 'access'" in effect. "File::stat" will ignore the pragma, and just use the information in the "mode" member as usual. File::stat ignores VMS ACLs VMS systems have a permissions structure that cannot be completely represented in a stat buffer, and unlike on other systems the builtin filetest operators respect this. The "File::stat" overloads, however, do not, since the information required is not available. NOTE
While this class is currently implemented using the Class::Struct module to build a struct-like class, you shouldn't rely upon this. AUTHOR
Tom Christiansen perl v5.16.3 2013-03-04 File::stat(3pm)
All times are GMT -4. The time now is 07:06 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy