SSH in python

Hi all,
Im trying to build a custom SSh client in python using sockets in windows.
below is a snippet of my function in the code

#!/usr/bin/env python
import os
import sys
from socket import *


localPortNo=8000
maxTries=10
blockSize=65536*16

def createTCPSocketSSH (remoteHostname, remotePort=22, localPort=-1):
    global localPortNo
    if localPort == -1:
        localPort = localPortNo
        localPortNo = localPortNo+1
    tryNo = 1
    while 1:
        command = "ssh -f -g -A -X %s\n" \
                  % (remoteHostname)        
        result = os.system(command)
        if result == 0:
            break
        localPortNo = localPortNo+1
        tryNo = tryNo + 1
        if tryNo == maxTries:
            os.exit(1)


    # create a TCP socket which connects to our ssh pipe
    s = socket(AF_INET, SOCK_STREAM)
    s.connect(("localhost", localPort))
    return s

It seems that everytime I run it, it give me this error
" 'ssh' is not recognized as an internal or external command, operable program or batch file. "
What is causing this? Are there any other examples of SSH clients using sockets? Im trying to use twisted framework but it seems to be unstable ( cant login twice). Any help on either is greatly appriciated!
Thanks
Marc

LVL 3
MytixAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ramromconsultant Commented:
Running under Windows? How are you starting the program? At a Command Prompt?

The error suggests you are doing C>ssh Is that true?
0
MytixAuthor Commented:
'doh...okay....got it there, but I still want to run SSH in windows from python...
i'm currently using paramiko....this is a sample of my code which does an ls -al twice and doesnt seem to be working (note the server info is alright). Any other suggestion for a stable multi platform ssh client?

#!/usr/bin/env python

import sys, os, socket, threading
import paramiko, base64

blockSize=65536*16
server = host = "localhost"
port = 22
buf = 1024
addr =(host,port)
user = "user1"
passwrd = "user1"

t = paramiko.Transport(host)
t.connect(username=user, password=passwrd)
chan = t.open_session()

chan.exec_command('ls -al')
for line in chan.makefile('r+'):
    print line.strip('\n')

chan.exec_command('ls -al')
for line in chan.makefile('r+'):
    print line.strip('\n')
chan.close()
t.close()
0
ramromconsultant Commented:
The more you say the less I understand. It sounds like you have multiple problems. Is that true?

What is the name of the module of your first code example?

Exactly what are you doing to run it?

What does "got it there" mean?

What does "doesnt seem to be working" mean. What is not working and what is the evidence?

What does "stable" mean?

If you can provide clear explanations we might be able to help. Otherwise we are more lost than you are.
0
MytixAuthor Commented:
okay...let me clarify...disregard the first code's example......i've found the problem and im using my second code as the question.
the second code (which requires the paramiko module) works half way....when I try to `ls -al` the first time it work...when I try to do it again..it says that chan is not open for exec_command.
Stable meaning that if i execute a command it doesnt disconnect me from the server or the connection can be maintained using simple commands in the code. Twisted framework is abit unstable because everytime I execute a command and close a connection...the next time I try to open it, it becomes non-responsive...
Any ideas?
0
TraltixxCommented:
Hmmm, well what about doing it like this:

#!/usr/bin/env python

import sys, os, socket, threading
import paramiko, base64

blockSize=65536*16
server = host = "localhost"
port = 22
buf = 1024
addr =(host,port)
user = "user1"
passwrd = "user1"

t = paramiko.Transport(host)
t.connect(username=user, password=passwrd)
chan = t.open_session()

chan.exec_command('ls -al')
for line in chan.makefile('r+'):
   print line.strip('\n')
chan.close()
t.close()


t = paramiko.Transport(host)
t.connect(username=user, password=passwrd)
chan = t.open_session()

chan.exec_command('ls -al')
for line in chan.makefile('r+'):
   print line.strip('\n')
chan.close()
t.close()

It does `ls -al` twice but makes the connection twice. This way works but you have the overhead of reconnecting.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.