ganapa2000
Hot Shot
Hot Shot

Unable to get the ssh output from vm

Jump to solution

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

 

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership

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

$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 | awk "{print}"
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 | awk "{print}"
"@

$Username = 'root'
$pass = ConvertTo-SecureString -AsPlainText 'Welcome2022!' -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 = ''
  }
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership

Is anything returned when you simplify your grep command?

grep "account1\|account2\|%account3\|#Defaults requiretty" /etc/sudoers

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Tags (1)
0 Kudos
ganapa2000
Hot Shot
Hot Shot

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

0 Kudos
LucD
Leadership
Leadership

And via SSH?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot

LucD,

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

0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

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

$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 | awk "{print}"
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 | awk "{print}"
"@

$Username = 'root'
$pass = ConvertTo-SecureString -AsPlainText 'Welcome2022!' -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 = ''
  }
}

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
ganapa2000
Hot Shot
Hot Shot

That worked...thank you very much LucD 🙂

0 Kudos