输出ssh命令在PowerShell中
问题描述:
我想输出下面的命令在PowerShell中的文本文件,但我似乎无法文本文件来得到它的工作:输出ssh命令在PowerShell中
ssh -v [email protected] | Out-File C:\output.txt
答
与使用原生的post below规定应用程序,你可以尝试使用Start-Process,例如
Start-Process ssh "-v [email protected]" -NoNewWindow -RedirectStandardOutput stdOut.log -RedirectStandardError stdErr.log; gc *.log; rm *.log
答
在我在我的博客How to SSH from Powershell Using Putty\Plink但短期的版本做了一个详细的岗位同样的问题,工作是这段代码。但是,确保在安装腻子后尝试使用它。
Function Invoke-SSHCommands {
Param($Hostname,$Username,$Password, $CommandArray, $PlinkAndPath, $ConnectOnceToAcceptHostKey = $true)
$Target = $Username + '@' + $Hostname
$plinkoptions = "-ssh $Target -pw $Password"
#Build ssh Commands
$remoteCommand = ""
$CommandArray | % {$remoteCommand += [string]::Format('{0}; ', $_) }
#plist prompts to accept client 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)
#Write-Host $PlinkCommand
$msg = Invoke-Expression $PlinkCommand
}
#format plist command
$PlinkCommand = [string]::Format('& "{0}" {1} "{2}"', $PlinkAndPath, $plinkoptions , $remoteCommand)
#ready to run the following command
#Write-Host $PlinkCommand
$msg = Invoke-Expression $PlinkCommand
$msg
}
$PlinkAndPath = "C:\Program Files (x86)\PuTTY\plink.exe"
$Username = "remoteshell"
$Password = "pa$$w0rd"
$Hostname = "Linuxhost"
$Commands = @()
$Commands += "ls"
$Commands += "whoami"
Invoke-SSHCommands -User $Username -Hostname $Hostname -Password $Password -PlinkAndPath $PlinkAndPath -CommandArray $Commands
您的命令是否生成任何输出? – guillermooo 2011-04-01 15:52:24
是的,我们需要更多的信息来帮助你解决这个问题。 – JasonMArcher 2011-04-03 03:42:41