paramiko模块已经安装了还是报No module named 'paramiko'

来源:4-15 Paramiko模块实战初步

慕粉8429964

2022-09-11

图片描述

以下是运行源码
"""
paramiko模块实战1:从案例中学习
python 远程控制工具包
"""
import sys
import socket
import traceback
import termios
import tty

import paramiko
from paramiko.py3compat import u


hostname = '192.168.xx.xx'
port = 22
username = 'root'
password = 'dongxin2069'


def posix_shell(chan):
    import select

    oldtty = termios.tcgetattr(sys.stdin)
    try:
        tty.setraw(sys.stdin.fileno())
        tty.setcbreak(sys.stdin.fileno())
        chan.settimeout(0.0)

        while True:
            r, _, _ = select.select([chan, sys.stdin], [], [])   # 
            if chan in r:
                try:
                    x = u(chan.recv(1024))
                    if len(x) == 0:
                        sys.stdout.write("\r\n*** EOF\r\n")
                        break
                    sys.stdout.write(x)
                    sys.stdout.flush()
                except socket.timeout:
                    pass
            if sys.stdin in r:
                x = sys.stdin.read(1)  # 一个字符一个字符读入
                if len(x) == 0:
                    break
                chan.send(x)

    finally:
        termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)

try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.WarningPolicy())
    print("*** Connecting...")
    client.connect(hostname, port, username, password)
    chan = client.invoke_shell()
    print(repr(client.get_transport()))
    print("*** Here we go!\n")
    posix_shell(chan)
    chan.close()
    client.close()
except Exception as e:
    print("*** Caught exception: %s: %s" % (e.__class__, e))
    traceback.print_exc()
    try:
        if client:
            client.close()
    except:
        pass
    sys.exit(1)

写回答

1回答

沈无奇

2022-09-19

根据课程中步骤安装即可,最好选择Python3.8,相关代码在Python3.8中调试通过

0
0

一课玩转自动化运维全流程,轻松应对自动化运维岗

自动化运维实战

288 学习 · 26 问题

查看课程