首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何使用Python3从Centos连接到windows 2012机器

如何使用Python3从Centos连接到windows 2012机器
EN

Stack Overflow用户
提问于 2018-10-17 10:58:59
回答 1查看 315关注 0票数 0

我的要求是能够在Windows2012服务器上远程运行PowerShell脚本,这必须由使用Python的Linux服务器触发。

需要关于处理这个问题的最佳方法的建议,以及示例代码(如果可能的话)。

下面是我打算实现的步骤,但我看到它并不像预期的那样起作用。

  1. 要执行的PowerShell脚本已经放置在Windows (2012)中。
  2. 运行在Linux上的Python3程序(CentOS)使用netmiko模块对Windows (2012年)执行SSH操作。
  3. 通过SSH连接发送命令(用于在远程Windows中执行脚本)。

我能够使用Python连接到远程Windows服务器。但我不认为这个方法会像预期的那样起作用。

需要一种有效和高效的方法来实现这一目标。

代码语言:javascript
运行
复制
from netmiko import ConnectHandler

device = ConnectHandler(device_type="terminal_server",
                        ip="X.X.X.x",
                        username="username",
                        password="password")
hostname = device.find_prompt()
output = device.send_command("ipconfig")
print (hostname)
print (output)
device.disconnect()
EN

回答 1

Stack Overflow用户

发布于 2018-10-20 00:05:06

对于“terminal_server”设备类型,没有什么可做的。您现在必须手动传递。

下面是从ISSUES.md中提取的

是否支持通过终端服务器进行连接?

有一个“terminal_server”device_type,它基本上不做SSH连接后的任何操作。这意味着您必须手动处理与终端服务器的交互,才能连接到终端设备。当您完全连接到终端网络设备之后,您就可以“重新调度”,Netmiko将正常运行。

代码语言:javascript
运行
复制
from __future__ import unicode_literals, print_function
import time
from netmiko import ConnectHandler, redispatch

net_connect = ConnectHandler(
    device_type='terminal_server',        # Notice 'terminal_server' here
    ip='10.10.10.10', 
    username='admin', 
    password='admin123', 
    secret='secret123')

# Manually handle interaction in the Terminal Server 
# (fictional example, but hopefully you see the pattern)
# Send Enter a Couple of Times
net_connect.write_channel("\r\n")
time.sleep(1)
net_connect.write_channel("\r\n")
time.sleep(1)
output = net_connect.read_channel()
print(output)                             # Should hopefully see the terminal server prompt

# Login to end device from terminal server
net_connect.write_channel("connect 1\r\n")
time.sleep(1)

# Manually handle the Username and Password
max_loops = 10
i = 1
while i <= max_loops:
    output = net_connect.read_channel()

    if 'Username' in output:
    net_connect.write_channel(net_connect.username + '\r\n')
    time.sleep(1)
    output = net_connect.read_channel()

    # Search for password pattern / send password
    if 'Password' in output:
    net_connect.write_channel(net_connect.password + '\r\n')
    time.sleep(.5)
    output = net_connect.read_channel()
    # Did we successfully login
    if '>' in output or '#' in output:
        break

    net_connect.write_channel('\r\n')
    time.sleep(.5)
    i += 1

# We are now logged into the end device 
# Dynamically reset the class back to the proper Netmiko class
redispatch(net_connect, device_type='cisco_ios')

# Now just do your normal Netmiko operations
new_output = net_connect.send_command("show ip int brief")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52853285

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档