Use PowerShell to automatically log in to Linux and execute commands without a password through Posh-SSH

Synology NAS uses the key for password-free authentication SSH access. I tried the setting but couldn't get in. So I tried other methods and found this PowerShell plug-in Posh-SSH to achieve https://github.com/darkoperator/Posh-SSH, The specific installation steps are as follows.

View Powell version command: Get-Host | Select-Object Versionimage.png


Posh-SSH requires 5.1 or 7.x or above, and powershell needs to be upgraded.

The machine is server2012 R2 version, download link:

https://download.microsoft.com/download/6/F/5/6F5FF66C-6775-42B0-86C4-47D41F2DA187/Win8.1AndW2K12R2-KB3191564-x64.msu

After installation, you will be asked to restart the system, check the version after completion

image.png

Try to install according to the command prompt on GIT: Install-Module -Name Posh-SSH

An error message pops up: PowerShellGet requires NuGet provider version '2.8.5.201' or newer to interact with NuGet-based repositories.

image.png

Use the following 2 commands to solve this problem

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Install-PackageProvider -Name NuGet

image.png

Run the command again: Install-Module -Name Posh-SSH

image.png

The installation is complete, and then you can write PowerShell script operations

$Password = "abcd1234562"
$User = "admin"
$ComputerName = "192.168.1.1"
$Command = "ls"

$secpasswd = ConvertTo-SecureString $Password -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential($User, $secpasswd)

$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials #Connect Over SSH

Invoke-SSHCommand -Index $sessionid.sessionid -Command $Command # Invoke Command Over SSH

###If ssh is not using the standard 22 port, you can specify it here

$SessionID = New-SSHSession -ComputerName $ComputerName -Credential $Credentials -Port 1234

###Or execute the sh script and specify it at the Command: here is a way to find that sh shellscript cannot be executed, such as "sh /tmp/test.sh"




Guess you like

Origin blog.51cto.com/helpdesk/2676433
Recommended