Skip to main content

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


Python Secret #Author: ManishHacker1

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:

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


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.


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


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.

If you want to learn more interesting article. Then subscribe,share and like.
Thank you very much for your support and love.

Krypsec Digital Security Provided Python Training
  • Best Python Training in Noida
  • Best Python Training in Delhi
  • Best Python Training in Meerut
  • Best Python Training in India
Purchase Cheap Computer Accessories

Thank You for reading this article. And also like my FB page givin below link and share it.

Follow ManishHacker1

Comments

  1. Thank you. Truly appreciate all your hardwork, especially your down-to-earth attitude.

    Also Refer this site for latest updates of Machine Learning with Python"

    Also Visit this site for "The Best Python Certification"

    ReplyDelete

Post a Comment

Popular posts from this blog

How to create Folder Lock with Password Protected using Python

Amazon.in Widgets Python Blog #Author: ManishHacker1 Hello guys, Today we will be learn how to create folder lock with password protected using Python programming language. What is Folder Lock? Folder Lock is a data security software that is allows its users to encrypt thier files and folder. Lock, hide and password protects files and folder on your computer. You can use Folder Lock to secure your files and folder on windows. So, I am going to tell you how to make your own Folder Lock, without using any software. Pre-Reuisites: You will need only pyhton installed in your operating system. if you want to distribute your Folder Lock then you will be also need pyinstaller using to convert our python file to .exe file. Compatibility: Compatible with any Microsoft Windows Operating system. Instructions: First open your python IDE or Notepad, where you want to write code. If you use to notepad please remember indentation using to write code. Or copy th...

How to Create MD5 Brute Force Script Using Python

Python Secret #Author: ManishHacker1 How to Create MD5 Brute Force Script Using Python Hello guys, Today we will be learn How to create MD5 brute force script using Python. What is brute-force Attack? Brute-force attack also known exhaustive key search Process of checking all possible keys Using a dictionary to attack with Dictionary is usually more effective than searching the whole key space. Exponentially grow with increasing key size. Brute force Attack Limit Because time/energy required to crack a key grows exponentially with key size, encryption in today's standards and computing power are safe brute-force attack. A 256 bit key would take on 50 of today's super computer 3x10^51 years What is Dictionary Attack Much faster than a whole key space search Not guaranteed Commonaly used on passwords Dictionaries can be found online A popular one is darkc0de.lst Good to run before a big dictionary like darkc0de. Can eliminate the most com...