ifconfigコマンドでIPアドレスを解析しようとしています。コマンドラインでうまく動作しますexpectスクリプト:ifconfigからIPアドレスを解析する
[email protected]:~/devtest/ngs/base/Tests/shellScripts$ ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p'
192.168.1.112
ただし、expectスクリプトで使用するとエラーになります。
#!/usr/bin/expect
set pidBash [ spawn bash ]
set ipIfConfig {ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/p'}
set clientIP [exec "echo $ipIfConfig"]
puts "clientIP = $clientIP"
exit 1
出力は
[email protected]:~/devtest/ngs/base/Tests/shellScripts$ ./ifconfig_parse.sh
spawn bash
couldn't execute "echo ifconfig enp0s3 | sed -n '/inet addr/s/.*addr.\([^ ]*\) .*/\1/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)
[この質問](https://stackoverflow.com/questions/5281341/get-local-network-interface-addresses-using-only-proc)は役に立ちますか? – tadman
"bash ifconfig_parse.sh"を試してください –