Script to download Cisco configs

by openplanet at 2013-03-07 03:51:18

Hello–I found the following on the Web. It’s "almost" working, in that it creates the output file, but as you’ll see, the file contains the initial login dialog rather than the output of the "show run" command. When logged into the cisco device, the next thing after "Building configuration…" is that the config file flies by, which is what the script is meant to grab. I’m sure one of you experienced PS people will see what’s wrong. Thanks in advance. (I’ll put the actual output file after the code.)

NOTE: I’ve obviously substituted the correct IP, username, and password in the actual code.


param(
[string] $remoteHost = "remotehostip",
[int] $port = 23,
[string] $username = "username",
[string] $password = "password",
[string] $command1 = "term len 0",
[string] $command2 = "en",
[string] $command3 = "enablepassword",
[string] $command4 = "show run",
[int] $commandDelay = 1000
)

[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. Press ^D followed by [ENTER] to exit.`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($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
$SCRIPT:output += GetOutput



## Close the streams
$writer.Close()
$stream.Close()

$output |Out-File \UNCPATH\Cisco\DeviceConfigs\NAME_OF_DEVICE.txt
}
. Main


The output file gets written where expected (the % Unknown command… can be ignored, because this device happens not to need an Enable password), but the contents the inital login dialog rather then the config, which gets listed immediately after "Building config…" during a "manual" session:

???????
Username: se_config
Password:
Colo_3750#term len 0
Colo_3750#en
Colo_3750#enablepassword
Translating "enablepassword"
% Unknown command or computer name, or unable to find computer address
Colo_3750#show run
Building configuration…

Thanks in advance for any help.
by DonJ at 2013-03-07 08:01:45
I know zero about Cisco devices, but it would appear as if the device is cutting off the stream and perhaps starting a new one. You might have a bit more luck using an add-in like NetCmdlets from /n software, which provide means for automating things like Telnet, SSH, TFTP, and whatnot.
by openplanet at 2013-03-07 10:15:15
Silly me…all I had to do was increase the delay time. Thx.