python 通过ssh获取Windows server 2016 DHCP的信息状态

本文介绍如何在 Windows Server 2016 上安装 OpenSSH,并通过 Python 脚本利用 Paramiko 库实现远程获取 DHCP 服务器上的用户信息。
Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

一、Windowsserver 2016安装ssh

1、 下载OpenSSH

Releases · PowerShell/Win32-OpenSSH · GitHub

2、 将下载的文件解压到文件夹路径C:\Program Files\OpenSSH

3、 运行命令提示符(管理员),使用cd命令到步骤3中文件夹OpenSSH的位置,然后输入命令后回车: powershell.exe -ExecutionPolicy Bypass -File install-sshd.ps1

 4、 步骤3成功后,继续如下的命令后回车: netsh advfirewall firewall add rule name=sshd dir=in action=allow protocol=TCP localport=22

 5、然后使用命令打开sshd,如下: net start sshd

二、Python通过ssh脚本获取到dhcp的用户信息

#!/usr/bin/python
# coding=utf-8
import paramiko


class windhcp(object):

    def SSHconnection(self, dhcpserver, username, password):
        ssh = paramiko.SSHClient()

        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        # print "Eseguo connessione SSH"
        ssh.connect(dhcpserver, 22, username, password)
        return ssh

    def SSHclose(self):
        ssh.close()

    def GETscopes(self):
        # print "Eseguo GETdhcpScopes"
        # id della rete dello scope
        get_scopes = 'netsh dhcp server scope 192.168.200.0    show clientsvq  '
        stdin, stdout, stderr = ssh.exec_command(get_scopes)
        # print(stdout.read().splitlines())
        # s = stdout.read()
        # print(s.decode("gbk"))
        a=0
        for line in stdout.read().splitlines():
            a +=1
            print(a,line.decode("gbk"))


windhcp = windhcp()
ssh = windhcp.SSHconnection("服务器ip地址",'账号','密码')
windhcp.GETscopes()
netsh dhcp server show all

您可能感兴趣的与本文相关的镜像

Python3.8

Python3.8

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值