Paramiko初体验

登录信息配置

config.ini

[ssh]
ip="1.1.1.1"
username="lbfang"
pwd="123"
port="22"

客户端实现
ssh_client.py

# coding:utf-8
import paramiko
import ConfigParser


class ParamikoClient:
def __init__(self, config_str):
self.config = ConfigParser.ConfigParser()
self.config.read('config.ini')
self.client = paramiko.SSHClient()
self.client.set_missing_host_key_policy()

def connect(self):
try:
self.client.connect(hostname == self.config.get('ssh', 'host'), port=self.config.get('ssh', 'port'),
username=self.config.get('ssh', 'username'), pwd=self.config.get('ssh', 'pwd'))
except Exception as e:
print(e)
try:
self.client.close()
except:
pass

def run_cmd(self, cmd_str):
stdin, stdout, stderr = self.client.exec_command(cmd_str)
for line in stdout:
print(line)

调用客户端

test.py

import time
from ssh_client import ParamikoClient

begin = time.time()

client = ParamikoClient('config.ini')
client.connect()
client.run_cmd()

query_num = 100
query_fre = 10
while query_num > 0:
now = time.time()
now - begin > query_fre
print(time.time())
begin = now
query_num = query_num - 1
posted @ 2020-03-13 01:04  房龙波  阅读(174)  评论(0)    收藏  举报