I use two custom functions:
function readtelnet {
$response = ""
While($Stream.DataAvailable) {
$Read = $Stream.Read($Buffer, 0, 1024)
$response += ($Encoding.GetString($Buffer, 0, $Read))
}
return $response;
}
function waitfor {
param([string]$search, [int]$timeout)
if ($timeout -eq 0) {
$timeout = 3
}
$pause = 100
$pending = readtelnet
$myResult = $pending
$counter = 0
while ($pending -notmatch $search -and $counter -le ($timeout * (1000/$pause))) {
$counter ++
Start-Sleep -m $pause
$pending = readtelnet
$myResult += $pending
}
return $myResult
}
which I found here on the forum.
Enable Notifications SSH from Windows to Open VMS using PS
I connect to the swithch:
$SocetNew = New-Object Net.Sockets.TcpClient
$SocetNew.Connect("1.11.1.1",23)
if ($SocetNew.Connected) {
$Stream = $SocetNew.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.ASCIIEncoding
}
and then i need enter Username and Password. For that, first I try to find out whot the switch asking:
$Result = waitfor ''
$Writer.writeline('')
$Writer.Flush()
and then enter Username and Password or only Password:
$Result = waitfor 'Username'
$Writer.writeline('admin')
$Writer.Flush()
$Result = waitfor 'Password'
$Writer.writeline('***********')
$Writer.Flush()
and read the version firmvare:
$Result = waitfor 'Viersion software'
$Writer.writeline('display version')
$Writer.Flush()
Here is all scrept:
function readtelnet {
$response = ""
While($Stream.DataAvailable) {
$Read = $Stream.Read($Buffer, 0, 1024)
$response += ($Encoding.GetString($Buffer, 0, $Read))
}
return $response;
}
function waitfor {
param([string]$search, [int]$timeout)
if ($timeout -eq 0) {
$timeout = 3
}
$pause = 100
$pending = readtelnet
$myResult = $pending
$counter = 0
while ($pending -notmatch $search -and $counter -le ($timeout * (1000/$pause))) {
$counter ++
Start-Sleep -m $pause
$pending = readtelnet
$myResult += $pending
}
return $myResult
}
$SocetNew = New-Object Net.Sockets.TcpClient
try {
$SocetNew.Connect("1.11.1.1",23)
if ($SocetNew.Connected) {
$Stream = $SocetNew.GetStream()
$Writer = New-Object System.IO.StreamWriter($Stream)
$Buffer = New-Object System.Byte[] 1024
$Encoding = New-Object System.Text.ASCIIEncoding
$Result = waitfor ''
$Writer.writeline('')
$Writer.Flush()
# need read whot the switch asking with $Result
$Result = waitfor 'Username'
$Writer.writeline('admin')
$Writer.Flush()
$Result = waitfor 'Password'
$Writer.writeline('*******')
$Writer.Flush()
$Result += waitfor 'UNFAS1_A_\$ ' 30
$Writer.Flush()
$Result = waitfor 'Viersion software'
$Writer.writeline('display version')
$Writer.Flush()
# need read Version software of the switch with $Result
Start-Sleep -s 1
readtelnet
$Writer.Close()
$Stream.Close()
Write-Output "Success"
}
else {
Write-Output "Failed"
}
}
catch {
Write-Output "Failed"
}
finally {
$SocetNew.Dispose()
}