CMDB connections

1.agent

agent (on each client server, scheduled tasks) script demonstrates

# 采集本机的信息  执行命令
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

connect to the server

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()

Remote execution of commands [public and private] (public key must be uploaded to the server in advance)

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', '/data/wy.txt')
sftp.get('/data/wy.txt', 'xx.txt')

transport.close()

Or write the key file

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()

Distribution of key command

ssh-copy-id -i ~.ssh/id_rsa.pub [email protected] 

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 

Command Line:

Server specify a server command execution

salt '*'  cmd.run 'ifconfig'

python interfaces:

pip3 install salt -i https://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. The data exchange

send data

import   requests

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

api (django program)

"""
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')

Guess you like

Origin www.cnblogs.com/zdqc/p/11886984.html