Myluzh Blog

python paramiko模块 连接ssh

发布时间: 2022-9-15 文章作者: myluzh 分类名称: Python 朗读文章


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

发表评论