Return to Snippet

Revision: 12297
at March 9, 2009 16:37 by adaminuwashinet


Updated Code
def interExecute(host,port,username,password,cmd):
        """Execute the given commands in an interactive shell."""
        transport = paramiko.Transport((host, port))            
        transport.connect(username = username, password = password)
        chan = paramiko.transport.open_session()
        chan.setblocking(0)
        chan.invoke_shell()

        out = ''

        chan.send(cmd+'\n')

        tCheck = 0

        # Wait for it.....
        while not chan.recv_ready():
            time.sleep(10)
            tCheck+=1
            if tCheck >= 6:
                print 'time out'#TODO: add exeption here 
                return False
        out = chan.recv(1024)
 
        return out

Revision: 12296
at March 9, 2009 16:35 by adaminuwashinet


Initial Code
def interExecute(host,port,username,password,cmd):
        """Execute the given commands in an interactive shell."""
        transport = paramiko.Transport((host, port))            
        transport.connect(username = username, password = password)
        chan = paramiko.transport.open_session()
        chan.setblocking(0)
        chan.invoke_shell()

        out = ''

        chan.send(cmd+'\n')

        tCheck = 0

        # Wait for it.....
        while not chan.recv_ready():
            time.sleep(10)
            tCheck+=1
            if tCheck >= 6:
                print 'time out'#TODO: add exeption here 
                return False
        out = chan.recv(1024)
101 
102 
103         return out

Initial URL

                                

Initial Description
It is necessary on occasion to execute a command in interactive shell. (I needed to to get access to some env vars... ) While there are many way to accomplish this I opted to run my cmd in interactive mode.
To do so I had to write this little wrapper snippet and figured it might be useful for someone else.

Initial Title
Runing  commnad in an interactive remote shell with python and paramiko

Initial Tags
ssh

Initial Language
Python