«

python paramiko模块 连接ssh

作者:myluzh 分类: Python 长度:1210 阅读:764


0x01 连接到linux ssh并执行命令

import paramiko
try:
    client = paramiko.SSHClient()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('10.1.123.218', 22, username='admin', password='1234123', timeout=5)
    # print("连接成功")
except:
    logger.info("连接ssh超时")
    exit()
command = "whoami"
stdin, stdout, stderr = client.exec_command(command)
for std in stdout.readlines():
    print(std)

0x02 连接到windows ssh并执行命令

因为windows的ssh是gbk所以有点麻烦,参考链接:paramiko连接windows10详解,远程管理windows服务器 https://www.cnblogs.com/machangwei-8/p/15215092.html

import paramiko
try:
    client = paramiko.SSHClient()
    client.load_system_host_keys()
    client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    client.connect('10.6.10.19', 22, username='administrator', password='Aa123..', timeout=5)
    # print("连接成功")
except:
    logger.info("连接ssh超时")
    exit()
command = "whoami"
stdin, stdout, stderr = client.exec_command(command)
info = stdout.read().decode('gbk') + stderr.read().decode('gbk')
print(info)

python windows paramiko ssh


正文到此结束
版权声明:若无特殊注明,本文皆为 Myluzh Blog 原创,转载请保留文章出处。
文章内容:https://itho.cn/py/196.html
文章标题:《python paramiko模块 连接ssh