首页 > 其他 > 详细

Paramiko初体验

时间:2020-03-13 11:53:28      阅读:47      评论:0      收藏:0      [点我收藏+]
登录信息配置

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

Paramiko初体验

原文:https://www.cnblogs.com/lbfang/p/12484044.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!