| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- import subprocess, base64
- ps1_content = r'''
- [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
- $portName = 'COM4'
- $baud = 115200
- $port = New-Object System.IO.Ports.SerialPort $portName, $baud, 'None', 8, 'One'
- $port.Handshake = [System.IO.Ports.Handshake]::None
- $port.DtrEnable = $false
- $port.RtsEnable = $false
- $port.ReadTimeout = 1000
- $port.WriteTimeout = 1000
- try {
- $port.Open()
- [Console]::WriteLine("PORT_OPEN_OK: " + $portName + " at " + $baud)
- $port.DiscardInBuffer()
- $port.DiscardOutBuffer()
- # Passive listen for 1 second
- $sw = [System.Diagnostics.Stopwatch]::StartNew()
- while ($sw.ElapsedMilliseconds -lt 1000 -and $port.BytesToRead -eq 0) {
- Start-Sleep -Milliseconds 50
- }
- $available = $port.BytesToRead
- [Console]::WriteLine("PASSIVE_BYTES: " + $available)
- if ($available -gt 0) {
- [byte[]]$rx = New-Object byte[] $available
- $readCount = $port.Read($rx, 0, $available)
- [Console]::WriteLine("RX_COUNT: " + $readCount + " HEX: " + [BitConverter]::ToString($rx))
- }
- } catch {
- [Console]::WriteLine("ERROR: " + $_.Exception.ToString())
- } finally {
- if ($port.IsOpen) { $port.Close() }
- $port.Dispose()
- }
- '''
- enc = base64.b64encode(ps1_content.encode('utf-16le')).decode('ascii')
- cmd = ['ssh', 'Administrator@192.168.2.24', f'powershell -NoProfile -NonInteractive -EncodedCommand {enc}']
- res = subprocess.run(cmd, capture_output=True, text=True)
- print('STDOUT:')
- print(res.stdout)
- print('STDERR:')
- print(res.stderr)
|