Created
April 9, 2012 20:56
-
-
Save jrothmanshore/2346495 to your computer and use it in GitHub Desktop.
Powershell script for sending a command to all nodes in a memcached cluster
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# | |
param( | |
[string] $command = $(throw "command is required. example .\memdata.ps1 ""get SOMEKEYNAME""") | |
) | |
# | |
function readResponse($stream) | |
{ | |
$encoding = new-object System.Text.AsciiEncoding | |
$buffer = new-object System.Byte[] 1024 | |
while ($stream.DataAvailable) | |
{ | |
$read = $stream.Read($buffer, 0, 1024) | |
Write-Output ($encoding.GetString($buffer, 0, $read)) | |
} | |
} | |
# | |
function callport($remoteHost, $port = 11211) | |
{ | |
try { | |
$socket = new-object System.Net.Sockets.TcpClient($remoteHost, $port) | |
if ($socket -eq $null) { return; } | |
$stream = $socket.GetStream() | |
$writer = new-object System.IO.StreamWriter($stream) | |
$writer.WriteLine($command) | |
$writer.Flush() | |
start-sleep -m 500 | |
readResponse $stream | |
} catch [Exception] { | |
$ex = $_.Exception | |
Write-Output ("Error: {0}" -f $ex.Message) | |
} | |
} | |
# all servers in the cluster | |
$servers = @( "SERVER1", "SERVER2", "SERVER3") | |
$servers |% { | |
$remoteHost = [string]$_ | |
Write-Output $remoteHost | |
# change value below to your memcached port if not the default of 11211 | |
# and call callport as callport $remoteHost YourPortNumber | |
callport $remoteHost | |
} | |
# script end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment