Python script for SSH login with password


A basic ssh botnet – Puneet Kumar Maurya


SSH Connection with password in Python

Overview

In this article, Iam going to told you about how we can access in in a server by a password in Python.Normally, we use SSH command in Linux and Window to access on a server on the Internet.like :-

$ ssh example.com@ssh.example.com

Mostly, people use Putty which is already very famous in the world.But we want to do it by a scrtach manner in Python.

In Python, we use pxssh module to access by SSH on the net.I would like to continue and explain you on the topic and write about it's PXSSH class.

With the pxssh module, it's easy to access other servers over SSH.


This article which is based on the Python documentation found here:

What is pxssh?

A basic ssh botnet – Puneet Kumar Maurya


This class extends pexpect.spawn to specialize setting up SSH connections. This adds methods for login, logout, and expecting  in the shell prompt.
It use many ways  to handle many conditions in the SSH login process.
For example, if the session is your first login, then pxssh automatically accepts the remote certificate; or if you have public key authentication setup then pxssh won’t wait for the password prompt.
pxssh uses the shell prompt to synchronize output from the remote host. In order to make this more robust it sets the shell prompt to something more unique than just $ or #. This should work on most Borne/Bash or Csh style shells.

I use pxssh module for making ssh connections in python.

Module documentation

Open up a terminal and type in the following commands to get help about the module
import pxssh
help(pxssh)

Details on the module pxssh:

NAME
pxssh

FILE
/usr/lib/python3.8/dist-packages/pxssh.py

DESCRIPTION
This class extends pexpect.spawn to specialize setting up SSH connections.
This adds methods for login, logout, and expecting the shell prompt.

$Id: pxssh.py 513 2008-02-09 18:26:13Z noah $

CLASSES
pexpect.ExceptionPexpect(exceptions.Exception)
ExceptionPxssh
pexpect.spawn(__builtin__.object)
pxssh

Methods and login process

Python script for SSH login with password

Pxssh adds methods for login, logout, and expecting the shell prompt.

It does various tricky things to handle many situations in the SSH login process.


For example, if the session is your first login, then pxssh automatically accepts
the remote certificate; or if you have public key authentication setup then pxssh
won't wait for the password prompt.

How does pxssh works?

pxssh uses the shell prompt to synchronize output from the remote host.

In order to make this more robust it sets the shell prompt to something more
unique than just $ or #. This should work on most Borne/Bash or Csh style
shells.

Python Program



This example runs a few commands on a remote server and prints the result.

First we import the modules that we need. (pxssh and getpass)

We import the getpass module, which will prompt the user for a password,
without echoing what they type to the console.



import pxssh
import getpass

try:                                                           
value = pxssh.pxssh()
host_name = raw_input('host-name: ')
user_name = raw_input('Username: ')
password = getpass.getpass('Password: ')
value.login (host_name, user_name, password)
value.sendline ('uptime')  
value.prompt()            
print (value.before )
value.sendline ('ls -l')
value.prompt()
print (s.before)
value.sendline ('df')
value.prompt()
print (value.before)
value.logout()
except pxssh.ExceptionPxssh, e:
print "pxssh failed on login."
print (str(e))

Run a command on a remote SSH server in Linux.

Let's see  one more example,I want to show.

To run a command ('uptime') and to print the output,
you need to do something like that :
import pxssh
value = pxssh.pxssh()
if not value.login ('localhost', 'Username', 'Password'):
print ("SSH session failed on login.")
print (str(value))
else:
print ("SSH session login successful")
value.sendline ('uptime')
value.prompt()        
print (s.before )
value.logout()


value.sendline ('uptime;df -h')

Now, we have to understand about constructor and we use pexcept.spawn() to adding the arguments or path or location on the PC in the Linux directory in the Python Language.

This is the constructor. The command parameter may be a string that includes a command and any arguments to the command. For example:
child = pexpect.spawn('/usr/bin/ftp')
child = pexpect.spawn('/usr/bin/ssh root@example123.com')
child = pexpect.spawn('ls -latr /tmp')
I want explain more briefly, we have to add these Coding’s you can create your own program in Python.

Reactions

Post a Comment

0 Comments