亲宝软件园·资讯

展开

CMDB连接方式

追梦NAN 人气:2

1.agent

agent (放在每台客户端服务器上,定时任务) 脚本演示

# 采集本机的信息  执行命令
import subprocess

v1 = subprocess.getoutput('ipconfig')
v1 = v1[20:30]
#print(v1)

v2 = subprocess.getoutput('dir')
v2 = v2[2:12]
#print(v2)

2.paramiko(SSH)

pip install paramiko

连接服务器

import paramiko

private_key = paramiko.RSAKey.from_private_key_file(r'C:/Users/Administrator/.ssh/id_rsa')

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器 #password密码  #pkey密钥
ssh.connect(hostname='192.168.16.85', port=22, username='root', pkey=private_key) 

# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read()
print(result.decode('utf-8'))
# 关闭连接
ssh.close()

远程执行命令【公钥和私钥】(公钥必须提前上传到服务器)

import paramiko
#密钥
private_key = paramiko.RSAKey.from_private_key_file(r'C:/Users/Administrator/.ssh/id_rsa')

transport = paramiko.Transport(('192.168.16.85', 22))
transport.connect(username='root', password='123456') #pkey=private_key
sftp = paramiko.SFTPClient.from_transport(transport)


# 将location.py 上传至服务器 /tmp/test.py
# sftp.put('wy.txt', 'https://img.qb5200.com/download-x/data/wy.txt')
sftp.get('https://img.qb5200.com/download-x/data/wy.txt', 'xx.txt')

transport.close()

或者将密钥写入文件

key = """-----BEGIN RSA PRIVATE KEY-----"""

import paramiko
from io import StringIO

private_key = paramiko.RSAKey(file_obj=StringIO(key))

# 创建SSH对象
ssh = paramiko.SSHClient()
# 允许连接不在know_hosts文件中的主机
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接服务器
ssh.connect(hostname='192.168.16.85', port=22, username='root', pkey=private_key)

# 执行命令
stdin, stdout, stderr = ssh.exec_command('df')
# 获取命令结果
result = stdout.read()
print(result.decode('utf-8'))
# 关闭连接
ssh.close()

分发密钥命令

ssh-copy-id -i ~.ssh/id_rsa.pub root@192.168.16.85 

3.salt

master

sudo yum install https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el7.noarch.rpm
yum install salt-master -y 

配置:

/etc/salt/master  

interface: 0.0.0.0

启动: service salt-master start 

授权:

    salt-key -L  查看

    salt-key -a   id  接受某一个 

    salt-key -A   接受所有

    salt-key -d  id   删除某一个

minion

sudo yum install https://repo.saltstack.com/py3/redhat/salt-py3-repo-latest.el7.noarch.rpm
yum install salt-minion -y 

配置:/etc/salt/minion

master: 192.168.16.37

启动: service salt-minion start 

命令行:

服务端指定某个服务器执行命令

salt '*'  cmd.run 'ifconfig'

python接口:

pip3 install salt -i http://pypi.tuna.tsinghua.edu.cn/simple

#pycharm 中要安装salt
import salt.client
local = salt.client.LocalClient()
result = local.cmd('c2.salt.com', 'cmd.run', ['ifconfig'])

4.数据交互

发送数据

import   requests

requests.post(
    url='http://127.0.0.1:8000/api/v1/server/',
    json={"server":"47.93.255.102","data":data}
)

api (django程序)

"""
from django.shortcuts import render,HttpResponse

def index(request):
    if request.method == "POST":
        print(request.POST)
        return HttpResponse("接受到了")

    else:
        return HttpResponse("index")
"""

    
from rest_framework.views import APIView
from rest_framework.response import Response
class ServerViews(APIView):
    def post(self,request,*args,**kwargs):
        print(request.data)
        return  Response('1')

加载全部内容

相关教程
猜你喜欢
用户评论