PowerShell 中通过 Plink 运行 SSH 命令

来源:互联网 发布:手机一碰就响的软件 编辑:程序博客网 时间:2024/06/05 21:07

今天在研究 Isilon 脚本管理,稍后会详述。其中有一种方案就是直接在 PowerShell 中通过SSH来运行 Isilon 命令。经过短暂的搜索,发现基本有以下几个方案:

  1. SharpSSH ,这个方案需要下载dll库,而且只支持一个SSH Session,可以参考 Scriptable SSH from PowerShell 。
  2. NetCmdlets ,收费软件,不知道有多少人愿意掏这个钱。
  3. SSH.NET library ,这个应该是比较强大的免费方案,其最大特色就是支持做个SSH Sessions。你可以在多个Sessions之间切换。
  4. Putty/Plink ,最便宜,最简单的方案。不需要任何的 PowerShell 库。使用 Invoke-Expression 运行SSH命令,只是这样每次执行一次都要重新建立SSH连接,比较花时间。

本文讨论的就是第四种方案,参考了 Yet Another Invoke-SSH PowerShell Function (thanks PS Fab) 。里面几个地方需要注意。

第一次登陆的时候自动接受SSH key

什么意思?我们来看个例子就明白了。

2013-10-30 17_14_47-plink

怎么解决呢?非常简单,运行如下命令: ehco y | plink -ssh username@host -pw password exit

密码错时不提示输入新密码

这个是我自己加的功能,为什么要这样呢?因为如果 Plink 输入的密码不对时,它会提示你输入新的密码,可是我们是脚本操作,该脚本可能一直在等着你输入新的密码,直到超时:

2013-10-30 17_24_51

解决方案就是加入参数-batch, -batch    disable all interactive prompts

效果图:

2013-10-30 17_36_08

脚本:

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
$plinkAndPath = "C:\Personal\plink.exe"$username = "root"$password = "password"$hostname = "192.168.0.11"# Commands to execute:$Commands = @()$Commands += "ifconfig" Function Invoke-SSH{    <#    .SYNOPSIS     Uses Plink.exe to SSH to a host and execute a list of commands.    .DESCRIPTION    Uses Plink.exe to SSH to a host and execute a list of commands.    .PARAMETER hostname    The host you wish to connect to.    .PARAMETER username    Username to connect with.    .PARAMETER password    Password for the specified user account.    .PARAMETER commandArray    A single, or list of commands stored in an array object.    .PARAMETER plinkAndPath    The location of the plink.exe including the executable (e.g. F:\tools\plink.exe)    .PARAMETER connectOnceToAcceptHostKey    If set to true, it will accept the remote host key (use when connecting for the first time)    .EXAMPLE    Invoke-SSH -username root -hostname centos-server -password Abzy4321! -plinkAndPath "F:\tools\plink.exe" -commandArray $commands -connectOnceToAcceptHostKey $true    .EXAMPLE    Invoke-SSH -username root -hostname centos-server -password Abzy4321! -plinkAndPath "F:\tools\plink.exe" -commandArray ifconfig -connectOnceToAcceptHostKey $true    .NOTES    Author: Robin Malik    Source: Modified from: http://www.zerrouki.com/invoke-ssh/    #>     Param(        [Parameter(Mandatory=$true,HelpMessage="Enter a host to connect to.")]        [string]        $hostname,         [Parameter(Mandatory=$true,HelpMessage="Enter a username.")]        [string]        $username,         [Parameter(Mandatory=$true,HelpMessage="Enter the password.")]        [string]        $password,         [Parameter(Mandatory=$true,HelpMessage="Provide a command or comma separated list of commands")]        [array]        $commandArray,         [Parameter(Mandatory=$true,HelpMessage="Path to plink (e.g. F:\tools\plink.exe).")]        [string]        $plinkAndPath,         [Parameter(HelpMessage="Accept host key if connecting for the first time (the default is `$false)")]        [string]        $connectOnceToAcceptHostKey = $false    )     $target = $username + '@' + $hostname    $plinkoptions = "-ssh  -batch $target -pw $password"     # On first connect to a host, plink will prompt you to accept the remote host key.     # This section will login and accept the host key then logout:    if($ConnectOnceToAcceptHostKey)    {        $plinkCommand  = [string]::Format('echo y | & "{0}" {1} exit', $plinkAndPath, $plinkoptions )        $msg = Invoke-Expression $plinkCommand    }     # Build the SSH Command by looping through the passed value(s). Append exit in order to logout:    $commandArray += "exit"    $commandArray | % { $remoteCommand += [string]::Format('{0}; ', $_) }       # Format the command to pass to plink:    $plinkCommand = [string]::Format('& "{0}" {1} "{2}"', $plinkAndPath, $plinkoptions , $remoteCommand)     # Execute the command and display the output:    $response = Invoke-Expression $plinkCommand     if ($null -eq $response) {         write-host "Access denied!!!"    }    else {         write-output $response    }    }  Invoke-SSH -username $username -hostname $hostname -password $password -plinkAndPath $p
0 0
原创粉丝点击