Automation

 View Only
  • 1.  Unable to get the ssh output from vm

    Posted May 30, 2022 11:01 AM

    Hi,

    I am unable get the output from below script from the vm remotely. please help.

    If I execute the command directly on the vm, I get the desired output but i am unable to get the output from grep command in the output

    please help!!

    $script = @"
    GREEN=$'\e[0;32m'
    RED=$'\e[0;31m'
    NC=$'\e[0m'

    echo ""
    echo "${GREEN}Checking centrifydc.conf for 3 parameters exists :: account1 - account2 - account3"
    echo ""
    grep -n -i "account1\|account2\|account3" --color=always /etc/centrifydc/centrifydc.conf
    echo ""
    echo ""
    echo "${GREEN}Checking sudoers file for 4 parameters exists :: account1 - account2 - account3 - #Defaults requiretty"
    echo ""
    grep -n -i "account1\|account2\|%account3\|#Defaults requiretty" --color=always /etc/sudoers
    "@

    $Username = 'root'
    $pass = ConvertTo-SecureString -AsPlainText 'password' -Force
    $Cred = New-Object System.Management.Automation.PSCredential -ArgumentList $Username,$pass
    Import-Csv -Path "D:\MyVM.csv" -UseCulture -PipelineVariable row |
    ForEach-Object -Process {
    try {
    $session = New-SSHSession "192.168.1.100" -Credential $Cred -AcceptKey -ErrorAction Stop
    $connected = $session.Connected
    $result = Invoke-SSHCommand -SSHSession $session -Command $script -Verbose
    $result.Output.Replace("`r`n",'|').Split('|').Where{$_ -ne ''}
    Remove-SSHSession $session -Verbose | Out-Null
    }
    catch {
    $connected = $false
    $result = ''
    }
    }

     

    I am getting the output from echo command but not from grep command

    ganapa2000_0-1653908445046.png

     



  • 2.  RE: Unable to get the ssh output from vm

    Posted May 30, 2022 11:17 AM

    Is anything returned when you simplify your grep command?

    Does the grep command return anything when you run it locally in the Guest OS?



  • 3.  RE: Unable to get the ssh output from vm

    Posted May 30, 2022 11:29 AM

    LucD,

    When I simplify and run the command, I am getting the output in the OS

    [root@BMAS ~]# grep "account1\|account2\|account3\|#Defaults requiretty" /etc/sudoers
    %account1 ALL=(ALL) ALL
    account2 ALL=(ALL) ALL
    account2 ALL=(ALL) ALL
    #Defaults requiretty



  • 4.  RE: Unable to get the ssh output from vm

    Posted May 30, 2022 11:50 AM

    And via SSH?



  • 5.  RE: Unable to get the ssh output from vm

    Posted May 30, 2022 11:59 AM

    LucD,

    yes via ssh, I am getting the above output but through the script, grep is not working



  • 6.  RE: Unable to get the ssh output from vm

    Posted May 30, 2022 12:02 PM

    I mean, do you get any output from the simplified grep command when you execute it via Invoke-SshCommand?



  • 7.  RE: Unable to get the ssh output from vm
    Best Answer

    Posted May 30, 2022 01:06 PM

    Looks like this is a known 'feature' of the grep command.
    A simple solution seems to be to pipe the grep output to awk.
    Works for me

     



  • 8.  RE: Unable to get the ssh output from vm

    Posted May 30, 2022 04:06 PM

    That worked...thank you very much LucD