Project 1 audit user
This commit is contained in:
@@ -1,9 +1,7 @@
|
|||||||
|
|
||||||
if the number of user dobules
|
if the number of user dobules
|
||||||
|
|
||||||
the time of execution will be multiply by 2
|
the time of execution will be multiply by 2
|
||||||
|
|
||||||
|
|
||||||
$$
|
$$
|
||||||
T(n, m) = n \times m
|
T(n, m) = n \times m
|
||||||
T(2n, m) = 2 \times n \times m
|
T(2n, m) = 2 \times n \times m
|
||||||
|
|||||||
28
week 1/projects/User audit Script/correction.ps1
Normal file
28
week 1/projects/User audit Script/correction.ps1
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
# PowerShell User Audit Script
|
||||||
|
|
||||||
|
# Get all local usernames as strings
|
||||||
|
#! Get-LocalUser returns objects, not just strings.
|
||||||
|
#! -split $users is incorrect for objects.
|
||||||
|
$users = Get-LocalUser | Select-Object -ExpandProperty Name
|
||||||
|
|
||||||
|
foreach ($username in $users) {
|
||||||
|
$badChar = $false
|
||||||
|
|
||||||
|
# Check each character
|
||||||
|
foreach ($char in $username.ToCharArray()) {
|
||||||
|
if ($char -notmatch "[A-Za-z]") {
|
||||||
|
$badChar = $true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
# Check first character
|
||||||
|
$firstChar = $username[0]
|
||||||
|
$validFirstLetter = $firstChar -match "[A-Za-z]"
|
||||||
|
|
||||||
|
# Flag invalid usernames
|
||||||
|
# * Note: -ge 20 includes length 20. If you want max length 20 valid, use -gt 20.
|
||||||
|
if ($badChar -or $username.Length -gt 20 -or -not $validFirstLetter) {
|
||||||
|
Write-Host "The username '$username' is invalid"
|
||||||
|
}
|
||||||
|
}
|
||||||
26
week 1/projects/User audit Script/correction.sh
Normal file
26
week 1/projects/User audit Script/correction.sh
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# userAudit: print invalid usernames
|
||||||
|
#! Safe iteration over /etc/passwd using while read -r
|
||||||
|
|
||||||
|
awk -F ":" '{print $1}' /etc/passwd | while read -r username; do
|
||||||
|
firstChar="${username:0:1}"
|
||||||
|
badChar=false
|
||||||
|
|
||||||
|
# Check if username contains only letters
|
||||||
|
for ((i=0; i<${#username}; i++)); do
|
||||||
|
char="${username:i:1}"
|
||||||
|
if ! [[ "$char" =~ [A-Za-z] ]]; then
|
||||||
|
badChar=true
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
# Flag invalid usernames
|
||||||
|
#! Corrected logic to properly flag invalid usernames:
|
||||||
|
#! check if the firstchar is NOT a letter
|
||||||
|
if (( ${#username} > 20 )) || ! [[ "$firstChar" =~ [A-Za-z] ]] || $badChar ; then
|
||||||
|
#! Quotes around $username in echo for robustness.
|
||||||
|
echo "$username"
|
||||||
|
fi
|
||||||
|
done
|
||||||
21
week 1/projects/User audit Script/userAudit.ps1
Normal file
21
week 1/projects/User audit Script/userAudit.ps1
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
$users = Get-LocalUser
|
||||||
|
|
||||||
|
foreach($username in -split $users){
|
||||||
|
|
||||||
|
$badChar = $false
|
||||||
|
|
||||||
|
foreach ($char in $username.ToCharArray()) {
|
||||||
|
if($char -notmatch "[A-Za-z]"){
|
||||||
|
$badChar = $true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$fistChar = $username[0]
|
||||||
|
$validFirstLetter = $fistChar -match "[A-Za-z]"
|
||||||
|
|
||||||
|
if ($badChar -or $username.Length -ge 20 -or -not $validFirstLetter ){
|
||||||
|
Write-Host "the userName " + $username + " is invalid"
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
29
week 1/projects/User audit Script/userAudit.sh
Executable file
29
week 1/projects/User audit Script/userAudit.sh
Executable file
@@ -0,0 +1,29 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# userAudit
|
||||||
|
|
||||||
|
#Read the passwd file for all the username
|
||||||
|
file=$(awk -F ":" '{print $1}' /etc/passwd)
|
||||||
|
|
||||||
|
for username in $file; do
|
||||||
|
firstChar="${username:0:1}"
|
||||||
|
badChar=false
|
||||||
|
|
||||||
|
#check if the username contain only letters
|
||||||
|
for ((i=0; i<${#username}; i++)); do
|
||||||
|
char="${username:i:1}"
|
||||||
|
if ! [[ "$char" =~ [A-Za-z] ]]; then
|
||||||
|
badChar=true
|
||||||
|
break
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
#if the username is not valid
|
||||||
|
# longer than 20 characters, start with not a letter and contain other char than letter
|
||||||
|
# print it
|
||||||
|
if ((${#username} >= 20)) || ! [[ "$firstChar" =~ [A-Za-z] ]] || $badChar ; then
|
||||||
|
echo $username
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user