How to Connect and Run Linux commands or scripts from Windows using Python

||
Posted 4 years ago
||
Views 391
||
1 min read
0 reactions

Step 1:

pip install paramiko

After the above installation, you can use the below sample script to connect Linux machine from Windows.

import paramiko
import sys


def etl_trigger(host,user,passw,cmd):
    nbytes = 4096
    hostname = host
    port = 22
    username = user
    password = passw
    command = cmd
    try:
        ssh=paramiko.SSHClient()
        ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
        ssh.connect(hostname,port,username,password)
        stdin,stdout,stderr=ssh.exec_command('ls -ltr')
        outlines = stdout.readlines()
        resp = ''.join(outlines)
        # print(resp)
        return True
    except Exception as e:
        print(e)
        return False



hostname = 'server name'
username = username
password = password
command = 'unix command'

if __name__ == '__main__':
    etl_trigger(hostname, username, password, command)

Please let us know if you guys face any issues.

Happy Coding !!!


0 reactions

Discussion


Looking for Freelancing Jobs
Joined on April 15, 2020

Latest Videos