Python实现ssh登录执行shell命令并将结果写入mysql数据库

#coding=utf-8
import MySQLdb
import paramiko
import datetime
import time

#timestamp

timestamp = time.mktime(datetime.datetime.now().timetuple())


#创建SSH对象
ssh = paramiko.SSHClient()

#把要连接的机器添加到known_hosts文件中
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

#连接服务器
ssh.connect(hostname='127.0.0.1', port=22, username='root', password='123456')

memCmd = 'free -m | awk -F: '/Mem/' | awk '{print $3}''
#cmd = 'ls -l;ifconfig'       #多个命令用;隔开
stdin, stdout, stderr = ssh.exec_command(memCmd)

memResult = stdout.read()

if not memResult:
    memResult = stderr.read()
ssh.close()

#print(memResult.decode())

conn= MySQLdb.connect(
    

你可能感兴趣的:(Python,python,mysql,shell,ssh)