Automate with powershell is becoming a trendy operation spread by many cloud administrators. In fact, by using a single language, is possible to automate many IT processes without become crazy passing through user interfaces. Talking about AWS EC2 there is a lot of APIs that could fit quite all deploy and management task. But IMHO, managing the instance in using the “traditional way” (by a ssh client) could result not so easy, especially in powershell environment.

In this post I’ll show how to use Posh-SSH (showed in previous post here: https://blog.linoproject.net/powershell-managing-ubuntu-with-sudo-and-posh-ssh/), SSH client from Git to help you managing an EC2 instance using Powershell. This article will end with a tip to configure ssh connection with RoyalTS using the key provided by AWS during EC2 deployment.

Requirements and notes

Every time you create an EC2 instance a key-pair should be applied to that to ensure a stronger access than username and password. Obviously if you loose that you’re not able to access to VM; for this reason you should keep the key.pem in a safe place!

Keeping the focus on requirements:

  • Set  allowed ip with the ip where you’re connecting from, to the  port 22 inbound rule in security group attached to the instance
  • Get the path to the key file
  • a username specified in connection details showed in connection dialog (Right click to instance then choose connect item)… in my case is “ubuntu”

image

image

Note: during connection or for test purpose, you could keep source to 0.0.0.0/0 as inbound rule, but don’t forget to restrict this policy in production.

PoshSSH

After getting the installation info from my previous post, you should connect to instance without specifying the password. To do it, simply prepare a no-password credentials using the instruction PSCredentials:

$nopasswd = new-object System.Security.SecureString
$Crendential= New-Object System.Management.Automation.PSCredential ("ubuntu", $nopasswd)

Now with New-SSHSession is possible to start working with stream command and variables:

New-SSHSession –ComputerName [instance_ip_fqdn] -KeyFile '\Path\To\Key.pem' -Credential $Crendential

image

And sending ssh command in opened ssh session could be done simply:

Invoke-SSHCommand -SessionId 0 -Command "pwd"

image

After operation you could close the connection using Remove-SSHSession cmdlet:

Remove-SSHSession -SessionId 0

SSH Client from Git

As a real alternative to Putty, in a powershell console, you could use Git ssh. Its Installation and configuration could be realized with the following commands issued with administrator rights:

#install chocolatey(a cool package system for windows)
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))
#install git using choco
choco install -y git -params "/GitAndUnixToolsOnPath"

Now setting the path environment variable is possible use ssh in every directory…

$new_path = "$env:PATH;C:/Program Files/Git/usr/bin"
$env:PATH=$new_path

It’s time to start using the ssh client with key.pem

ssh -i '\path\to\Key.pem' ubuntu@[instance_ip_fqdn]

A Tip for RoyalTS

For RoyalTS lover (like me) the procedure is very simple:

  1. Create new terminal connection (use Putty plugin)
  2. In “Credentials” step, under Credential tab, select “Specify username and password”; then type the username specified with the EC2 connection (in my case ubuntu)
  3. Under “Private Key File” Choose “Embed Private Key File” and select the path to the key.pem file. Obviously don’t specify any passphrase.

That’s all Folks!

By admin

Utilizzando il sito, accetti l'utilizzo dei cookie da parte nostra. Using this site you accept cooking utilization. maggiori informazioni more Informations

Questo sito utilizza i cookie per fornire la migliore esperienza di navigazione possibile. Continuando a utilizzare questo sito senza modificare le impostazioni dei cookie o cliccando su "Accetta" permetti il loro utilizzo. This site uses cookies to provide the best browsing experience possible. By continuing to use this website without changing your cookie settings or clicking "Accept" allow their use.

Chiudi Close