0

I'm fairly new to how Paramiko works, but my main objective is to be able to run automated commands over SSH using Python.

I have the following code and I am trying to run a simple ls command to start off with but for some odd reason the code seems to get stuck and no output or error messages are produced.

import sys
import paramiko as pm
sys.stderr = sys.__stderr__
import os

class AllowAllKeys(pm.MissingHostKeyPolicy):
    def missing_host_key(self, client, hostname, key):
        return

HOST = '192.168.31.1'
USER = 'admin'
PASSWORD = 'admin'

client = pm.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(pm.AutoAddPolicy())

client.connect(HOST, username=USER, password=PASSWORD)

channel = client.invoke_shell()
stdin = channel.makefile('wb')
stdout = channel.makefile('rb')

stdin.write('''
ls
exit
''')
print stdout.read()

stdout.close()
stdin.close()

Any help would be much appreciated :)

3
  • 1
    Are you sure stdin and stdout are not inverted? Commented May 29, 2013 at 10:37
  • @mrpopo,better u can use pexpect module instead of paramiko Commented May 30, 2013 at 5:46
  • 1
    I wanted to use pexpect initially but I'm using a windows machine :/ Can't import resource on windows. No worries, I'll just virtualize a debian client and learn pexpect. Commented May 30, 2013 at 15:48

3 Answers 3

1

I attempted the same route you were going, and didn't have much luck either. What I then opted for were the send() and recv() methods of the paramiko Channel class. Here is what I used:

>>> s = paramiko.SSHClient()
>>> s.load_system_host_keys()
>>> s.set_missing_host_key_policy(paramiko.AutoAddPolicy())
>>> s.connect(HOST,USER,PASS)
>>> c = s.invoke_shell()
>>> c.send('ls')
>>> c.recv(1024)
ls
bin     etc     usr     home
proc    var     lib     tmp
Sign up to request clarification or add additional context in comments.

Comments

0

import paramiko ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) target_host = 'x.x.x.x' target_port = 22 target_port = 22 pwd = ':)' un = 'root' ssh.connect( hostname = target_host , username = un, password = pwd ) stdin, stdout, stderr = ssh.exec_command('ls;exit')

Comments

-2

@mrpopo,try this code which uses pecpect,

import pexpect
import pxssh
import time
import os

    def tc(ipaddr,password):
        try:
            ss = pexpect.spawn(ipaddr)
            ss.logfile = sys.stdout
                print "SSH connecting"
                print 
        except:
            print "connection refused"
            print
            #sys.exit()

            try:
                ss.expect (':')
                ss.sendline (password +"\n")
                    print "connected"
                    time.sleep(30)
                    ss.expect (">")
                    print "connection established"
                    print

            except:
                    print "Permission denied, please try again."
                        print
                        sys.exit()
            try:
                ss.sendline ('ls\n')
                ss.expect ('>')

    tc("ssh [email protected],admin")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.