darkoperator / Posh-SSH

PowerShell Module for automating tasks on remote systems using SSH
BSD 3-Clause "New" or "Revised" License
978 stars 226 forks source link

Unable to run POSH-SSH commands in some of my linux machines #545

Closed viju3010 closed 11 months ago

viju3010 commented 12 months ago

Hi Team, I am noticing the below issue when trying to list "Get-SSHTrustedHost". This happens one some of my jenkins slaves and I have multiple things including to reinstall the module.

[root@vcavst-wdc-slave1 ~]# pwsh
PowerShell 7.3.3
PS /root> Get-SSHTrustedHost
MethodInvocationException: Exception calling "GetAllKeys" with "0" argument(s): "Additional text encountered after finished reading JSON content: }. Path '', line 33, position 0."
PS /root> Get-Module Posh-SSH

ModuleType Version    PreRelease Name                                ExportedCommands
---------- -------    ---------- ----                                ----------------
Manifest   3.0.8                 Posh-SSH                            {Get-SCPItem, Get-SFTPItem, Get-SSHHostKey, Get-SSHJsonKnownHost…}
viju3010 commented 12 months ago

My Code :

# Remove trusted host keys to make host key validation succeeds
try{
    Write-host "Removing trusted host keys from the local host"
    $removeTrustedHosts = Get-SSHTrustedHost -Hostname $VC_ip | Remove-SSHTrustedHost -ErrorAction SilentlyContinue
#    Invoke-Expression "ssh-keygen -R $VC_ip"
    Write-host "Removed trusted host keys from the local host"
}
catch{
    Write-host "Could not remove trusted host keys"
    Write-Host $_.Exception|format-list -force
}

Error :

Removing trusted host keys from the local host
PS>TerminatingError(Get-SSHTrustedHost): "Exception calling "GetAllKeys" with "0" argument(s): "Additional text encountered after finished reading JSON content: }. Path '', line 33, position 0.""
Could not remove trusted host keys
System.Management.Automation.MethodInvocationException: Exception calling "GetAllKeys" with "0" argument(s): "Additional text encountered after finished reading JSON content: }. Path '', line 33, position 0."
 ---> Newtonsoft.Json.JsonReaderException: Additional text encountered after finished reading JSON content: }. Path '', line 33, position 0.
   at Newtonsoft.Json.JsonTextReader.Read()
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at SSH.Stores.JsonStore.LoadFromDisk()
   at SSH.Stores.JsonStore.OnGetKeys()
   at SSH.Stores.MemoryStore.get_HostKeys()
   at SSH.Stores.MemoryStore.GetAllKeys()
   at CallSite.Target(Closure, CallSite, Object)
   --- End of inner exception stack trace ---
MVKozlov commented 11 months ago

and what

Additional text encountered after finished reading JSON content

you see in $env:USERPROFILE\.poshssh\hosts.json ?

line 33, position 0.""

viju3010 commented 11 months ago
{
  "Keys": {
    "xx.xx.0.14": {
      "HostKeyName": "xxxx",
      "Fingerprint": "xxxxxx"
    }
  }
}
}

Looks like an additional bracket at the end

viju3010 commented 11 months ago

After removing the } in the end, the commands are working. Thank you !