码迷,mamicode.com
首页 > 编程语言 > 详细

Python的paramiko,实现ssh

时间:2017-04-15 19:17:44      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:慢慢   ini   脚本   log   init   cep   pytho   config   bsp   

最简单的使用paramiko登录远程机器执行一些命令,学习实验楼的paramiko记录下来,第一次使用ConfigParser这个库,对于封装这些还是不太熟悉,只能慢慢来,嘿嘿嘿

 

这是python脚本文件,还有一个变量文本

import paramiko
import ConfigParser

class ParamikoClient:
    def __init__(self,config_str):
        self.config = ConfigParser.ConfigParser()
        self.config.read(config_str)

        self.client = paramiko.SSHClient()
        self.client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    def connet(self):

        try:
            self.client.connect(hostname=self.config.get(‘ssh‘,‘host‘),port=self.config.getint(‘ssh‘,‘port‘),username=self.config.get(‘ssh‘,‘username‘),password=self.config.get(‘ssh‘,‘password‘))
        except Exception,e:
            print e
            try:
                self.client.close()
            except:
                pass
    def run_cmd(self,cmd_str):
            stdin, stdout, stderr = self.client.exec_command(cmd_str)
            print stdout.read()

client = ParamikoClient(‘config.ini‘)
client.connet()
client.run_cmd(‘date‘)

 

config.ini文件

[ssh]
host = 192.168.1.101
port = 22
username = root
password = 123456

 

Python的paramiko,实现ssh

标签:慢慢   ini   脚本   log   init   cep   pytho   config   bsp   

原文地址:http://www.cnblogs.com/lemon-le/p/6715094.html

踩
(0)
赞
(0)
   
举报
评论 一句话评论(0)
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!