我的要求是能够在Windows2012服务器上远程运行PowerShell脚本,这必须由使用Python的Linux服务器触发。
需要关于处理这个问题的最佳方法的建议,以及示例代码(如果可能的话)。
下面是我打算实现的步骤,但我看到它并不像预期的那样起作用。
我能够使用Python连接到远程Windows服务器。但我不认为这个方法会像预期的那样起作用。
需要一种有效和高效的方法来实现这一目标。
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()
发布于 2018-10-20 00:05:06
对于“terminal_server”设备类型,没有什么可做的。您现在必须手动传递。
下面是从ISSUES.md中提取的
是否支持通过终端服务器进行连接?
有一个“terminal_server”device_type,它基本上不做SSH连接后的任何操作。这意味着您必须手动处理与终端服务器的交互,才能连接到终端设备。当您完全连接到终端网络设备之后,您就可以“重新调度”,Netmiko将正常运行。
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")
https://stackoverflow.com/questions/52853285
复制相似问题