Results 1 to 7 of 7

Thread: Perl Script - .CSV File and Commands

  1. #1
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default Perl Script - .CSV File and Commands

    I need some help writing a perl script that will read from a file and and take the data from a file and use the data as part of a bunch of commands.

    So this is what I have to do:

    1. Read contents from CSV file containing [first name],[last name],[campus email id] parse it
    2. Place parsed data into create user command
    Code:
    useradd -gstudents -Gassistants -s/bin/bash -d/home/roger -m roger
    **need to figure out how to add the first name and last name to the command

  2. #2
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    post your code
    ;
    1337

  3. #3
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    Code:
       #!/usr/bin/perl
        use strict;
        use warnings;
        use Text::CSV;
    
        my $file = 'Test.csv';
    
        my $csv = Text::CSV->new();
    
        open (CSV, "<", $file) or die $!;
    
        while (<CSV>) {
            if ($csv->parse($_)) {
                my @line = $csv->fields();
                print "@line\n";
            } else {
                my $err = $csv->error_input;
                print "Failed to parse line: $err";
            }
        }
        close CSV;
    This is the code he provided for us to modify

  4. #4
    jackal Guest

    Default

    .................................................. ....
    Last edited by jackal; Apr 21, 2013 at 10:12 AM.

  5. #5
    Join Date
    Mar 2006
    Posts
    325
    Rep Power
    0

    Default

    Code:
    #!/usr/bin/perl
    use strict;
    use 5.12.4;#the version on the machine I was using
    use Text::ParseWords;
    my $file = 'myfile.csv';
    my @fields = ();
    open(my $fh, '<', $file) or die "Can't read file '$file' [$!]\n";
    while (my $line = <$fh>) {
        @fields = &quotewords( ',', 0, $line )
          or ( warn "sumn nuh right yahso $.:$_" );
    }
    exec "$fields[0]";
    try it with the date command in a csv file
    1337

  6. #6
    Join Date
    May 2010
    Posts
    3,851
    Rep Power
    17

    Default

    Thanks guys for the help, highly appreciated! Got it working!

    The final code is below

    Code:
       #!/usr/bin/perl
        use strict;
        use warnings;
        use Text::CSV;
    
        my $file = 'Test.csv';
    
        my $csv = Text::CSV->new();
    
        open (CSV, "<", $file) or die $!;
    
        while (<CSV>) {
            if ($csv->parse($_)) {
    	my @line = $csv->fields();
    
    	my $psw = "$line[2]";
    	my $salt = 'ub51ck7.7-+kx-si+/90vb]5nuxkfc';
    	my $encrypted_string = crypt($psw,$salt);
    chomp $psw;
    	print "$line[0] $line[1]\n"; #returns parsed elements $line[1], $line[2] etc
    	my $result = `useradd -gstudents -s/bin/bash -d/home/$line[2] -m $line[2] -p $encrypted_string -c "$line[0] $line[1]"`; 
             $result = `passwd -e "$line[2]" `; # forces user to change password at logon
    	      } else {
                my $err = $csv->error_input;
                print "Failed to parse line: $err";
            }
        }
        close CSV;

  7. #7
    jackal Guest

    Default

    .................................................. ......
    Last edited by jackal; Apr 21, 2013 at 10:12 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •