我有一个dns&我们公司的apachi服务器,它的IP地址是动态的 我试图做以下事情:
#!/bin/bash
# Get the dynamic IP (dirty, I know)
IP=`host -t a mychangingip.myip.com | perl -nle '/((?:\d+\.?){4})/ && print $1' | head -n1`
# Update the hosts file
if test -n "$IP"; then
grep -v www.thesite.com /etc/hosts > /tmp/hosts
echo "$IP www.thesite.com" >> /tmp/hosts
cp /tmp/hosts /etc/hosts
fi

到目前为止这一步工作正常,但是当我尝试:
#!/bin/bash
# Get the dynamic IP (dirty, I know)
IP=`host -t a mychangingip.myip.com | perl -nle '/((?:\d+\.?){4})/ && print $1' | head -n1`
# Update the hosts file
if test -n "$IP"; then
grep -v www.thesite.com /etc/hosts > /tmp/hosts
grep -v portal.thesite.com /etc/hosts > /tmp/hosts
echo "$IP www.thesite.com" >> /tmp/hosts/
echo "$IP portal.thesite.com" >> /tmp/hosts
cp /tmp/hosts /etc/hosts
fi

它没有按预期工作,只更新了一个条目
答案 0 :(得分:3)
替换:
grep -v www.thesite.com /etc/hosts > /tmp/hosts
grep -v portal.thesite.com /etc/hosts > /tmp/hosts
使用:
grep -v www.thesite.com /etc/hosts > /tmp/hosts2
grep -v portal.thesite.com /etc/hosts2 > /tmp/hosts
或,用以下内容替换两行:
grep -v 'www.thesite.com\|portal.thesite.com' /etc/hosts > /tmp/hosts
考虑更换这五行:
grep -v www.thesite.com /etc/hosts > /tmp/hosts
grep -v portal.thesite.com /etc/hosts > /tmp/hosts
echo "$IP www.thesite.com" >> /tmp/hosts/
echo "$IP portal.thesite.com" >> /tmp/hosts
cp /tmp/hosts /etc/hosts
这一行:
sed -i.bak "/www.thesite.com|portal.thesite.com/ s/[^[[:space:]]*/$IP/" /etc/hosts
答案 1 :(得分:0)
供您参考:
grep -v 'www.thesite.com' /etc/hosts|grep -v 'portal.thesite.com' >/tmp/hosts
echo -e $IP'\t\t'www.thesite.com >> /tmp/hosts
echo -e $IP'\t\t'portal.thesite.com >> /tmp/hosts
cat /tmp/hosts > /etc/hosts