1、导入包
import salt.client
2、建立连接和测试网络
client = salt.client.LocalClient() ret = client.cmd(‘*‘,‘test.ping‘)
3、archive模块
#打包文件 client.cmd(‘*‘,‘archive.gzip‘,[‘/tmp/test.txt‘]) print(client.cmd("*",‘cmd.run‘,[‘ls -ltr /tmp/test.txt*‘])) #解压文件 client.cmd(‘*‘,‘archive.gunzip‘,[‘/tmp/test.txt‘]) print(client.cmd("*",‘cmd.run‘,[‘ls -ltr /tmp/test.txt*‘]))
4、cmd模块
用来执行命令行命令
#执行命令行命令 ret = client.cmd(‘*‘,‘cmd.run‘,[‘free -m‘]) print(ret)
5、cp模块
#复制文服务器文件夹到本地 print(client.cmd(‘*‘,‘cp.get_dir‘,[‘salt://test_cp/te2‘,‘/tmp‘])) #复制服务器文件到本地 print(client.cmd(‘*‘,‘cp.get_file‘,[‘salt://text_cp/test_get.txt‘,‘/tmp/test_get.txt‘]))
6、cron模块
#定时间新建任务 print(client.cmd(‘*‘,‘cron.set_job‘,[‘root‘,‘*‘,‘*‘,‘*‘,‘*‘,‘1‘,‘echo 123456‘]))
7、dnsutil模块
编辑hosts文件,添加dns对应
#编辑添加hosts文件 ret = client.cmd(‘*‘,‘dnsutil.hosts_append‘,[‘/etc/hosts‘,‘127.0.0.1‘,‘ad1.zxw.com,ad2.zxw.com‘]) #编辑删除hosts文件 ret = client.cmd(‘*‘,‘dnsutil.hosts_remove‘,[‘/etc/hosts‘,‘ad1.zxw.com,ad2.zxw.com‘])
8、file模块
#查看文件状态 ret = client.cmd(‘*‘,‘file.stats‘,[‘/tmp/file_cp‘]) #根改文件属组 ret = client.cmd(‘*‘,‘file.chown‘,[‘/tmp/file_cp‘,‘zxw‘,‘root‘]) #获取文件权限 ret = client.cmd(‘*‘,‘file.get_mode‘,[‘/tmp/file_cp‘]) #根改文件权限 ret = client.cmd(‘*‘,‘file.set_mode‘,[‘/tmp/file_cp‘,‘0644‘])
9、iptables模块
#添加防火墙属性 ret = client.cmd(‘*‘,‘iptables.append‘,[‘filter‘,‘INPUT‘,"rule=‘-m state --state RELATED,ESTABLISHED -j ACCEPT‘"]) #在第3的位置插入防火墙规则 ret = client.cmd(‘*‘,‘iptables.insert‘,[‘filter‘,‘INPUT‘,‘position=3‘,"rule=‘-m state --state RELATED,ESTABLISHED -j ACCEPT‘"]) #删除防火墙规则 ret = client.cmd(‘*‘,‘iptables.delete‘,[‘filter‘,‘INPUT‘,"rule=‘-m state --state RELATED,ESTABLISHED -j ACCEPT‘"]) #删除指定index的规则 ret = client.cmd(‘*‘,‘iptables.delete‘,[‘filter‘,‘INPUT‘,‘position=3‘])
10、network模块
#客户机进行ping操作 ret = client.cmd(‘*‘,‘network.ping‘,[‘www.baidu.com‘]) print("network.ping res:") print(ret) #客户机进行dig,dns验证操作 ret = client.cmd(‘*‘,‘network.dig‘,[‘www.baidu.com‘]) print("network.dig res:") print(ret) #客户机路由探测 ret = client.cmd(‘*‘,‘network.traceroute‘,[‘www.baidu.com‘]) print("network.traceroute res:") print(ret) #客户机网卡信息获取 ret = client.cmd(‘*‘,‘network.interfaces‘) print("network.interfaces res:") print(ret) #客户机硬件信息获取 ret = client.cmd(‘*‘,‘network.hwaddr‘,[‘eth0‘]) print("network.hwaddr res:") print(ret) #判断客户机是否属于网段 ret = client.cmd(‘*‘,‘network.in_subnet‘,[‘192.168.100.0/24‘]) print("network.in_subnet res:") print(ret) #获取客户机网段 ret = client.cmd(‘*‘,‘network.subnets‘) print("network.subnets res:") print(ret)
11、pkg模块
相当于在客户机执行yum和apt-get命令
#安装php服务 ret = client.cmd(‘*‘,‘pkg.install‘,[‘php‘]) print("pkg.install res:") print(ret) #更新php服务 ret = client.cmd(‘*‘,‘pkg.upgrade‘,[‘php‘]) print("pkg.upgrade res:") print(ret) #卸载phpfuwu ret = client.cmd(‘*‘,‘pkg.remove‘,[‘php‘]) print("pkg.remove res:") print(ret)
12、service模块
#开机启动nginx ret = client.cmd(‘*‘,‘service.enable‘,[‘nginx‘]) print("service.enable res:") print(ret) #关闭开机启动nxinx ret = client.cmd(‘*‘,‘service.disable‘,[‘nginx‘]) print("service.disable res:") print(ret) #停止nginx ret = client.cmd(‘*‘,‘service.stop‘,[‘nginx‘]) print("service.stop res:") print(ret) #验证nginx状态 ret = client.cmd(‘*‘,‘service.status‘,[‘nginx‘]) print("service.status res:") print(ret) #启动nginx ret = client.cmd(‘*‘,‘service.start‘,[‘nginx‘]) print("service.start res:") print(ret) #再次验证nginx状态 ret = client.cmd(‘*‘,‘service.status‘,[‘nginx‘]) print("service.status res:") print(ret)