Internal ping script

Hello guys, I hope you are all well and healthy!

I’m creating a script that accesses a certain device via SSH and the one inside it pinges some ip. I’ve already managed to perform SSH, the problem is being how to get the ping return. I just need to take the ping time only and put it in a variable. Thank you in advance for your support.

Below the result of ping.

ping -brief -c 1 8.8.8.8
PING 8.8.8.8: 56 data bytes, press CTRL_C to break
!

--- 8.8.8.8 ping statistics ---
1 packet (s) transmitted
1 packet (s) received
0.00% packet loss
round-trip min / avg / max = 42/42/42 ms

I only need one of these values after equal signal(only one of them):

round-trip min / avg / max = 42/42/42 ms
$String = 'round-trip min / avg / max = 42/42/42 ms'
(($String -split '=')[1].Trim() -split ' ')[0]

Thank you very much for your answer Mr. Boutros,

I need to find that value within the data below:

ping -brief -c 1 8.8.8.8
PING 8.8.8.8: 56 data bytes, press CTRL_C to break
!
--- 8.8.8.8 ping statistics ---
1 packet (s) transmitted
1 packet (s) received
0.00% packet loss
round-trip min / avg / max = 42/42/42 ms

The format must always be the same, but the values ​​are not.

PowerShell can send variable values from a remote session to a local session of PowerShell (assuming you properly create a remote session and work within the limits of remote output) but you can’t just pass variables from a Bash (or w/e) session to a PowerShell session (they don’t handle variables the same way).

To get this information back to your local PowerShell session you will probably need to use Posh-SSH. Here is an example for storing output from an SSH session. Don’t forget to close the connection with Remove-SSHSession (not shown in that example).

[quote quote=265277]ping -brief -c 1 8.8.8.8
PING 8.8.8.8: 56 data bytes, press CTRL_C to break
!
— 8.8.8.8 ping statistics —
1 packet (s) transmitted
1 packet (s) received
0.00% packet loss
round-trip min / avg / max = 42/42/42 ms[/quote]

$LinuxData = @'
ping -brief -c 1 8.8.8.8
PING 8.8.8.8: 56 data bytes, press CTRL_C to break
!
--- 8.8.8.8 ping statistics ---
1 packet (s) transmitted
1 packet (s) received
0.00% packet loss
round-trip min / avg / max = 42/42/42 ms
'@
if ($LinuxData -match 'round-trip') {
    ((($LinuxData -split 'round-trip')[1] -split '=')[1].Trim() -split ' ')[0]
} else {
    'Not found'
}

Thank you Grokkit,

Below is part of code:

Get-SSHTrustedHost | Remove-SSHTrustedHost

New-SSHSession -ComputerName $hostIP -Port $port -Credential $Cred -ConnectionTimeout 120 -OperationTimeout 120 -AcceptKey

$stream = New-SSHShellStream -Index 0

$command = Invoke-SSHStreamShellCommand -ShellStream $stream -Command "ping -brief -c 1 $service"

The output option don’t appear like showing in the example as you indicate.

 

 

Hello guys,

the script’s operation is still inconsistent. in one consultation it works, in another it doesn’t. To clarify a little more … the need is to make a ping test from inside a router to have latency information through a given link. The result needs to be returned to a variable to then send this data to a monitoring system.

The connection to the router is OK, the pass-through with the ping command as well. The problem is to seek only the data. I was unable to get the output of the command. Once again, thank you very much for your help.

Hello Guys,

 

I am still searching for a solution. It appears the problem is the SshStreamShellCommand because that is not bring the output option after variable/command.

Again, thanks for any help.

 

 

rc.

I’ve been having a play with this using the links provided by Grokkit and the code below worked OK for me. I ran this against a Linux host.

$session = New-SSHSession -ComputerName 192.168.7.22 -Credential (Get-Credential)
$result = Invoke-SSHCommand -SSHSession $session -Command "ping 192.168.7.1 -c 2"
(( $result.output | Select-String 'rtt') -split '/')[4]

This returns 2.508 which is the average round trip time. How you chop up the contents of $result.output will obviously depend on what the remote system is sending back to you. On my test host, the ping command is continous unless a count value is specified. I found that $result was empty if I didn’t specify the count and just quit the script.