PowerShell 中通过 Plink 运行 SSH 命令
来源:互联网 发布:手机一碰就响的软件 编辑:程序博客网 时间:2024/06/05 21:07
今天在研究 Isilon 脚本管理,稍后会详述。其中有一种方案就是直接在 PowerShell 中通过SSH来运行 Isilon 命令。经过短暂的搜索,发现基本有以下几个方案:
- SharpSSH ,这个方案需要下载dll库,而且只支持一个SSH Session,可以参考 Scriptable SSH from PowerShell 。
- NetCmdlets ,收费软件,不知道有多少人愿意掏这个钱。
- SSH.NET library ,这个应该是比较强大的免费方案,其最大特色就是支持做个SSH Sessions。你可以在多个Sessions之间切换。
- Putty/Plink ,最便宜,最简单的方案。不需要任何的 PowerShell 库。使用 Invoke-Expression 运行SSH命令,只是这样每次执行一次都要重新建立SSH连接,比较花时间。
本文讨论的就是第四种方案,参考了 Yet Another Invoke-SSH PowerShell Function (thanks PS Fab) 。里面几个地方需要注意。
第一次登陆的时候自动接受SSH key
什么意思?我们来看个例子就明白了。
怎么解决呢?非常简单,运行如下命令: ehco y | plink -ssh username@host -pw password exit
密码错时不提示输入新密码
这个是我自己加的功能,为什么要这样呢?因为如果 Plink 输入的密码不对时,它会提示你输入新的密码,可是我们是脚本操作,该脚本可能一直在等着你输入新的密码,直到超时:
解决方案就是加入参数-batch, -batch disable all interactive prompts
效果图:
脚本:
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
- PowerShell 中通过 Plink 运行 SSH 命令
- ssh自动登录并执行命令以及plink
- ssh plink 端口转发
- 通过Plink,SOCKS上网
- 通过PowerShell Server使用ssh连接windows
- Exchange通过powershell创建帐号的命令
- Windows PowerShell中scp命令
- PowerShell中查看命令帮助
- 使用Plink -ssh与Linux交互
- plink 与 ssh 远程登录问题
- Powershell 中直接运行 Python 的文件
- 如何在PowerShell中运行virtualenv?
- 如何在PowerShell中运行VIM
- PowerShell命令
- PowerShell命令
- Powershell命令
- 通过STAF Process Service调用Powershell执行命令或脚本
- PowerShell 2.0在远程主机上运行命令和脚本
- shell 管理 uwsgi 进行重启 防止多个进行 导致服务器挂掉
- 如何做好机器人方向的毕业设计?
- 用mm编译APK时大量can't find referenced method的问题的解决
- linux 下安装jdk
- 销项税额
- PowerShell 中通过 Plink 运行 SSH 命令
- 如何把caj转成pdf
- recycleview平分间隔
- 二维数组与行指针
- Android性能优化摘录
- 20161214C语言基础11_运算符运用
- ANDROID学习之路
- CSS及选择器归纳
- Android------startActivityForResult的详细用法--返回数据