使用fab可以大大简化你的远程维护工作,下面看一个例子,用来显示远程主机的hostname和磁盘信息。
[root@gtlionsdev ~]# vi fabfile.py
[root@gtlionsdev ~]# cat fabfile.py
from fabric.api import run
def hostname():
run('hostname')
def disk():
run('df -h')
[root@gtlionsdev ~]# fab -l
Available commands:
disk
hostname
[root@gtlionsdev ~]# fab -H192.168.2.233 disk
[192.168.2.233] Executing task 'disk'
[192.168.2.233] run: df -h
[192.168.2.233] Login password for 'root':
[192.168.2.233] out: τ?t?? ? ?? ??`??% 1??
[192.168.2.233] out: /dev/mapper/VolGroup00-LogVol00
[192.168.2.233] out: 130G 51G 73G 42% /
[192.168.2.233] out: /dev/sda1 99M 12M 82M 13% /boot
[192.168.2.233] out: tmpfs 471M 0 471M 0% /dev/shm
Done.
Disconnecting from 192.168.2.233... done.
[root@gtlionsdev ~]# fab -H 192.168.2.233 hostname
[192.168.2.233] Executing task 'hostname'
[192.168.2.233] run: hostname
[192.168.2.233] Login password for 'root':
[192.168.2.233] out: db233
Done.
Disconnecting from 192.168.2.233... done.
是不是很简单呢?嘿嘿,当然还有其他很多的功能需要去挖掘,比如省略掉输入主机地址和密码的过程等待步骤都可以做到的。
-The End-
[root@gtlionsdev ~]# vi fabfile.py
[root@gtlionsdev ~]# cat fabfile.py
from fabric.api import run
def hostname():
run('hostname')
def disk():
run('df -h')
[root@gtlionsdev ~]# fab -l
Available commands:
disk
hostname
[root@gtlionsdev ~]# fab -H192.168.2.233 disk
[192.168.2.233] Executing task 'disk'
[192.168.2.233] run: df -h
[192.168.2.233] Login password for 'root':
[192.168.2.233] out: τ?t?? ? ?? ??`??% 1??
[192.168.2.233] out: /dev/mapper/VolGroup00-LogVol00
[192.168.2.233] out: 130G 51G 73G 42% /
[192.168.2.233] out: /dev/sda1 99M 12M 82M 13% /boot
[192.168.2.233] out: tmpfs 471M 0 471M 0% /dev/shm
Done.
Disconnecting from 192.168.2.233... done.
[root@gtlionsdev ~]# fab -H 192.168.2.233 hostname
[192.168.2.233] Executing task 'hostname'
[192.168.2.233] run: hostname
[192.168.2.233] Login password for 'root':
[192.168.2.233] out: db233
Done.
Disconnecting from 192.168.2.233... done.
是不是很简单呢?嘿嘿,当然还有其他很多的功能需要去挖掘,比如省略掉输入主机地址和密码的过程等待步骤都可以做到的。
-The End-