2017-12-05 11 views
0

ラズベリーパイでは、dhcpcd.confファイルの静的IP設定を変更できる簡単なスクリプトを作成しようとしています。以下のスクリプトはdns-serversを除いて動作します。その行には、スペースで区切られた2つのIPアドレスが含まれているため、sedステートメントは動作しません。次のようにスクリプトは次のとおりです。sedを使用してdhcpcd.confファイルのIPアドレスを変更する

#!/bin/bash 
currip=$(cat /etc/dhcpcd.conf | grep -e '^static ip_address=' | cut -d= - 
f2) 
currgw=$(cat /etc/dhcpcd.conf | grep -e '^static routers=' | cut -d= -f2) 
currdns=$(cat /etc/dhcpcd.conf | grep -e '^static domain_name_servers=' | 
cut -d= -f2) 
echo "current IP is $currip" 
echo "current GW is $currgw" 
echo "current DNS servers are $currdns" 
echo "Enter new static ip in form of x.x.x.x/x: " 
read newip 
echo "Enter new GW in form of x.x.x.x: " 
read newgw 
echo "Enter new DNS servers in form of x.x.x.x x.x.x.x: " 
read newdns 
echo "currip is $currip" 
echo "new ip will be $newip" 
echo "new dns will be $newdns" 
sed -i -e "[email protected]$currip\[email protected][email protected]" /etc/dhcpcd.conf 
sed -i -e "[email protected]$currgw\[email protected][email protected]" /etc/dhcpcd.conf 
sed -i -e "[email protected]$currdns\[email protected][email protected]" /etc/dhcpcd.conf 
chip=$(cat /etc/dhcpcd.conf | grep -e '^static ip_address=' | cut -d= - 
f2) 
chgw=$(cat /etc/dhcpcd.conf | grep -e '^static routers=' | cut -d= -f2) 
chdns=$(cat /etc/dhcpcd.conf | grep -e '^static domain_name_servers=' | 
cut -d= -f2) 
echo "The ip has been changed to $chip" 
echo "The GW has been changed to $chgw" 
echo "The DNS server have been changed to $chdns" 

このようなdhcpcd.confファイルの外観の行:

静的IP_ADDRESS = 192.168.126.7/24

静的ルーター= 192.168.126.1

static domain_name_servers = 192.168.126.1 66.243.243.101

domain_name_serversにどのようにsedステートメントを調整する必要がありますか?

+2

何が問題ですか?現在のスクリプトは、あなたが入力したどんな新しいDNSでも '192.168.126.1 66.243.243.101'を置き換えます。 – janos

答えて

0

スタティックルータ "192.168.126.1"も静的domain_name_serversに存在します。あなたは

sed -i -e "[email protected]$currgw\[email protected][email protected]" /etc/dhcpcd.conf 

でルータを上書きする場合したがって、あなたのconfファイル内の行は、それはもはや、それはsedのネームサーバにマッチした

static domain_name_servers={{what you entered}} 66.243.243.101 

に変更されます。

私が見つけるの変更を提案して、次のようにキーと同様に値を含むように文字列を置き換えます。これは、それ置き換え以前を含めることが起こるので、ノー、他のラインになります

sed -i -e "[email protected]^static ip_address=$currip\[email protected] [email protected]" dhcpcd.conf 
sed -i -e "[email protected]^static routers=$currgw\[email protected] [email protected]" dhcpcd.conf 
sed -i -e "[email protected]^static domain_name_servers=$currdns\[email protected] [email protected]" dhcpcd.conf**strong text** 

を-stringが変更されます

+0

ダー、それを見ていない。私はあなたが提案したように一度それをしようとしましたが、それを動作させることができませんでした。構文エラーがあった必要があります。私は、sedステートメントの区切り文字の代わりに/の代わりに@を使用する前にそれがあったと思います。助けてくれてありがとう。今は素晴らしい作品です。 –

関連する問題