簡體   English   中英

使用 shell 腳本將行附加到 /etc/hosts 文件

[英]Append line to /etc/hosts file with shell script

我有一個新的 Ubuntu 12.04 VPS。 我正在嘗試編寫一個安裝腳本來完成整個 LAMP 安裝。 我遇到問題的地方是在/etc/hosts文件中附加一行。 我當前的主機文件如下所示:

127.0.0.1       localhost Venus

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我希望它看起來像這樣:

127.0.0.1       localhost Venus
192.241.xx.xx  venus.example.com venus

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

我已經使用 append ( \\a ) 命令嘗試了各種sed命令。 出於某種原因,Ubuntu 要么只是在終端中回顯hosts文件的內容,要么什么都不做。 如何使用 bash 腳本將第二行正確注入文件中?

確保使用sed-i選項。

-i[SUFFIX], --in-place[=SUFFIX]
  edit files in place (makes backup if extension supplied)

sed -i "2i192.241.xx.xx  venus.example.com venus" /etc/hosts

否則,

echo "192.241.xx.xx  venus.example.com venus" >> /etc/hosts

將在文件末尾附加該行,這可以按您的預期工作。

如果您在 mac 中或者您需要 sudo 權限,請嘗試以下操作:

sudo -- sh -c -e "echo '192.34.0.03   subdomain.domain.com' >> /etc/hosts";

它仍然會要求您輸入密碼。

來自@kainjow的替代方式

echo '192.34.0.03 subdomain.domain.com' | sudo tee -a /etc/hosts

插入/更新條目

如果你想使用 bash 以編程方式插入/更新主機條目,這是我寫的一個腳本:

#!/bin/bash

# insert/update hosts entry
ip_address="192.168.x.x"
host_name="my.hostname.example.com"
# find existing instances in the host file and save the line numbers
matches_in_hosts="$(grep -n $host_name /etc/hosts | cut -f1 -d:)"
host_entry="${ip_address} ${host_name}"

echo "Please enter your password if requested."

if [ ! -z "$matches_in_hosts" ]
then
    echo "Updating existing hosts entry."
    # iterate over the line numbers on which matches were found
    while read -r line_number; do
        # replace the text of each line with the desired host entry
        sudo sed -i '' "${line_number}s/.*/${host_entry} /" /etc/hosts
    done <<< "$matches_in_hosts"
else
    echo "Adding new hosts entry."
    echo "$host_entry" | sudo tee -a /etc/hosts > /dev/null
fi

該腳本旨在與 OS X 一起使用,但也可以在 linux 上使用,並稍作調整。

echo "127.0.0.1 localhost `hostname`">./temp_hosts
echo "192.241.xx.xx  venus.example.com">>./temp_hosts
cat /etc/hosts |tail -n +2 >>./temp_hosts
cat ./temp_hosts > /etc/hosts
rm ./temp_file

我應該指出sed編輯器)實際上並不用於編輯文件,盡管它可以用來做到這一點。 標准 sed沒有用於寫入標准輸出以外的內置機制。)更合適的工具是ed

下面的 ed 腳本說“找到包含(公認的草率)正則表達式 /127.0.0.1/ 的行並附加到下一行。” (單獨的句點告訴 ed 停止追加。)

ed /etc/hosts <<-'EOF'
    /127.0.0.1/a
    192.241.xx.xx  venus.example.com
    .
    wq
EOF

也就是說,您真的可以非常簡單地將此行附加到 /etc/hosts 文件的末尾

echo '192.241.xx.xx  venus.example.com' >> /etc/hosts

您可以使用sed ,例如:

sed '/Venus/ a\  
192.241.xx.xx  venus.example.com venus' /etc/hosts

用 root 訪問試試這個。

 public void edithost() {
    sudo("echo " + "192.168.43.1     www.openrap.com openrap" + " >> /etc/hosts");
    sudo("echo " + "192.168.43.1  openrap.com openrap" + " >> /etc/hosts");
    sudo("echo " + "192.168.2.144  www.openrap.com openrap" + " >> /etc/hosts");
    sudo("echo " + "192.168.2.144  openrap.com openrap" + " >> /etc/hosts");
}

sudo 獲得超級用戶權限

public static void sudo(String... strings) {
    try {
        Process su = Runtime.getRuntime().exec("su");
        DataOutputStream outputStream = new DataOutputStream(su.getOutputStream());

        for (String s : strings) {
            outputStream.writeBytes(s + "\n");
            outputStream.flush();
        }

        outputStream.writeBytes("exit\n");
        outputStream.flush();
        try {
            su.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

這會將行附加到 android 中的主機

我從 bash 腳本中嘗試了上述所有內容。 沒有一個工作。 所以我用這種方式:

chmod 777 /etc/hosts
echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
chmod 644 /etc/hosts

不理想,但它至少有效......

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM