<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Huddled Masses &#187; SSH</title>
	<atom:link href="http://huddledmasses.org/tag/ssh/feed/" rel="self" type="application/rss+xml" />
	<link>http://huddledmasses.org</link>
	<description>You can do more than breathe for free...</description>
	<lastBuildDate>Fri, 27 Apr 2012 05:42:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
<cloud domain='huddledmasses.org' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
		<item>
		<title>Scriptable SSH from PowerShell</title>
		<link>http://huddledmasses.org/scriptable-ssh-from-powershell/</link>
		<comments>http://huddledmasses.org/scriptable-ssh-from-powershell/#comments</comments>
		<pubDate>Tue, 17 Feb 2009 16:20:32 +0000</pubDate>
		<dc:creator>Joel 'Jaykul' Bennett</dc:creator>
				<category><![CDATA[Huddled]]></category>
		<category><![CDATA[Internet]]></category>
		<category><![CDATA[PowerShell]]></category>
		<category><![CDATA[SCP]]></category>
		<category><![CDATA[Scripting]]></category>
		<category><![CDATA[SFTP]]></category>
		<category><![CDATA[SSH]]></category>

		<guid isPermaLink="false">http://huddledmasses.org/?p=1079</guid>
		<description><![CDATA[Just in the last week, I&#8217;ve seen at least four people asking about SSH from PowerShell, and at least three of those specifically were looking for something they could script. That is, they needed a way to make an SSH connection, and then run commands on the remote PC and do switches in their script [...]]]></description>
			<content:encoded><![CDATA[	<p>Just in the last week, I&#8217;ve seen at least four people asking about <span class="caps">SSH</span> from PowerShell, and at least three of those specifically were looking for something they could script.  That is, they needed a way to make an <a class="zem_slink" href="http://en.wikipedia.org/wiki/Secure_Shell" title="Secure Shell" rel="wikipedia">SSH</a> connection, and then run commands on the remote PC and do switches in their script based on the output of those commands, so they could automate complicated server-side tasks.</p>

	<p>Generally, the <em>simplest</em> answer to that is to write the scripts on the server, and then invoke them from PowerShell using the PLink executable which comes with Putty.  However, that&#8217;s not very satisfactory, and it requires you to know a Linux scripting language like Perl or Python, and also requires you to have uploaded and tested that script ahead of time. Not always practical.</p>

	<p>It&#8217;s also possible to do this using the <a href="http://www.nsoftware.com/powershell/">NetCmdlets from /n</a> and I&#8217;m constantly surprised at how unwilling people are to pay for them.</p>

	<p>Well, I&#8217;ve already written my own little workaround using <a href="http://www.tamirgal.com/home/dev.aspx?Item=SharpSsh">SharpSSH</a>, a library I <a href="http://sourceforge.net/projects/sharpssh">found on SourceForge</a> from years ago, so I figured I&#8217;d share it with you. You might want to read the <a href="http://www.codeproject.com/KB/IP/sharpssh.aspx">CodeProject Article about the first release of SharpSSH</a> if you want more information about it, but otherwise, just <a href="http://downloads.sourceforge.net/sharpssh/SharpSSH-1.1.1.13.bin.zip">download the binaries</a>, extract them, and put the three dll&#8217;s somewhere handy.  I keep mine in a &#8220;Libraries&#8221; folder within my WindowsPowerShell profile directory, so they&#8217;re easy to include in a backup of my scripts.</p>

	<p>Now, lets say that your task is to ssh into a server, do a process check to see if anyone else is logged in, and if not, restart the server immediately, otherwise, schedule a reboot for midnight. Here&#8217;s the script for that:</p>

	<div class="posh code posh" style="font-family:monospace;"><br />
<span style="color: #0066cc; font-style: italic;">New-<span style="font-style: normal;">SshSession</span></span> jaykul huddledmasses.<span style="color: #003366;">org</span> <span style="color: #666666; font-style: italic;">## You'll be prompted for a password</span><br />
<span style="color: #666699; font-weight: bold;">if</span><span style="color: #333;">&#40;</span> <span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Ssh</span></span> who <span style="color: #009900;">'\$'</span><span style="color: #66cc66;">|</span> <span style="color: #0066cc; font-style: italic;">select-<span style="font-style: normal;">string</span></span> <span style="color: #009900;">&quot;^(?!jaykul).*pts/\d&quot;</span> &nbsp;<span style="color: #333;">&#41;</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Ssh</span></span> <span style="color: #009900;">&quot;shutdown -r 0:00&quot;</span> &nbsp;<span style="color: #009900;">'\$'</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #666699; font-weight: bold;">else</span> <span style="color: #333;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066cc; font-style: italic;">Invoke-<span style="font-style: normal;">Ssh</span></span> <span style="color: #009900;">&quot;shutdown -r now&quot;</span> &nbsp;<span style="color: #009900;">'\$'</span><br />
<span style="color: #333;">&#125;</span><br />
<span style="color: #0066cc; font-style: italic;">Remove-<span style="font-style: normal;">SshSession</span></span><br />
&nbsp;</div>

	<p>Notice that I use <code>\$</code> (backslash dollar-sign) as my &#8220;expect&#8221; text.  That&#8217;s the last character on my prompt.  It occasionally backfires, so you might want to use something more precise, in my case, I usually would store a patters for the full prompt (as a regular expression): </p>

	<div class="posh code posh" style="font-family:monospace;"><span style="color: #660033; font-weight: bold;">$prompt</span> <span style="color: #66cc66;">=</span> <span style="color: #009900;">'\[jaykul@peace.*\]\$'</span></div>

	<p>Here&#8217;s the script module with the functions you&#8217;ll need. <span id="more-1079"></span> You can just dot-source it, or use it as a module in PowerShell 2. Hope this helps someone.  </p>

<script type="text/javascript" src="http://PoshCode.org/embed/877"></script>

	<p>A footnote: There&#8217;s a lot more functionality in the SharpSSH module, including support for <span class="caps">SCP</span> and <span class="caps">SFTP</span>, but there&#8217;s also a few oddities, like the SshShell object exposing your password as a plain string property (which is why I made New-SshSession not return the object by default: when you specify <code>-Passthru</code> it will print the password property to the console if you don&#8217;t trap the output in a variable). Have fun, and feel free to expand on this module by pasting new versions to <a href="http://poshcode.org/">PoshCode</a>!</p>

	<p>[New] Update: There&#8217;s now another <span class="caps">SSH</span> Library on CodePlex (<a href="http://sshnet.codeplex.net"><span class="caps">SSH</span>.NET</a>), and Joakim Svendsen over at PowerShellAdmin.com has a <a href="http://www.powershelladmin.com/wiki/SSH_from_PowerShell_using_the_SSH.NET_library">PowerShell module using <span class="caps">SSH</span>.Net</a> &#8212; you should check his out too, it even has an Enter-SshSession and the ability to Invoke-SshCommand across multiple Ssh Sessions!</p>

<div class="zemanta-pixie"><a class="zemanta-pixie-a" href="http://reblog.zemanta.com/zemified/73ec8773-92ff-4975-b1ef-407ca360acc4/" title="Zemified by Zemanta"><img class="zemanta-pixie-img" src="http://img.zemanta.com/reblog_e.png?x-id=73ec8773-92ff-4975-b1ef-407ca360acc4" alt="Reblog this post [with Zemanta]" /></a></div>]]></content:encoded>
			<wfw:commentRss>http://huddledmasses.org/scriptable-ssh-from-powershell/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

