0

I am currently setting up a test ARM based board running Linux and I need to access it from a Windows machine. This test board is connected via the com port using a USB-to-Serial cable. I am using PowerShell to run commands and control the test board. I'm stuck at a stage where I need to query the test board to find out its IP Address and store it in a variable on my Windows host.

This is what I'm using to display the IP Address:

$port.WriteLine("ifconfig | grep -A 1 'gphy' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1")

When I read the output from the console, I get the ifconfig command as well as the IP Address into my variable:

$ipAddr = $port.ReadExisting()
$ipAddr

The output is something like:

ifconfig | grep -A 1 'gphy' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1
123.33.33.150
#

I fail to get only the IP Address as a string. Is my approach correct or am I doing something wrong here? What is the best way for querying for the IP Address?

1
  • 2
    What is puzzling you? The fact that the variable also contains the entire command? How is $port defined/created? Commented Jan 3, 2018 at 5:43

2 Answers 2

2

Just use either a substring or a regex to extract the value(s) you'd want.

$ipaddr = @"
ifconfig | grep -A 1 'gphy' | tail -1 | cut -d ':' -f 2 | cut -d ' ' -f 1
123.33.33.150
"@
$regex = '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}'
(($ipaddr | Select-String -Pattern $regex).Matches).Value

Results

123.33.33.150
Sign up to request clarification or add additional context in comments.

2 Comments

This is by far the most simple way to do this.
@postanote I was wondering if I should take the regex route. Like everyone's suggested, this is by far the easiest way. Just to add to your answer, I will get my final IP Address in $matches[0]. This all but solves my problem. Thank you!
1

In ARM Linux, it will be possible to set echo off of the port connected to Windows.
For example, if the connection port is /dev/ttyS0, add the following line to startup script.

stty -F /dev/ttyS0 -echo

However, unless you change the setting, all the write data will not be echoed back in other uses.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.