2017-09-08 7 views
0

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) 
+0

[この質問](https://stackoverflow.com/questions/5281341/get-local-network-interface-addresses-using-only-proc)は役に立ちますか? – tadman

+0

"bash ifconfig_parse.sh"を試してください –

答えて

1

次のように試してみてくださいされています

set ip [exec ifconfig enp0s3 | sed -n {/inet addr/s/.*addr.\([^ ]*\) .*/\1/p}] 
puts ip=$ip 

TclのマニュアルにCommand substitutionexecを参照してください。

+0

なぜか、一重引用符と括弧を説明する必要があります。 –

+0

@whjmありがとうございます。引用符と括弧を使用するときは注意が必要です。これを使って 'sed'を使わずにIPアドレスを抽出することもできました。 if [regexp {inet addr:(\ S +)} [exec ifconfig enp0s3] - > clientIP]} {puts "clientIP = $ clientIP"} –

関連する問題