Lab 14: SSH Prompt - squatchulator/Tech-Journal GitHub Wiki

week14.ps1

# Storyline: Create a script that will test if a connection to a client is possible on port 22, and will execute a whitelisted command on the server.

# Define the web server IP
$clientIP = "10.18.1.23"

# Test if a connection to the web server is possible on port 22
$connection = Test-Connection $clientIP
if ($connection) {
    Write-Host "Connection to $clientIP on port 22 succeeded!" -BackgroundColor Green -ForegroundColor White
    sleep 2
    clear
} else {
    Write-Host "Connection to $clientIP on port 22 failed." -BackgroundColor Red -ForegroundColor White
    sleep 2
    clear
}
# Array of linux commands allowed to be executed on the web server
$commandList = @('ps -ef', 'netstat -apn --inet', 'last -10', 'cat /etc/passwd', 'id', 'dpkg -l')

# While loop that allows the user to execute the remote commands
while ($true) {
    $command = Read-Host "Enter a command to run on $clientIP"
    # Check if the input matches a command in the array, and execute it if so
    if ($command -eq "exit") {
        break
    }
    if ($commandList -contains $command) {
        Invoke-Expression $command
    } else {
        Write-Host "Invalid command." -BackgroundColor Red -ForegroundColor White
        sleep 2
        clear
    }
}