Developing an SSH Configuration Helper with PowerShell
Engin Polat
Happiest Senior Software Engineer at Microsoft ??♂? | C#, Typescript, Bicep, Docker, Kubernetes, Go, Terraform, Helm, DevOps, ...
Original article and the source code of the SSH Configuration PowerShell Script published at Kiev repo on my GitHub account (polatengin).
In projects, we usually need to open SSH connections to remote systems.
For example, we can open an SSH connection to a remote machine from within the Visual Studio Code (you need to have Remote Development extension pack) and develop the project inside of the remote machine.
This scenario allows us to have a thin machine, but still, develop the project with a much more powerful machine with lots of RAM, CPU and even GPU resources on Azure.
But to do that, we need to edit SSH Configuration file
We can find SSH Configuration file in ~/.ssh/config or C:\Users\{USER}\.ssh\config path
Configuration Helper
You can use Configuration Helper PowerShell Script to make it easier to edit SSH Configuration file
Menu
Reference: ssh_config.ps1#L40
You can choose 1 of the following Menu Items
- Login Azure
- List VM Machines
- Start VM Machine(s)
- Stop VM Machine(s)
- Clean SSH Config File
- Add SSH Config for Azure VMs
- Help
- Quit
Login to Azure
Reference: ssh_config.ps1#L77
You can login to Azure Portal via Azure CLI to access to the VMs running on Azure.
It's checking if the user is already logged-in to the Azure Portal, if not, it launches a new browser page to login to the Azure Portal.
If only 1 subscription is available for the user, it selects that subscription, if there is more than 1 subscription, it asks the user to choose one of them, and, it sets that subscription as Default.
So, all the upcoming commands will be executed on selected subscription, such as, getting the list of VMs, etc.
List VM Machines
Reference: ssh_config.ps1#L108
It's getting all the VMs on Azure Subscription and list them with Names and Power States
Start VM Machine(s)
Reference: ssh_config.ps1#L120
It's getting all the VMs on Azure Subscription, list them with checkboxes, selected VMs will be started
Stop VM Machine(s)
Reference: ssh_config.ps1#L133
It's getting all the VMs on Azure Subscription, list them with checkboxes, selected VMs will be stopped
Clean SSH Configuration File
Reference: ssh_config.ps1#L146
It parse the SSH Configuration file and creates a ConfigItem array.
Lists ConfigItem array with checkboxes, selected ConfigItems will be preserved, others will be removed from the SSH Configuration file
Add SSH Config for Azure VMs
Reference: ssh_config.ps1#L232
It gets all the VMs on the Azure Subscription, lists them with checkboxes, for the selected VMs a new ConfigItem element will be created.
All the ConfigItems will be appended to the SSH Configuration file
Help
Reference: ssh_config.ps1#L281
References
Write-Menu function for PowerShell (created by QuietusPlus) helped a-lot during the development of this script.