Created
December 29, 2009 14:54
-
-
Save ymirpl/265352 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use warnings; | |
use strict; | |
# args: login shell | |
sub change_shell { | |
my $username = shift; | |
my $shell = shift; | |
my @command = ("chsh", "-s ".$shell, $username ); | |
system(@command) or print STDERR "Failed to chsh"; | |
} | |
# args login gid | |
sub change_group { | |
open PASSWD, '</etc/passwd' or die $!; | |
my $uid = 1000; | |
while (<PASSWD>) { | |
chomp; | |
my @line = split(/:/); | |
if ($line[2] > $uid) { | |
$uid = $line[2]; | |
} | |
} | |
close(PASSWD); | |
return $uid+1; | |
} | |
# This function generates random strings of a given length | |
sub generate_password { | |
my $length_of_randomstring = shift; | |
my @chars=('a'..'z','A'..'Z','0'..'9','_','!','@','#','$','%','^','&','(',')','-','=','+'); | |
my $random_string; | |
foreach (1..$length_of_randomstring) | |
{ | |
$random_string .= $chars[rand @chars]; | |
} | |
return $random_string; | |
} | |
# Follows unix convention: free uid is higher not taken uid | |
sub find_free_uid { | |
open PASSWD, '</etc/passwd' or die $!; | |
my $uid = 1000; | |
while (<PASSWD>) { | |
chomp; | |
my @line = split(/:/); | |
if ($line[2] > $uid) { | |
$uid = $line[2]; | |
} | |
} | |
close(PASSWD); | |
return $uid+1; | |
} | |
sub uid_exists { | |
my $uid = shift; | |
open PASSWD, '</etc/passwd' or die $!; | |
while (<PASSWD>) { | |
chomp; | |
my @line = split(/:/); | |
if ($line[2] == $uid) { | |
return 1; | |
} | |
} | |
close(PASSWD); | |
return 0; | |
} | |
my @args = ("ls", "-la"); | |
print find_free_uid(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment