-
Notifications
You must be signed in to change notification settings - Fork 0
/
based off..ps1
171 lines (170 loc) · 11.9 KB
/
based off..ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
## 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:
## ./PowerShellTelnetRemoteSession.ps1 "127.0.0.1" "23" "admin" "password" "term len 0" "en" "enablepassword" "show interface"
param(
#[string] $remoteHost = "172.31.0.111",
#[int] $port = 23,
[string] $username = "admin",
[string] $password = "",
#[string] $termlength = "term len 0", #Useful for older consoles that have line display limitations
[string] $enable = "en", #Useful for appliances like Cisco switches that have an 'enable' command mode
[string] $enablepassword = "",
[string] $command1 = "show run", #You can add additional commands below here with additonal strings if you want
[string] $command2 = " ",
[string] $command3 = " ",
[string] $command4 = " ",
[string] $command5 = " ",
[int] $commandDelay = 1000
)
function Main
{
param ($ip,$port)
## Open the socket, and connect to the computer on the specified port
Write-Host "Connecting to $ip on port $port"
$socket = New-Object System.Net.Sockets.TcpClient($ip, $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($termlength)
#$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($enable)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($enablepassword)
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command1) #Add additional entries below here for additional 'strings' you created above
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command2) #Add additional entries below here for additional 'strings' you created above
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command3) #Add additional entries below here for additional 'strings' you created above
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command4) #Add additional entries below here for additional 'strings' you created above
$writer.Flush()
Start-Sleep -m $commandDelay
$writer.WriteLine($command5) #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()
#building file name
$filename = "c:\switch\$ip - " + $timestamp + ".txt"
$output | Out-File $filename #Change this to suit your environment
}
#build the list of ips to be interegated
$ips = "192.168.1.2","169.254.2.2","192.168.1.101"
#building a file path
$timestamp = get-date -Format g | foreach {$_ -replace ":","."}
#building different file paths for different functions
$errortimestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
$errorfilename = "c:\switch\" + $timestamp + " - ERROR.txt"
#building different file paths for different functions
$blockedtimestamp = Get-Date -Format o | foreach {$_ -replace ":", "."}
$blockedfilename = "c:\switch\" + $timestamp + " - blocked.txt"
#looping through each ip in ip list
foreach ($ip in $ips)
{
Write-host "Testing $ip"
#test to see if device is responding to pings
if (Test-Connection $ip -Quiet)
{
#creating connection on port 23
$t = New-Object Net.Sockets.TcpClient
$t.Connect($ip, 23)
#if it connects, runs the required function with the ip
if ($t.Connected)
{
. Main $ip "23"
}
#script block for device responding to ping, but port 23 is NOT open
else
{
$portblocked = "Port 23 on $ip is closed , You may need to contact your IT team to open it. "
Add-Content $blockedfilename "`n$portblocked"
}
}
#script block for NO RESPONSE
else
{
$errorfilename = "c:\switch\" + $timestamp + " - ERROR.txt"
Add-Content $errorfilename "`nCould not ping $ip"
}
}
#displaying information on console
Write-Host "Build file list" -NoNewline
#getting file list to be emailed
$files = Get-ChildItem C:\Switch\ | Where {-NOT $_.PSIsContainer} | foreach {$_.fullname}
#pausing script
Start-Sleep 3
Write-Host "`t" [File List Built] -ForegroundColor "Green"
Write-Host "Sending Email" -NoNewline
#building checks for sending emails
#no error
#replace as needed
$to = "<to>"
$from = "<from>"
$smtpserver = "<smtpserver>"
try
{
Send-MailMessage -Attachments $files -to $to -from $from -Subject "Network Config backup - $timestamp" -SmtpServer $smtpserver -ErrorAction Stop
Write-Host "`t [Email Sent]" -ForegroundColor "Green"
}
#error
catch
{
$ErrorMessage = $_.Exception.Message
#$ErrorMessage
if ($ErrorMessage -ne $null)
{
Write-Host "`t [Unable to send Mail]" -ForegroundColor "Red"
Write-Host "There was an error: $ErrorMessage" -ForegroundColor "Yellow"
}
}
Start-Sleep 3
Write-Host "Removing Files" -NoNewline
$files | Remove-Item
Write-Host "`t [Files removed]" -ForegroundColor "Green"