YAZONG 我的开源

简单视图化管理/etc/hosts的命令工具hostctl

  , ,
0 评论0 浏览

一、简介

在日常的工作中,我们经常会通过编辑hosts文件来处理一些自定义域名的解析任务,每次手动的编辑linux或者mac上/etc/hosts和window上的hosts是一项繁琐的任务。

但是现在,我们可以通过hostctl来自动化管理本地域名解析文件,这样一来就可以使某些方面自动化,以使其变得更加干净快捷。

二、安装方法

wget https://github.com/guumaster/hostctl/releases/download/v1.0.14/hostctl_1.0.14_linux_64-bit.tar.gz

tar -zxf hostctl_1.0.14_linux_64-bit.tar.gz

ln -s /data/software/workspace/hostctl/hostctl /usr/bin/hostctl

三、文档

https://guumaster.github.io/hostctl/docs/getting-started/

四、用法

1)add


#在/etc/profile中新增

 

[root@ms17 workspace]# hostctl add domains test test1.com --ip 1.2.3.4      

[?] Domains 'test1.com' added.

 

+---------+--------+---------+-----------+

| PROFILE | STATUS |   IP    |  DOMAIN   |

+---------+--------+---------+-----------+

| test    | on     | 1.2.3.4 | test1.com |

+---------+--------+---------+-----------+

[root@ms17 workspace]# hostctl add domains test test2.com --ip 1.2.3.4 

[?] Domains 'test2.com' added.

 

+---------+--------+---------+-----------+

| PROFILE | STATUS |   IP    |  DOMAIN   |

+---------+--------+---------+-----------+

| test    | on     | 1.2.3.4 | test1.com |

| test    | on     | 1.2.3.4 | test2.com |

+---------+--------+---------+-----------+

 

 

[root@ms17 ~]# cat /etc/hosts

##################################################################

# Content under this line is handled by hostctl. DO NOT EDIT.

##################################################################

 

# profile.on test

1.2.3.4 test1.com

1.2.3.4 test2.com

# end

 

[root@ms17 ~]# hostctl list  

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| default | on     | 127.0.0.1   | localhost            |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

| test    | on     | 1.2.3.4     | test2.com            |

+---------+--------+-------------+----------------------+

 

--一个IP对应多个域名

 

[root@ms17 test]# hostctl add domains fat fat1.com fat2.com --ip 2.3.4.5

[?] Domains 'fat1.com, fat2.com' added.

 

+---------+--------+---------+----------+

| PROFILE | STATUS |   IP    |  DOMAIN  |

+---------+--------+---------+----------+

| fat     | on     | 2.3.4.5 | fat1.com |

| fat     | on     | 2.3.4.5 | fat2.com |

+---------+--------+---------+----------+

 

[root@ms17 ~]# hostctl  list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

 

[root@ms17 ~]# cat /etc/hosts

# profile.on fat

2.3.4.5 fat1.com

2.3.4.5 fat2.com

# end

2)disable


#关闭某profile的所有内容

 

--怎么disable其中一个?这里是否管理的是整个profile,而不能管理profile中的其中一个待定?

 

[root@ms17 workspace]# hostctl disable test 

+---------+--------+---------+-----------+

| PROFILE | STATUS |   IP    |  DOMAIN   |

+---------+--------+---------+-----------+

| test    | off    | 1.2.3.4 | test1.com |

| test    | off    | 1.2.3.4 | test2.com |

+---------+--------+---------+-----------+

 

[root@ms17 ~]# hostctl list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | off    | 1.2.3.4     | test1.com            |

| test    | off    | 1.2.3.4     | test2.com            |

+---------+--------+-------------+----------------------+

[root@ms17 ~]# cat /etc/hosts

##################################################################

# Content under this line is handled by hostctl. DO NOT EDIT.

##################################################################

 

# profile.off test

# 1.2.3.4 test1.com

# 1.2.3.4 test2.com

# end

 

--disable所有,但不包括之前没使用hostctl的内容.之前的profile都为default。

 

[root@ms17 test]# hostctl disable --all

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | off    | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

| fat     | off    | 2.3.4.5     | fat1.com             |

| fat     | off    | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

3)enable


#开启某profile的所有内容

 

--怎么enable其中一个?

 

[root@ms17 workspace]# hostctl enable test  

+---------+--------+---------+-----------+

| PROFILE | STATUS |   IP    |  DOMAIN   |

+---------+--------+---------+-----------+

| test    | on     | 1.2.3.4 | test1.com |

| test    | on     | 1.2.3.4 | test2.com |

+---------+--------+---------+-----------+

 

[root@ms17 ~]# cat /etc/hosts

 

##################################################################

# Content under this line is handled by hostctl. DO NOT EDIT.

##################################################################

 

# profile.on test

1.2.3.4 test1.com

1.2.3.4 test2.com

# end

 

[root@ms17 ~]# hostctl list  

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

| test    | on     | 1.2.3.4     | test2.com            |

+---------+--------+-------------+----------------------+

 

--enable所有,但不包括之前没使用hostctl的内容.之前的profile都为default。

 

[root@ms17 test]# hostctl enable --all   

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

4)remove


#移除某个profile的所有内容

 

[root@ms17 test]# hostctl remove test

 

[root@ms17 workspace]# hostctl remove domains test test2.com

[?] Domains 'test2.com' removed.

 

+---------+--------+---------+-----------+

| PROFILE | STATUS |   IP    |  DOMAIN   |

+---------+--------+---------+-----------+

| test    | on     | 1.2.3.4 | test1.com |

+---------+--------+---------+-----------+

 

[root@ms17 ~]# hostctl list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

 

[root@ms17 ~]# cat /etc/hosts

 

##################################################################

# Content under this line is handled by hostctl. DO NOT EDIT.

##################################################################

 

# profile.on test

1.2.3.4 test1.com

# end

5)backup


#备份/etc/hosts到某个路径

 

[root@ms17 test]# hostctl backup --path .

[root@ms17 test]# ll

total 4

-rw-r--r-- 1 root root 1074 Dec 21 13:55 hosts.20201221

6)list


#list列表的不同用法

 

[root@ms17 test]# hostctl list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

 

[root@ms17 test]# hostctl list test

+---------+--------+---------+-----------+

| PROFILE | STATUS |   IP    |  DOMAIN   |

+---------+--------+---------+-----------+

| test    | on     | 1.2.3.4 | test1.com |

+---------+--------+---------+-----------+

 

--输出格式

 

[root@ms17 test]# hostctl list --out table

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

 

[root@ms17 test]# hostctl list --out raw

PROFILE STATUS  IP              DOMAIN 

test    on      1.2.3.4         test1.com         

fat     on      2.3.4.5         fat1.com          

fat     on      2.3.4.5         fat2.com 

 

[root@ms17 test]# hostctl list --out markdown

| PROFILE | STATUS |     IP      |        DOMAIN        |

|---------|--------|-------------|----------------------|

| test    | on     | 1.2.3.4     | test1.com            |

|---------|--------|-------------|----------------------|

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

 

[root@ms17 test]# hostctl list --out json

[

{"Profile":"test","Status":"on","IP":"1.2.3.4","Host":"test1.com"},

{"Profile":"fat","Status":"on","IP":"2.3.4.5","Host":"fat1.com"},

{"Profile":"fat","Status":"on","IP":"2.3.4.5","Host":"fat2.com"}

]

 

[root@ms17 test]# hostctl list --column ip

+-------------+

|     IP      |

+-------------+

| 1.2.3.4     |

+-------------+

| 2.3.4.5     |

| 2.3.4.5     |

+-------------+

 

[root@ms17 test]# hostctl list --host-file /etc/hosts --raw

PROFILE STATUS  IP              DOMAIN  

test    on      1.2.3.4         test1.com         

fat     on      2.3.4.5         fat1.com          

fat     on      2.3.4.5         fat2.com 

 

[root@ms17 test]# hostctl list --host-file /etc/hosts --out markdown

| PROFILE | STATUS |     IP      |        DOMAIN        |

|---------|--------|-------------|----------------------|

| test    | on     | 1.2.3.4     | test1.com            |

|---------|--------|-------------|----------------------|

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

 

[root@ms17 test]# hostctl list --host-file /etc/hosts --no-color

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

 

--不仅显示disabled的,还有default的。

 

[root@ms17 test]# hostctl list disabled

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| default | on     | 127.0.0.1   | localhost            |

+---------+--------+-------------+----------------------+

| test    | off    | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

 

--不仅显示enable的,还有default的。

 

[root@ms17 test]# hostctl list enabled

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| default | on     | 127.0.0.1   | localhost            |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

7)replace


#替换/etc/profile中的一部分为导入文件的内容

 

[root@ms17 ~]# hostctl list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

| test    | on     | 1.2.3.4     | test2.com            |

| test    | on     | 2.3.4.5     | test1.com            |

| test    | on     | 2.3.4.5     | test2.com            |

+---------+--------+-------------+----------------------+

 

[root@ms17 ~]# hostctl replace test --from hosts.20201221

 

[root@ms17 ~]# hostctl list  

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

| test    | on     | 2.3.4.5     | fat1.com             |

| test    | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

8)restore


#全局替换/etc/profile

 

[root@ms17 ~]# hostctl list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

 

[root@ms17 test]# hostctl add domains test test11.com test22.com --ip 9.8.7.6

[?] Domains 'test11.com, test22.com' added.

 

+---------+--------+---------+------------+

| PROFILE | STATUS |   IP    |   DOMAIN   |

+---------+--------+---------+------------+

| test    | on     | 9.8.7.6 | test11.com |

| test    | on     | 9.8.7.6 | test22.com |

+---------+--------+---------+------------+

 

[root@ms17 test]# hostctl restore --from hosts.20201221 

[?] File 'hosts.20201221' restored.

 

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

+---------+--------+-------------+----------------------+

| fat     | on     | 2.3.4.5     | fat1.com             |

| fat     | on     | 2.3.4.5     | fat2.com             |

+---------+--------+-------------+----------------------+

9)status


#查看某profile的status

 

[root@ms17 test]# hostctl add domains test1 test111.com --ip 8.7.6.5         

[?] Domains 'test111.com' added.

 

+---------+--------+---------+-------------+

| PROFILE | STATUS |   IP    |   DOMAIN    |

+---------+--------+---------+-------------+

| test1   | on     | 8.7.6.5 | test111.com |

+---------+--------+---------+-------------+

 

[root@ms17 test]# hostctl disable test1

+---------+--------+---------+-------------+

| PROFILE | STATUS |   IP    |   DOMAIN    |

+---------+--------+---------+-------------+

| test1   | off    | 8.7.6.5 | test111.com |

+---------+--------+---------+-------------+

 

[root@ms17 test]# hostctl status test1

+---------+--------+

| PROFILE | STATUS |

+---------+--------+

| test1   | off    |

+---------+--------+

10)toggle


#转换某profile的status

 

[root@ms17 ~]# hostctl list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | off     | 1.2.3.4     | test1.com            |

| test    | off     | 9.8.7.6     | test11.com           |

| test    | off     | 9.8.7.6     | test22.com           |

+---------+--------+-------------+----------------------+

 

[root@ms17 test]# hostctl toggle test

+---------+--------+---------+------------+

| PROFILE | STATUS |   IP    |   DOMAIN   |

+---------+--------+---------+------------+

| test    | on     | 1.2.3.4 | test1.com  |

| test    | on     | 9.8.7.6 | test11.com |

| test    | on     | 9.8.7.6 | test22.com |

+---------+--------+---------+------------+

 

[root@ms17 ~]# hostctl list

+---------+--------+-------------+----------------------+

| PROFILE | STATUS |     IP      |        DOMAIN        |

+---------+--------+-------------+----------------------+

| test    | on     | 1.2.3.4     | test1.com            |

| test    | on     | 9.8.7.6     | test11.com           |

| test    | on     | 9.8.7.6     | test22.com           |

+---------+--------+-------------+----------------------+

五、其他

5、1profile=default

在未使用hostctl之前配置的内容,通过hostctl list,展示的profile值都为default。

profile值都为default的内容不能通过hostctl命令来编辑。

5、2公共参数

#所有命令通用


Options inherited from parent commands

  -c, --column strings     Column names to show on lists. comma separated

      --host-file string   Hosts file path (default "/etc/hosts")

      --no-color           force colorless output

  -o, --out string         Output type (table|raw|markdown|json) (default "table")

  -q, --quiet              Run command without output

      --raw                Output without borders (same as -o raw)


标题:简单视图化管理/etc/hosts的命令工具hostctl
作者:yazong
地址:https://blog.llyweb.com/articles/2020/12/21/1608537113956.html