ラズベリーパイでは、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ステートメントを調整する必要がありますか?
何が問題ですか?現在のスクリプトは、あなたが入力したどんな新しいDNSでも '192.168.126.1 66.243.243.101'を置き換えます。 – janos