Chinaunix首页 | 论坛 | 博客
  • 博客访问: 750852
  • 博文数量: 256
  • 博客积分: 3502
  • 博客等级: 中校
  • 技术积分: 3988
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-17 21:13
文章分类

全部博文(256)

文章存档

2014年(11)

2013年(134)

2012年(111)

我的朋友

分类: 系统运维

2013-12-16 10:19:17

最近我需要执行一些PowerCLI的行动从ESXi Shell你可知道目前还没有cmdlet允许您运行shell命令这是流行在社区内一个选项使用第三方工具,称为砰砰exe来运行通过SSH命令

 

下载plink.exe通过PowerShell

我在写脚本的人谁,我知道已经有安装砰砰,我想他们很少有使用该脚本时没有力气所以我想分享第一招是能够检查plink.exe下载它,如果它不是同一个目录中的脚本可以看出如下:
$myDir = Split-Path -Parent $MyInvocation.MyCommand.Path
$PlinkLocation = $myDir + "\Plink.exe"
If (-not (Test-Path $PlinkLocation)){
	Write-Host "Plink.exe not found, trying to download..."
	$WC = new-object net.webclient
	$WC.DownloadFile("~sgtatham/putty/latest/x86/plink.exe",$PlinkLocation)
	If (-not (Test-Path $PlinkLocation)){
		Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: ~sgtatham/putty/latest/x86/plink.exe"
		Exit
	} Else {
		$PlinkEXE = Get-ChildItem $PlinkLocation
		If ($PlinkEXE.Length -gt 0) {
			Write-Host "Plink.exe downloaded, continuing script"
		} Else {
			Write-Host "Unable to download plink.exe, please download from the following URL and add it to the same folder as this script: ~sgtatham/putty/latest/x86/plink.exe"
			Exit
		}
	}	
}

Accept plink.exe host key automatically

Secondly when connecting to a host for the first time you will need to accept the host key and allow plink.exe to connect to the host, the message is similar to the one below:

image

At the moment there is no option in plink.exe to skip the host key checking, however after some searching and messing around I came upon a solution.

It turns out you can actually send a Y to the script you are running by simply piping “echo Y” to plink.exe, this sends a Y accepting the host key when the question is asked from the command line, this can be scripted like the below example:

Echo Y | Plink.exe…..

Hopefully someone will find these useful in the future as I did with this script.

阅读(3211) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~