Skip to content

Instantly share code, notes, and snippets.

@jespernohr
Last active October 31, 2022 15:09
Show Gist options
  • Save jespernohr/fe37d647de8e42c2a09e to your computer and use it in GitHub Desktop.
Save jespernohr/fe37d647de8e42c2a09e to your computer and use it in GitHub Desktop.
## PowerShell: Script To Telnet To Remote Hosts And Run Commands Against Them With Output To A File ##
## Overview: Useful for Telnet connections to Cisco Switches and other devices. Can add additional command strings
## Usage Examples: Add your environment specific details into the parameters below, or include when calling the script:
## In this script it 1. change Ethernet NIC to static IP - 2. connects to Zyxel VMG1312-B10A and configure device- 3.change Ethernet NIC to DHCP
## Contributer "Chrisdee" https://github.com/chrisdee/Scripts/blob/master/PowerShell/Working/telnet/PowerShellTelnetRemoteSession.ps1
## Modified "[email protected]"
param(
[string] $remoteHost = "192.168.1.1",
[int] $port = 23,
[string] $username = "admin",
[string] $password = "1234",
[string] $enablepassword = "",
[string] $command1 = "wlan config --status primary disable",
[string] $command2 = "wan edit servicename VDSL toBridge",
[string] $command3 = "dhcpserver disable",
[string] $command4 = "firewall config disable",
[string] $command5 = "save",
[string] $command6= "logout",
[int] $commandDelay = 2000
)
[string] $output = ""
## Read output from a remote host
function GetOutput
{
## Create a buffer to receive the response
$buffer = new-object System.Byte[] 1024
$encoding = new-object System.Text.AsciiEncoding
$outputBuffer = ""
$foundMore = $false
## Read all the data available from the stream, writing it to the
## output buffer when done.
do
{
## Allow data to buffer for a bit
start-sleep -m 1000
## Read what data is available
$foundmore = $false
$stream.ReadTimeout = 1000
do
{
try
{
$read = $stream.Read($buffer, 0, 1024)
if($read -gt 0)
{
$foundmore = $true
$outputBuffer += ($encoding.GetString($buffer, 0, $read))
}
} catch { $foundMore = $false; $read = 0 }
} while($read -gt 0)
} while($foundmore)
$outputBuffer
}
function Main
{
## Open the socket, and connect to the computer on the specified port
write-host "Connecting to $remoteHost on port $port"
trap { Write-Error "Could not connect to remote computer: $_"; exit }
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port)
write-host "Connected. Please Wait for Job to Finish....`n"
$stream = $socket.GetStream()
$writer = new-object System.IO.StreamWriter $stream
## Receive the output that has buffered so far
$SCRIPT:output += GetOutput
$writer.WriteLine($username)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($password)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($enablepassword)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command3)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command5)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command6) #Add additional entries below here for additional 'strings' you created above
$writer.Flush()
Start-Sleep -m $commandDelay
$SCRIPT:output += GetOutput
## Close the streams
$writer.Close()
$stream.Close()
$output | Out-File ("C:\$remoteHost.txt") #Change this to suit your environment
}
# Creates a variable for network adapter with descritip like "Gigabit"
$NIC = Get-WmiObject -Class "Win32_NetworkAdapterConfiguration" -Filter "Description LIKE '%Giga%'"
# Uses variable to change Lan adapter to static IP adress.
$NIC.EnableStatic("192.168.1.10","255.255.255.0")
# Running main config
. Main
# Uses variable to change Lan adapter to dynamic IP adress.
$NIC.EnableDHCP()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment