summaryrefslogtreecommitdiff
path: root/.github/pdu/Pdu.ps1
blob: b87fd4705c65a69d2dddc8066f15dfec5dd0b0b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# ======================================================================
#
# Manufacturer: Eaton Tripp Lite Switched Power Distribution Unit (PDU)
# Website: https://www.eaton.com/us/en-us/skuPage.PDUMH15NET.html
# Model: PDUMH15NET
# SNMP/Web Management Accessory Card: WEBCARDLX
#
# HW Requirements: Ethernet cable or USBA-to-MicroUSB cable.
# SW Requirements: Plink.exe from PuTTY terminal client SW (if Ethernet)
# PuTTY: https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html
#
# Script: PowerShell script for turning ON/OFF and power CYCLING the PDU
# Usage: .\Pdu.ps1 [on | off | cycle] [1-16]
#
# ======================================================================

param(
    [Parameter(Mandatory=$true, Position=0)]
    [ValidateSet("cycle","on","off")]
    [string]$Action,

    [Parameter(Mandatory=$true, Position=1)]
    [ValidateRange(1,16)]
    [int]$LoadNumber
)

# =========================
# Configuration
# =========================
$ComNumber   = 3
$ComPort     = "COM$ComNumber"

$PduIP       = "169.254.0.1"
$SshPort     = 22

$Username    = "localadmin"
$Password    = "@Password123"

$Plink       = "C:\Program Files\PuTTY\plink.exe"

# =========================
# Helper Functions
# =========================

function Test-PduSerialConnection
{
    [bool]$Connected = $false

    try
    {
        $Port = New-Object System.IO.Ports.SerialPort $ComPort,115200,None,8,one
        $Port.ReadTimeout = 3000
        $Port.WriteTimeout = 3000
        $Port.Open()
        Start-Sleep 5
        $Port.DiscardOutBuffer()
        $Port.DiscardInBuffer()
        $null = $Port.ReadExisting()

        # Wake up console
        $Port.WriteLine("")
        Start-Sleep 2

        $Banner = $Port.ReadExisting()
        if ($Banner -match "login|localadmin|PowerAlert|Ubuntu") {
            Write-Host "PDU detected on serial connection at $ComPort"
            $Connected = $true
        } else {
            Write-Host "PDU NOT detected on serial connection at $ComPort" -ForegroundColor Yellow
        }
    }
    catch
    {
        Write-Host "Unable to communicate on serial connection at $ComPort" -ForegroundColor Yellow
        Write-Host $_.Exception.Message
    }
    finally 
    {
        if ($Port -and $Port.IsOpen) {
            $Port.DiscardOutBuffer()
            $Port.DiscardInBuffer()
            if ($Port.BytesToRead -gt 0) {
                $null = $Port.ReadExisting()
            }
            $Port.Close()
        }

        $Port.Dispose()
        $Port = $null
    }
    return $Connected
}

function Send-PduSerialCommand
{
    param([string]$Action)

    $Port = New-Object System.IO.Ports.SerialPort $ComPort,115200,None,8,one

    try
    {
        $Port.ReadTimeout = 3000
        $Port.WriteTimeout = 3000

        $Port.Open()
        Start-Sleep 5
        $Port.DiscardOutBuffer()
        $Port.DiscardInBuffer()
        $null = $Port.ReadExisting()

        Write-Host "Using SERIAL connection..." -ForegroundColor Green
        $Port.WriteLine("")
        Start-Sleep 2

        Write-Host "Sending Username to PDU..."
        $Port.WriteLine($Username)
        Start-Sleep 2

        Write-Host "Sending Password to PDU..."
        $Port.WriteLine($Password)
        Start-Sleep 5

        # Sending [ cycle | on | off ] command to PDU.
        Write-Host "Sending '$Action' command to PDU..."
        $Port.WriteLine("device; load $LoadNumber; $Action force")
        Start-Sleep 2

        # "Sending Exit command to PDU..."
        $Port.WriteLine("exit; exit; exit")
        Start-Sleep 2
    }
    catch
    {
        Write-Host "Unable to communicate with $ComPort"
        Write-Host $_.Exception.Message
    }
    finally 
    {
        if ($Port -and $Port.IsOpen) {
            $Port.DiscardOutBuffer()
            $Port.DiscardInBuffer()
            if ($Port.BytesToRead -gt 0) {
                $null = $Port.ReadExisting()
            }
            $Port.Close()
            Write-Host "Closed serial port $ComPort"
        }
        if ($Port)
        {
            $Port.Dispose()
        }
        $Port = $null
    }
}

function Test-PduNetworkConnection
{
    [bool]$Connected = $false

    try
    {
        $Client = New-Object System.Net.Sockets.TcpClient
#        $Client.Connect($PduIP,$SshPort)

        $Result = $Client.BeginConnect($PduIP,$SshPort,$null,$null)
        if (-not $Result.AsyncWaitHandle.WaitOne(3000))
        {
            throw "Connection timeout"
        }
        $Client.EndConnect($Result)

        $Stream = $Client.GetStream()
        Start-Sleep 1

        $Buffer = New-Object byte[] 1024
        $Stream.ReadTimeout = 3000
        $Bytes = $Stream.Read($Buffer,0,$Buffer.Length)

        $Banner = [System.Text.Encoding]::ASCII.GetString($Buffer,0,$Bytes)

        if ($Banner -match "login|localadmin|PowerAlert|Ubuntu") {
            Write-Host "PDU detected on network connection at IP: $PduIP, Port: $SshPort"
            $Connected = $true
        }
        else {
            Write-Host "PDU NOT detected on network connection at IP: $PduIP, Port: $SshPort"  -ForegroundColor Yellow
        }

        $Client.Close()
    }
    catch
    {
        Write-Host "Unable to establish network connection at IP: $PduIP, Port: $SshPort"  -ForegroundColor Yellow
        Write-Host $_.Exception.Message
    }
    return $Connected
}

function Send-PduNetworkCommand
{
    param([string]$Action)

    if (-not (Test-Path $Plink))
    {
        throw "plink.exe not found: $Plink"
    }

    Write-Host "Using NETWORK connection..." -ForegroundColor Green

    $Command = "device; load $LoadNumber; $Action force; exit; exit; exit"

    $Command | & $Plink `
        -ssh `
        -batch `
        -pw $Password `
        "$Username@$PduIP" `
}

# =========================
# Main Logic
# =========================

Write-Host ""
Write-Host "Requested Action : $Action"
Write-Host ""

# First choice = Serial
if (Test-PduSerialConnection)
{
    Send-PduSerialCommand $Action
    exit 0
}

Write-Host "Serial connection not available."
Write-Host "Trying network connection..."

# Second choice = Network
if (Test-PduNetworkConnection)
{
    Send-PduNetworkCommand $Action
    exit 0
}

Write-Host ""
Write-Host "ERROR: No PDU connection available." -ForegroundColor Red
Write-Host "  Serial : $ComPort"
Write-Host "  Ethernet : $PduIP"
Write-Host ""

exit 1