[foogallery id=”1024″]






How to Connect Window Machine to Linux Machine Using Python using SSH

#Author: ManishHacker1

How to Connect Window Machine to Linux Machine Using Python

Hello everyone, This is my another article about SSH connectivity to the client machine. In my previous article we have some trouble using pxssh module on windows machine. That’s why I am writing another article for the SSH connection.

In this article we will use paramiko module to connect another machine. This is fully supported windows, Mac and Linux machine users.

In this article, I will show you how to use Paramiko SSH (a Python SSH library) to connect and gather information from another Machine.

What is Paramiko?

Paramiko is a Python (2.6+, 3.3+) implementation of the SSHv2 protocol [1], providing both client and server functionality. While it leverages a Python C extension for low level cryptography (Cryptography), Paramiko itself is a pure Python interface around SSH networking concepts.

Read Full Documentation

Let start our demonstration:

How to Connect Window Machine to Linux Machine Using Python via SSH

Requirment:Paramiko( For SSH connection)

How to Install paramilko module:

The first step is to get the Paramiko SSH library installed.

Open your command prompt and type:


pip install paramiko    and press enter button

How to Connect Window Machine to Linux Machine Using Python


After finish installation we will start our main code.
Open your IDLE and put some code for check connectivty.

Note:Before this process please start your SSH server in your client machine(Linux).

If you don’t have SSH server in your client machine first install it:

Type in your terminal:


apt-get install openssh-server
service -status -all
service ssh start

After that go to your window machine and open your IDLE and type below command in your IDLE.


>>> import paramiko
>>> ip = "192.168.201.132"
>>> user = "root"
>>> passwd = "root"
>>> client = paramiko.SSHClient()
>>> client

>>> client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> client.connect(ip,username=user,password=passwd)

In the above example first we import paramiko module. After that we set ip,username and password.After the variables are initialized, then I create a Paramiko SSHClient object. Paramiko describes the SSHClient class as, “A high-level representation of a session with an SSH server”.After that we set host key policy. We are ready to connect client machine.

If all process are correct without any error we are ready to send command and recieve input in your client machine. Now type in your IDLE


>>> remote_connection = client.invoke_shell()
>>> output = remote_connection.recv(1000)
>>> print output

When you will run above command we may see like below output.


Linux kali 3.7-trunk-686-pae #1 SMP Debian 3.7.2-0+kali8 i686

The programs included with the Kali GNU/Linux system are free software;
the exact distribution terms for each program are described in the
individual files in /usr/share/doc/*/copyright.

Kali GNU/Linux comes with ABSOLUTELY NO WARRANTY, to the extent
permitted by applicable law.
Last login: Sat Oct  7 10:23:56 2017 from 192.168.201.1


root@kali:~#

Next step type in your IDLE:


>>> remote_connection.send("ls\n")
3
>>> output = remote_connection.recv(5000)
>>> print output
ls

Desktop    fjZQXehT.jpeg  sslstrip.log

root@kali:~# 
>>> 

Great!!! Now we are able to connect to the client machine, and send a command to the client machine and recieve output. Let’s start create a python program which automatically connect our client machine.


import threading
import paramiko
import subprocess
import sys

__Author__="""

******************************************************************************
------------------------------------------------------------------------------
                Created By ManishHacker1
                Follow on FB: https://www.facebook.com/ManishHacker1
                Follow on FB: https://www.facebook.com/krypsec
                BLOG: https://pythonsecret.blogspot.in
                Website: http://krypsec.com
------------------------------------------------------------------------------                
******************************************************************************              
"""
print __Author__

ip = raw_input("Enter your client IP address: ")
user = raw_input("Enter your client machine Username: ")
passwd = raw_input("Enter your client machine Password: ")

client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(ip, username=user, password=passwd)

print "SSH connection established to %s" % ip

remote_conn = client.invoke_shell()
print "Interactive SSH session established"
output = remote_conn.recv(1000)
print output



while True:
    try:
        command = raw_input("root@ManishHacker1> ")
        ssh_session = client.get_transport().open_session()
        if ssh_session.active:
            ssh_session.exec_command(command)
            print ssh_session.recv(1024)
    except:
        print "Keyboard Interuppt, quit session"
        sys.exit()

    

How to Use:

The above script save as “anyname.py” in any location. Where .py is extension. In my case I save my script on “F:\Studend\Python\Advanced\SSH”.
Now you can direct run your code using IDLE. Go to your “Run” tab and click Run Mudule button.
You can see a prompt where program ask your client Machine IP address. Put your Linux Machine IP address and press enter button.

After that program ask your Linux Machine username. Put your Linux Machine username and press enter button.

Next, program ask your Linux Machine Password. Put your Linux Machine password and press enter button.

How to Connect Window Machine to Linux Machine Using Python


Or open your command prompt and set path where your python script is store.

How to Connect Window Machine to Linux Machine Using Python


you can see in above picture. we have successfully connect our client machine. Now you are ready to access your Linux Machine information. After that put your command and press enter button.
Voilla !!! You can access all information in your client Machine(Linux) using commands.


You can see in the above picture. We put “ls” command and press enter button. And we find output. And our script ready to next command.

Well, you can access all information your client machine using commands.

If you want to terminate your session then type:

Press CTRl+C

I hope you will have enjoyed read this article.(How to Connect Window Machine to Linux Machine Using Python)


If you want to learn more interesting article. Then subscribe,share and like.

Thank you very much for your support and love.

Best Python Training and Ethical Hacking Training in Meerut, Noida , Delhi.


Thank You for reading this article(How to Connect Window Machine to Linux Machine Using Python).
And also like my FB page givin below link and share it.

Follow ManishHacker1