expect 脚本:从 ifconfig 解析 ip 地址
expect script: parse ip address from ifconfig
我正在尝试从 ifconfig 命令解析 IP 地址。当我在命令行上执行时它工作正常
pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*//p'
192.168.1.112
但是当我在 expect 脚本中使用它时出现错误。
#!/usr/bin/expect
set pidBash [ spawn bash ]
set ipIfConfig {ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*//p'}
set clientIP [exec "echo $ipIfConfig"]
puts "clientIP = $clientIP"
exit 1
输出为
pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ ./ifconfig_parse.sh
spawn bash
couldn't execute "echo ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*//p'": no such file or directory
while executing
"exec "echo $ipIfConfig""
invoked from within
"set clientIP [exec "echo $ipIfConfig"]"
(file "./ifconfig_parse.sh" line 7)
这样试试:
set ip [exec ifconfig enp0s3 | sed -n {/inet addr/s/.*addr.\([^ ]*\) .*//p}]
puts ip=$ip
参见Command substitution and exec in Tcl的手册。
这里是提取ip的简洁明了的awk脚本
对于ip4
:
ifconfig eno1 |awk '/inet /{print }'
对于ip6
ifconfig eno1 |awk '/inet6/{print }'
ip4
和 ip6
ifconfig eno1 |awk '/inet/{print $2}'
基本上脚本应该是:
set ip [ ifconfig eno1 |awk '/inet /{print }' ]
我正在尝试从 ifconfig 命令解析 IP 地址。当我在命令行上执行时它工作正常
pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*//p'
192.168.1.112
但是当我在 expect 脚本中使用它时出现错误。
#!/usr/bin/expect
set pidBash [ spawn bash ]
set ipIfConfig {ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*//p'}
set clientIP [exec "echo $ipIfConfig"]
puts "clientIP = $clientIP"
exit 1
输出为
pb791b@pb791b-VirtualBox:~/devtest/ngs/base/Tests/shellScripts$ ./ifconfig_parse.sh
spawn bash
couldn't execute "echo ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*//p'": no such file or directory
while executing
"exec "echo $ipIfConfig""
invoked from within
"set clientIP [exec "echo $ipIfConfig"]"
(file "./ifconfig_parse.sh" line 7)
这样试试:
set ip [exec ifconfig enp0s3 | sed -n {/inet addr/s/.*addr.\([^ ]*\) .*//p}]
puts ip=$ip
参见Command substitution and exec in Tcl的手册。
这里是提取ip的简洁明了的awk脚本
对于ip4
:
ifconfig eno1 |awk '/inet /{print }'
对于ip6
ifconfig eno1 |awk '/inet6/{print }'
ip4
和 ip6
ifconfig eno1 |awk '/inet/{print $2}'
基本上脚本应该是:
set ip [ ifconfig eno1 |awk '/inet /{print }' ]