Skip to content

Instantly share code, notes, and snippets.

@ymirpl
Created December 29, 2009 14:54
Show Gist options
  • Save ymirpl/265352 to your computer and use it in GitHub Desktop.
Save ymirpl/265352 to your computer and use it in GitHub Desktop.
#!/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