Remote Code Execution on Windows Server From a Linux Server
A quick tutorial on how to execute scripts on a Windows server remotely from a Linux server using the Python module Paramiko.
Join the DZone community and get the full member experience.
Join For FreeLet's say a Workflow Management Framework of our choice based on the business needs works only on Linux. And let's say our jobs/scripts depend on some libraries that are only compatible with Windows. One of the solutions here could be to host the Workflow Management Framework on a Linux server and execute the jobs on a Windows server via SSH. So the jobs will be triggered on the Linux server and executed on the Windows server.
Paramiko is a Python implementation of the SSH protocol, providing both client and server functionality. It is a Python interface around SSH networking concepts. http://www.paramiko.org/
Let's get started.
Make sure SSH is enabled on both the servers.
ssh <hostname>
Install the Paramiko module on the Linux server.
pip install paramiko
On the Windows server, create a python file (winser_test.py) and add the below sample code.
xxxxxxxxxx
import os
os.system("systeminfo")
On the Linux server, create a python file (linser_test.py) and add the below client code.
xxxxxxxxxx
# import the library
import paramiko
# initialize the ssh client
ssh_client = paramiko.SSHClient()
# enable host auto-add policy
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# connect to windows server
ssh_client.connect(hostname = <hostname>, username = <username>, password = <password>, port = 22)
# execute the script on windows server
stdin, stdout, stderr = ssh_client.exec_command('python <path_to_the_file>winser_test.py')
# print the standard out and error (if any)
print('stdout -- ', stdout.read())
print('stderr -- ', stderr.read())
Now, execute the linser_test.py script and that will in turn execute the winser_test.py on the Windows server.
Reference: http://docs.paramiko.org/en/stable/
Opinions expressed by DZone contributors are their own.
Comments