Reboot Billion BiPAC 7404VGOX from Batch File

I need to reboot a Billion BiPAC 7404VGOX on a schedule. The router OS does not have that feature, but it does have a Telnet interface.

I can telnet to it's IP address and it prompts for a user. I give it the user & it prompts for a password. I give it the password then it waits for a command. I give is SYSTEM RESTART and it reboots.

How to put that in a batch file?

Telnet 192.168.50.200
admin
password
system restart

I tried the above and it just prompts me for a user. Maybe needs a vbs?
snooflehammerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

NVITEnd-user supportCommented:
Try this. Make a .bat file:

Set username=admin
SET password=password
SET servername=192.168.50.200
Set cmd=system restart
echo %username%> telnetcmd.dat
echo %password%>> telnetcmd.dat
echo %cmd%>> telnetcmd.dat

telnet %servername% < telnetcmd.dat
snooflehammerAuthor Commented:
Sadly doesn't work and no errors. Script just runs and closes. I added pauses on each line but no feedback.

Manually, when I open a Telnet session is prompts as follows - Login :

So I changed username to login. No joy
Bill PrewIT / Software Engineering ConsultantCommented:
What telnet client are you using, just the built in Windows one?

~bp
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

snooflehammerAuthor Commented:
yes. script has to run off a windows box
oBdACommented:
Here's a Powershell version (should run on PS2.0 or later). You can pass it an array of strings or a script file name to send to the device.
It'll produce some console output (white for what it sends, green for the replies from the device), and at the end send the whole conversation to the output pipeline for further inspection and/or processing (or a log file, if specified).
I called it Invoke-Telnet.ps1; to test (maybe with some more harmless commands than "system restart"), you should be able to run something like this in a PS console:
$Session = .\Invoke-Telnet.ps1 -ComputerName 192.168.50.200 -Script "admin", "password", "system restart"

Open in new window

From batch or directly from task scheduler, with log file:
Powershell.exe -ExecutionPolicy RemoteSigned -Command "& 'C:\Temp\Invoke-Telnet.ps1'" -ComputerName 192.168.50.200 -Script "admin", "password", "system restart" -LogFile "C:\Temp\reboot.log"

Open in new window

Or to pull the script from a file:
Powershell.exe -ExecutionPolicy RemoteSigned -Command "& 'C:\Temp\Invoke-Telnet.ps1'" -ComputerName 192.168.50.200 -Path "C:\Temp\reboot.txt" -LogFile "C:\Temp\reboot.log"

Open in new window

Not a perfect telnet client, but should work for your purposes.
[CmdletBinding(DefaultParameterSetName="ScriptArray")]
Param(
	[Parameter(Mandatory=$True)]
	[String]$ComputerName,
	[Parameter(Mandatory=$True, ValueFromPipeline=$True, ParameterSetName="ScriptArray")]
	[String[]]$Script,
	[Parameter(Mandatory=$True, ValueFromPipeline=$False, ParameterSetName="ScriptFile")]
	[String]$Path,
	[uint16]$Port = 23,
	[uint32]$CommandDelay = 1000,
	[Switch]$NoHello,
	[ConsoleColor]$CommandColor = "White",
	[ConsoleColor]$ResponseColor = "Green",
	[String]$LogFile
)

Begin {
	$BufferSize = 1024
	$Log = @()
	$Index = 0
	If ($Path) {
		If (Test-Path -Path $Path) {
			$ScriptArray = Get-Content -Path $Path
		} Else {
			Throw "Script path '$($Path)' not found!"
		}
	} Else {
		$ScriptArray = @()
	}
	If ($LogFile) {
		If (-Not (Test-Path -Path (Split-Path -Path $LogFile -Parent))) {
			Throw "Log file folder '$($LogFile)' not found!"
		}
	}
}

Process {
	If (-Not $Path) {
		$ScriptArray += $Script
	}
}

End {
	Try {
		"Establising network connection to $($ComputerName):$($Port) ..." | Write-Verbose
		$TcpClient = New-Object -TypeName "System.Net.Sockets.TcpClient" -ArgumentList $ComputerName, $Port -ErrorAction Stop
		"... OK." | Write-Verbose
		$Stream = $TcpClient.GetStream()
		$StreamWriter = New-Object -TypeName "System.IO.StreamWriter" -ArgumentList $Stream
		$StreamWriter.AutoFlush = $True
		$Buffer = New-Object -TypeName "System.Byte[]" -ArgumentList $BufferSize
		$Encoding = New-Object System.Text.AsciiEncoding
		$Command = "<TcpConnect>"
		$Timeout = $False
		$StopWait = (Get-Date).AddSeconds(10)
		"Waiting for initial response ..." | Write-Verbose
		Do {
			Start-Sleep -Milliseconds 100
			$Timeout = (Get-Date) -gt $StopWait
		} Until ($NoHello -Or $Stream.DataAvailable -Or $Timeout)
		If ($Timeout -And -Not $NoHello) {
			"Established connection to $($ComputerName):$($Port), but got no initial response; use the switch -NoHello to send the script anyway." | Write-Warning
		} Else {
			"... OK." | Write-Verbose
			$Response = ""
			While ($Stream.DataAvailable) {
				$Count = $Stream.Read($Buffer, 0, $BufferSize)
				$Response += [System.Text.Encoding]::ASCII.GetString($Buffer, 0, $Count)
			}
			$Log += "" | Select-Object -Property `
				@{Name="Index"; Expression={$Index}},
				@{Name="Command"; Expression={"<TcpConnect>"}},
				@{Name="Response"; Expression={$Response}}
			$Command | Write-Host -ForegroundColor $CommandColor
			$Response | Write-Host -ForegroundColor $ResponseColor
			"Sending script ..." | Write-Verbose
			ForEach ($Command In $ScriptArray) {
				$Index += 1
				$Command | Write-Host -ForegroundColor $CommandColor
				$StreamWriter.WriteLine($Command)
				Start-Sleep -Milliseconds $CommandDelay
				$Response = ""
				While($Stream.DataAvailable) {
					$Count = $Stream.Read($Buffer, 0, $BufferSize)
					$Response += [System.Text.Encoding]::ASCII.GetString($Buffer, 0, $Count)
				}
				$Response | Write-Host -ForegroundColor $ResponseColor
				$Log += "" | Select-Object -Property `
					@{Name="Index"; Expression={$Index}},
					@{Name="Command"; Expression={$Command}},
					@{Name="Response"; Expression={$Response}}
			}
			"... script done, leaving." | Write-Verbose
			$StreamWriter.Close()
			$TcpClient.Close()
			If ($LogFile) {
				$Log | Format-List | Out-File -FilePath $LogFile
			} Else {
				$Log | Write-Output
			}
		}
	} Catch {
		If ($LogFile) {
			$Log | Format-List | Out-File -FilePath $LogFile
			$_ | Out-String | Out-File -FilePath $LogFile -Append
		}
		Throw $_
	}
}

Open in new window

oBdACommented:
snooflehammer,
re: your message

Where exactly do you need help? What have you tried so far?
Save the script somewhere (assuming C:\Temp) as "Invoke-Telnet.ps1"
Open a Powershell console, and start with setting the execution policy; only required once, and only if it hasn't been done before (only for your account):
Set-ExecutionPolicy RemoteSigned

Open in new window

Then follow up with the telnet commands you want to send (I replaced the "system restart" with wild guesses; just pass what the device accepts, and don't forget the logoff):
$Session = C:\Temp\Invoke-Telnet.ps1 -ComputerName 192.168.50.200 -Script "admin", "password", "ls", "exit"

Open in new window


If you get the correct responses, use one of the methods I described above to schedule it.
snooflehammerAuthor Commented:
Thank you for responding, but I'm missing something fundamental here.

If I open PowerShell and give it these commands, the router reboots:

telnet 192.168.50.200
admin
password
system restart

I want to automate that.

I've created the file invoke-telnet.ps1 in a folder C:\RouterReboot with this content:

$Session = C:\RouterReboot\Invoke-Telnet.ps1 -ComputerName 192.168.50.200 -Script "admin", "password", "system restart"

Trouble is, I can't see to what end. The file contains a reference to itself & if I run it with PowerShell nothing happens except the PS window opens & closes.

What I want to achieve is a script in a file that I can schedule.

Thank you for your input
oBdACommented:
The script itself is the in the last code box in http:#a40767673 (109 lines)
The three code boxes before that are just examples, including how to call it from a scheduled task.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
snooflehammerAuthor Commented:
Thanks. I got it to work via a batch file and a .vbs.

Batch File
start telnet.exe 192.168.50.200
cscript sendkeys.vbs

Open in new window


VB Script
set OBJECT=WScript.CreateObject("WScript.Shell")
WScript.sleep 50 
OBJECT.SendKeys "admin{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "mypassword{ENTER}"
WScript.sleep 50 
OBJECT.SendKeys " system restart{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys " rm log_web_activity{ENTER}" 
WScript.sleep 50 
OBJECT.SendKeys "exit{ENTER}" 
OBJECT.SendKeys " "

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.