linux - 使用 PowerShell 从 com 端口获取 Linux 设备 IP 地址

标签 linux windows powershell serial-port ip-address

我目前正在设置一个运行 Linux 的基于 ARM 的测试板,我需要从 Windows 机器访问它。该测试板使用 USB 转串口电缆通过 com 端口连接。我正在使用 PowerShell 来运行命令和控制测试板。 我被困在需要查询测试板以找出其 IP 地址并将其存储在我的 Windows 主机上的变量中的阶段。

这是我用来显示 IP 地址的内容:

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

当我从控制台读取输出时,我得到了 ifconfig 命令以及 IP 地址到我的变量中:

$ipAddr = $port.ReadExisting()
$ipAddr

输出是这样的:

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

我无法仅获取字符串形式的 IP 地址。我的方法是正确的还是我在这里做错了什么?查询 IP 地址的最佳方式是什么?

最佳答案

只需使用子字符串或正则表达式来提取您想要的值。

$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

结果

123.33.33.150

关于linux - 使用 PowerShell 从 com 端口获取 Linux 设备 IP 地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48070630/

相关文章:

c++ - 返回引用时,gcc 将 0x30 添加到 %eax

c - 在c中使用互斥锁进行多线程并一次运行一个线程

c# - WPF 应用程序的不同表示 - 运行时与设计器

c++ - 在 Windows 上存储信息有哪些选项?我应该如何阅读这些信息?

powershell - 如何通过Powershell配置新的Azure AD应用程序?

powershell : replace character in TXT file in certain position

Linux CLI 查找没有子路径的 dir 路径 - 结果只有父路径

linux - 如何在 linux 的 hdfs super 组中添加用户?

regex - 在 PowerShell 中使用加号重命名文件

windows - cmd 或 powershell 获取所有子目录