일상 기록 창고

파이썬으로 ssh 접속하여 cpu,메모리,os,아이피 정보 가져오기 본문

프로그래밍/PHP

파이썬으로 ssh 접속하여 cpu,메모리,os,아이피 정보 가져오기

Crazy_Kong 2018. 4. 12. 16:12

서버의 CPU, 메모리, OS, 아이피 정보 등을 확인할 필요가 있어서 수동으로 처리하기 귀찮아 구글링으로 만든거..


import paramiko


olist = {}

ips = ['127.0.0.1'];


for index, ip in enumerate(ips):


    c = paramiko.SSHClient()

    c.set_missing_host_key_policy(paramiko.AutoAddPolicy())

    c.connect( hostname = ip, username = '', password = '', port = '' )


    command01 = "grep \"model name\" /proc/cpuinfo | sort -u | awk '{print $4, $5, $6, $7, $8, $9}'"

    command02 = "cat /proc/meminfo | grep MemTotal: | awk '{print $2}'"

    command03 = "cat /etc/redhat-release"


    # 사설아이피 세팅했다면 head -n 숫자 적당히

    command04 = "/sbin/ifconfig | grep \"inet addr:\" | awk '{print $2}' | head -n 1"  


    commands = [command01,command02, command03, command04]


    info = {}


    for index,command in enumerate(commands):

        stdin , stdout, stderr = c.exec_command(command)


        if index == 0:

            info['cpu'] = stdout.read()

        elif index == 1:

            info['memory'] = stdout.read()

        elif index == 2:

            info['os'] = stdout.read()

        else:

            info['ip'] = stdout.read()

    c.close()


    olist[ip] = info


print(olist)


내용만 그냥 출력 해 봄.