dilip
dilip

Reputation: 1

How to connect to windows 2012 machine from Centos using Python3

My requirement is ability to run a PowerShell script on a Windows 2012 server remotely, this has to be triggered from a Linux server using Python script.

Need suggestions on best way to handle this and also sample code (if possible).

Below are the steps I intend to achieve but i see it's not working as expected.

  1. PowerShell scripts to be executed are already placed in Windows server (2012).
  2. Python3 program running on Linux (CentOS) does SSH to Windows server (2012) using netmiko module.
  3. sends the command (PowerShell command to execute script in remote Windows server) over the SSH connection.

I was able to connect to the remote Windows server using Python. But I don't see this method working as expected.

Need an effective and efficient way to achieve this.

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()

Upvotes: 0

Views: 418

Answers (1)

yoonghm
yoonghm

Reputation: 4645

Nothing much is done for 'terminal_server" device type. You have to do manual passing at the moment.

Below is extracted from COMMON_ISSUES.md

Does Netmiko support connecting via a terminal server?

There is a 'terminal_server' device_type that basically does nothing post SSH connect. This means you have to manually handle the interaction with the terminal server to connect to the end device. After you are fully connected to the end network device, you can then 'redispatch' and Netmiko will behave normally

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")

Upvotes: 1

Related Questions