Internal ping script

Welcome Forums General PowerShell Q&A Internal ping script

Viewing 8 reply threads
  • Author
    Posts
    • #265253
      Participant
      Topics: 14
      Replies: 50
      Points: 88
      Rank: Member

      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.

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

    • #265265
      Participant
      Topics: 12
      Replies: 542
      Points: 1,319
      Helping Hand
      Rank: Community Hero
    • #265277
      Participant
      Topics: 14
      Replies: 50
      Points: 88
      Rank: Member

      Thank you very much for your answer Mr. Boutros,

      I need to find that value within the data below:

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

    • #265280
      Senior Moderator
      Topics: 10
      Replies: 192
      Points: 1,031
      Helping Hand
      Rank: Community Hero

      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).

    • #265286
      Participant
      Topics: 12
      Replies: 542
      Points: 1,319
      Helping Hand
      Rank: Community Hero

      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

    • #265289
      Participant
      Topics: 14
      Replies: 50
      Points: 88
      Rank: Member

      Thank you Grokkit,

      Below is part of code:

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

       

       

       

    • #265454
      Participant
      Topics: 14
      Replies: 50
      Points: 88
      Rank: Member

      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.

    • #266024
      Participant
      Topics: 14
      Replies: 50
      Points: 88
      Rank: Member

      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.

    • #266042
      Participant
      Topics: 2
      Replies: 378
      Points: 503
      Helping Hand
      Rank: Major Contributor

      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.

      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.

Viewing 8 reply threads
  • You must be logged in to reply to this topic.