Paramiko first experience

Login configuration 

the config.ini

[SSH]
IP = "1.1.1.1"
username = "lbfang"
pwd = "123"
Port = "22 is"

client implementation
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

Guess you like

Origin www.cnblogs.com/lbfang/p/12484044.html