Sudeep Mohanty
Sudeep Mohanty

Reputation: 41

Get Linux device IP address from com port using PowerShell

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?

Upvotes: 0

Views: 1444

Answers (2)

postanote
postanote

Reputation: 16116

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

Upvotes: 2

kunif
kunif

Reputation: 4360

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.

Upvotes: 1

Related Questions