CMDB鏈接方式

1.agent

agent (放在每臺客戶端服務器上,定時任務) 腳本演示python

# 採集本機的信息  執行命令
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 paramikodjango

鏈接服務器json

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

遠程執行命令【公鑰和私鑰】(公鑰必須提早上傳到服務器)api

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

或者將密鑰寫入文件bash

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

masterssh

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   刪除某一個

minionpost

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

命令行:url

服務端指定某個服務器執行命令

salt '*'  cmd.run 'ifconfig'

python接口:命令行

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.數據交互

發送數據

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')
相關文章
相關標籤/搜索