三方组件

paramiko

创建ssh 连接

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13

def connect_ssh(host: str, port: int, username: str, password: str):

    ssh = paramiko.SSHClient()

    key = paramiko.AutoAddPolicy()

    ssh.set_missing_host_key_policy(key)

    ssh.connect(host, port, username, password, timeout=5)

    return ssh

执行命令

1
2
3
4
5
6
7
8
9

def execute(ssh: paramiko.SSHClient):

    stdin, stdout, stderr = ssh.exec_command('command')

    for line in stdout.readlines():

        print(line)