[Twisted-Python] DOUBTS PLEASE SOLVE

Hi al i am new to twisted. I had been assigned a project on SSH. the project is as follows. i have to login to a remote Linux machine through Windows i have to execute a shell script and print the output back in Windows machine this needs to be done through SSH. Any clues . please help. i need it urgently

On Sun, 28 Sep 2008 16:43:25 +0530, shweta mani shweta3230@gmail.com wrote:
Hi al i am new to twisted. I had been assigned a project on SSH. the project is as follows. i have to login to a remote Linux machine through Windows i have to execute a shell script and print the output back in Windows machine this needs to be done through SSH. Any clues . please help. i need it urgently
Take a look at the API docs and the examples. Perhaps get a copy of _Twisted Network Programming Essentials_ or find the Conch chapter online. If you get stuck, come back with specific questions.
Jean-Paul

Jean-Paul Calderone schrieb:
On Sun, 28 Sep 2008 16:43:25 +0530, shweta mani shweta3230@gmail.com wrote:
Hi al i am new to twisted. I had been assigned a project on SSH. the project is as follows. i have to login to a remote Linux machine through Windows i have to execute a shell script and print the output back in Windows machine this needs to be done through SSH. Any clues . please help. i need it urgently
Take a look at the API docs and the examples. Perhaps get a copy of _Twisted Network Programming Essentials_ or find the Conch chapter online. If you get stuck, come back with specific questions.
Hope this isn't too offensive but last time I checked conch didn't work on win32 (http://twistedmatrix.com/trac/ticket/50). I guess most users would be happy with a basic "secure RPC" implementation, no console stuff. No idea how much work that is though ...
cheers Paul

On Sun, 28 Sep 2008 02:11:49 +0200, paul paul@subsignal.org wrote:
Jean-Paul Calderone schrieb:
On Sun, 28 Sep 2008 16:43:25 +0530, shweta mani shweta3230@gmail.com wrote:
Hi al i am new to twisted. I had been assigned a project on SSH. the project is as follows. i have to login to a remote Linux machine through Windows i have to execute a shell script and print the output back in Windows machine this needs to be done through SSH. Any clues . please help. i need it urgently
Take a look at the API docs and the examples. Perhaps get a copy of _Twisted Network Programming Essentials_ or find the Conch chapter online. If you get stuck, come back with specific questions.
Hope this isn't too offensive but last time I checked conch didn't work on win32 (http://twistedmatrix.com/trac/ticket/50). I guess most users would be happy with a basic "secure RPC" implementation, no console stuff. No idea how much work that is though ...
Ticket 50 is about the conch command line client. I'm not sure if the rest of conch, the part you would use to automate an SSH login, works on Windows or not. It shouldn't be affected by the lack of an fcntl module (or any of the other modules missing on Windows), but it may accidentally import them, preventing the code from working out of the box, even though it doesn't use them. This is probably simpler to fix than actually writing a command line client. However, it may already work; I don't have a Windows machine handy to try, but by looking at buildbot I can see that a significant portion of the Conch test suite runs and passes on there.
Jean-Paul

For a non-twisted solution, you can also try paramiko: http://www.lag.net/paramiko/
It might be a bit easier to wrap your brain around if you are very new to the twisted way of doing things.
It should also work nicely on Win32 (I haven't checked in a while but the last time I used it it worked fine).
Kevin Horn
On Sat, Sep 27, 2008 at 7:11 PM, paul paul@subsignal.org wrote:
Jean-Paul Calderone schrieb:
On Sun, 28 Sep 2008 16:43:25 +0530, shweta mani shweta3230@gmail.com wrote:
Hi al i am new to twisted. I had been assigned a project on SSH. the project is as follows. i have to login to a remote Linux machine through Windows i have to execute a shell script and print the output back in Windows machine this needs to be done through SSH. Any clues . please help. i need it urgently
Take a look at the API docs and the examples. Perhaps get a copy of _Twisted Network Programming Essentials_ or find the Conch chapter online. If you get stuck, come back with specific questions.
Hope this isn't too offensive but last time I checked conch didn't work on win32 (http://twistedmatrix.com/trac/ticket/50). I guess most users would be happy with a basic "secure RPC" implementation, no console stuff. No idea how much work that is though ...
cheers
Paul
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

On Sun, Sep 28, 2008 at 02:11:49AM +0200, paul wrote:
Hope this isn't too offensive but last time I checked conch didn't work on win32 (http://twistedmatrix.com/trac/ticket/50). I guess most users would be happy with a basic "secure RPC" implementation, no console stuff. No idea how much work that is though ...
Conch does work on Win32, I have written an application that uses it (not the command line client). You have to install pycrypto in addition to Twisted.

hi friends thanks for immediate reply. i have referred the book twisted.......... currently i am facing the problem. i have to show ls -l output in a listbox using Tkinter but because of TCP it first stores information in a window or some packet format. so it doesnot show the output in a continuous manner.
On Tue, Sep 30, 2008 at 2:07 AM, Jack Whitham jack-tp@cs.york.ac.uk wrote:
On Sun, Sep 28, 2008 at 02:11:49AM +0200, paul wrote:
Hope this isn't too offensive but last time I checked conch didn't work on win32 (http://twistedmatrix.com/trac/ticket/50). I guess most users would be happy with a basic "secure RPC" implementation, no console stuff. No idea how much work that is though ...
Conch does work on Win32, I have written an application that uses it (not the command line client). You have to install pycrypto in addition to Twisted.
-- Jack Whitham jack@cs.york.ac.uk
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

from twisted.conch import error
from twisted.conch.ssh import transport, connection, keys, userauth, channel, common
from twisted.internet import defer, protocol, reactor import re import Tkinter from Tkinter import * from tkMessageBox import * from os.path import exists from Tkinter import * import commands #from DT import demo import shutil from os.path import exists import ftplib import sys from ftplib import FTP from tkMessageBox import * from tkFileDialog import asksaveasfilename import string from os import popen from re import split import Tkinter import os
win = Tkinter.Tk(); Label(win,text='CARD UPLOAD UTILITY').pack(side=TOP)
#btn=Tkinter.Button(win,text='LIST ALL SUB FILES',command =(lambda : slist())) #btn.pack(side=BOTTOM)
def submit(): login=textlog.get() password=textpass.get() if login=="" or password=="": showinfo('ERROR','PLEASE FILL THE NECESSARY DETAILS ') elif login=='m' and password=='m':
print 'HELLO USER' top.withdraw()#closes current window import sys, getpass
server = '10.144.17.2'
command = 'ls -l'
username = 'cteam'
password = 'cteam'
factory = ClientCommandFactory( username, password, command)
reactor.connectTCP(server, 22, factory)
reactor.run( )
class ClientCommandTransport(transport.SSHClientTransport):
def __init__(self, username, password, command):
self.username = username
self.password = password
self.command = command print "username",username print "password",password print "command",command
def verifyHostKey(self, pubKey, fingerprint):
# in a real app, you should verify that the fingerprint matches
# the one you expected to get from this server
return defer.succeed(True)
def connectionSecure(self):
self.requestService(
PasswordAuth(self.username, self.password,
ClientConnection(self.command)))
class PasswordAuth(userauth.SSHUserAuthClient):
def __init__(self, user, password, connection):
userauth.SSHUserAuthClient.__init__(self, user, connection)
self.password = password
def getPassword(self, prompt=None):
return defer.succeed(self.password)
class ClientConnection(connection.SSHConnection):
def __init__(self, cmd, *args, **kwargs):
connection.SSHConnection.__init__(self)
self.command = cmd
def serviceStarted(self):
self.openChannel(CommandChannel(self.command, conn=self))
class CommandChannel(channel.SSHChannel):
name = 'session'
def __init__(self, command, *args, **kwargs):
channel.SSHChannel.__init__(self, *args, **kwargs)
self.command = command
def channelOpen(self, data):
self.conn.sendRequest(
self, 'exec', common.NS(self.command), wantReply=True).addCallback(
self._gotResponse)
def _gotResponse(self, _):
self.conn.sendEOF(self)
def dataReceived(self, data): #self.dat=date print "nenu",data dat=data #print"nenu1",dat lis(dat) def lis(m):
Label(win,text='%s'%m).pack(side=TOP)
dirfm = Frame(win) dirsa=Scrollbar(dirfm) dirsb= Scrollbar(dirfm) dirsb.pack(side = RIGHT, fill =Y)
dirs = Listbox(dirfm,height = 50, width = 70, yscrollcommand = dirsb.set) textsfile = Entry(dirfm) def closed(self):
reactor.stop( ) print "nenuclose"
#print"nenu" ,data[56] #print"nenu" ,data[57] #print"nenu" ,data[58] #print"nenu" ,data[59] #print"nenu" ,data[63] '''p=1 for i in range(40,100): if(data[i]=='-' or data[i]=='x'): p=i print"nenu" ,data[p-9] #ClientCommandFactory(username, password, command)
#__init__(self, username, password, command)'''
class ClientCommandFactory(protocol.ClientFactory):
def __init__(self, username, password, command):
self.username = username
self.password = password
self.command = command
def buildProtocol(self, addr):
protocol = ClientCommandTransport(
self.username, self.password, self.command)
return protocol
if __name__ == "__main__": top = Tkinter.Tk() #to draw Label in the main page
label = Tkinter.Label(top, text=' CARD UPLOAD UTILITY ' ) label.pack()
#to draw Label
label = Tkinter.Label(top, text='ENTER LOGIN ') label.pack(side=LEFT,fill=X)
#to draw Textbox tv = Tkinter.StringVar( ) textlog=Entry(top,width=20,textvariable=tv) textlog.pack(side=LEFT)
#to draw Label
label = Tkinter.Label(top, text=' ENTER PASSWORD ') label.pack(side=LEFT)
#to draw Textbox tv1 = Tkinter.StringVar( ) textpass=Entry(top,width=20,textvariable=tv1) textpass.pack(side=LEFT)
#to draw submit submit=Tkinter.Button(top, text=' SUBMIT ',command=submit) submit.pack(side=LEFT) top.bind_all('<Return>',(lambda event: submit()))
#to draw Quit quit = Tkinter.Button(top, text=' QUIT ',command=top.quit) quit.pack(side=LEFT)
Tkinter.mainloop()
#put m as username and m as password
On Tue, Sep 30, 2008 at 9:12 PM, shweta mani shweta3230@gmail.com wrote:
hi friends thanks for immediate reply. i have referred the book twisted.......... currently i am facing the problem. i have to show ls -l output in a listbox using Tkinter but because of TCP it first stores information in a window or some packet format. so it doesnot show the output in a continuous manner.
On Tue, Sep 30, 2008 at 2:07 AM, Jack Whitham jack-tp@cs.york.ac.ukwrote:
On Sun, Sep 28, 2008 at 02:11:49AM +0200, paul wrote:
Hope this isn't too offensive but last time I checked conch didn't work on win32 (http://twistedmatrix.com/trac/ticket/50). I guess most users would be happy with a basic "secure RPC" implementation, no console stuff. No idea how much work that is though ...
Conch does work on Win32, I have written an application that uses it (not the command line client). You have to install pycrypto in addition to Twisted.
-- Jack Whitham jack@cs.york.ac.uk
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

On Tue, 30 Sep 2008 21:24:03 +0530, shweta mani shweta3230@gmail.com wrote:
[snip]
This isn't the right way to use Tk with Twisted.
See http://twistedmatrix.com/projects/core/documentation/howto/choosing-reactor....
Jean-Paul

hi all i got the solution of listing all ls -l files in a listbox . Next problem is to use recurrsively ls -l command again if a user selects a directory. Can we use multiple in Linux commands within a program withoy using pipe. i am forwarding the code. i need to use ls -l command dependng on whether he chooses a directory.
#!/usr/bin/env python from twisted.conch.ssh import transport, userauth, connection, common, keys, channel from twisted.internet import defer, protocol, reactor from twisted.python import log import struct, sys, getpass, os import re import Tkinter from Tkinter import * from tkMessageBox import * from os.path import exists from Tkinter import * import commands #from DT import demo import shutil from os.path import exists import ftplib import sys from ftplib import FTP from tkMessageBox import * from tkFileDialog import asksaveasfilename import string from os import popen from re import split import Tkinter import os USER = 'cteam' # replace this with a valid username HOST = '10.144.17.2' # and a valid host
class SimpleTransport(transport.SSHClientTransport): def verifyHostKey(self, hostKey, fingerprint): print 'host key fingerprint: %s' % fingerprint return defer.succeed(1) def connectionSecure(self): self.requestService( SimpleUserAuth(USER, SimpleConnection())) class SimpleUserAuth(userauth.SSHUserAuthClient): def getPassword(self): return defer.succeed(getpass.getpass("%s@%s's password: " % (USER, HOST))) def getGenericAnswers(self, name, instruction, questions): print name print instruction answers = [] for prompt, echo in questions: if echo: answer = raw_input(prompt) else: answer = getpass.getpass(prompt) answers.append(answer) return defer.succeed(answers)
def getPublicKey(self): path = os.path.expanduser('~/.ssh/id_dsa') # this works with rsa too # just change the name here and in getPrivateKey if not os.path.exists(path) or self.lastPublicKey: # the file doesn't exist, or we've tried a public key return return keys.getPublicKeyString(path+'.pub') def getPrivateKey(self): path = os.path.expanduser('~/.ssh/id_dsa') return defer.succeed(keys.getPrivateKeyObject(path)) class SimpleConnection(connection.SSHConnection): def serviceStarted(self): self.openChannel(TrueChannel(2**16, 2**15, self)) self.openChannel(FalseChannel(2**16, 2**15, self)) self.openChannel(CatChannel(2**16, 2**15, self)) class TrueChannel(channel.SSHChannel): name = 'session' # needed for commands def openFailed(self, reason): print 'true failed', reason
def channelOpen(self, ignoredData): self.conn.sendRequest(self, 'exec', common.NS('true')) def request_exit_status(self, data): status = struct.unpack('>L', data)[0] print 'true status was: %s' % status self.loseConnection() class FalseChannel(channel.SSHChannel): name = 'session' def openFailed(self, reason): print 'false failed', reason def channelOpen(self, ignoredData): self.conn.sendRequest(self, 'exec', common.NS('false')) def request_exit_status(self, data): status = struct.unpack('>L', data)[0] print 'false status was: %s' % status self.loseConnection() class CatChannel(channel.SSHChannel): name = 'session' def openFailed(self, reason): print 'echo failed', reason def channelOpen(self, ignoredData): self.data = '' d = self.conn.sendRequest(self, 'exec', common.NS('ls -l'), wantReply = 1) d.addCallback(self._cbRequest)
def _cbRequest(self, ignored): self.write('hello conch\n') self.conn.sendEOF(self) def dataReceived(self, data): self.data=self.data+data
def closed(self): win = Tkinter.Tk(); Label(win,text='CARD UPLOAD UTILITY').pack(side=TOP)
dirfm = Frame(win) dirsa=Scrollbar(dirfm) dirsb= Scrollbar(dirfm) dirsb.pack(side = RIGHT, fill =Y)
dirs = Listbox(dirfm,height = 50, width = 70, yscrollcommand = dirsb.set) textsfile = Entry(dirfm) print 'got data from listing: %s' % self.data recs=self.data.split('\n') '''p4 = re.sub('[]''""['']','','%s'%recs) print 'p4[0]',p4[0]'''
print 'shweta',recs var=StringVar(); for line in recs[1:]:
p4 = re.sub('[]''""['']','','%s'%line) print p4[0]
p2 = string.split(p4) print 'p2',p2 p5 = [] p5 = p5.append(p2[8]) print line w=p4[0] # d r _ dirs.insert(END,line) var=StringVar(); #dirs.bind('<Double-1>', listd)
textsfile.pack( side = BOTTOM) dirsb.config(command=dirs.yview) dirs.pack(side=LEFT,fill=BOTH) c=textsfile.get() dirfm.pack() self.loseConnection() reactor.stop()
protocol.ClientCreator(reactor, SimpleTransport).connectTCP(HOST, 22) reactor.run()
the next problem is to execute a script from the server which accepts inputs from the user and processes it. for eg sh test1.sh On Tue, Sep 30, 2008 at 9:34 PM, Jean-Paul Calderone exarkun@divmod.comwrote:
On Tue, 30 Sep 2008 21:24:03 +0530, shweta mani shweta3230@gmail.com wrote:
[snip]
This isn't the right way to use Tk with Twisted.
See http://twistedmatrix.com/projects/core/documentation/howto/choosing-reactor....
Jean-Paul
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

Would 'ls -l -R' do the trick?
-George
On Thu, 2008-10-02 at 11:40 +0530, shweta mani wrote:
hi all i got the solution of listing all ls -l files in a listbox . Next problem is to use recurrsively ls -l command again if a user selects a directory. Can we use multiple in Linux commands within a program withoy using pipe. i am forwarding the code. i need to use ls -l command dependng on whether he chooses a directory.
..........
d = self.conn.sendRequest(self, 'exec', common.NS('ls -l'),
wantReply = 1)

hi all it really helped.
On Thu, Oct 2, 2008 at 9:28 PM, George Pauly george@ringdevelopment.comwrote:
Would 'ls -l -R' do the trick?
-George
On Thu, 2008-10-02 at 11:40 +0530, shweta mani wrote:
hi all i got the solution of listing all ls -l files in a listbox . Next problem is to use recurrsively ls -l command again if a user selects a directory. Can we use multiple in Linux commands within a program withoy using pipe. i am forwarding the code. i need to use ls -l command dependng on whether he chooses a directory.
..........
d = self.conn.sendRequest(self, 'exec', common.NS('ls -l'),
wantReply = 1)
-- George Pauly Ring Development www.ringdevelopment.com
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python

hi folks i need to execute a shell script file through SSH twisted or paramiko. if it is a normal file then directly with the command sh <filename>.sh it is getting executed. self.conn.sendRequest(self, 'exec', common.NS(sh test1.sh), wantReply = 1) but if a shell scripts asks for user input, this particular way does not wait for user input. it just print the echo part . is there any way so that interactive type of scripts can be executed in Paramiko or Twisted Conch in a windows machine (remote login in a unix machine through Paramiko SSH or Twisted)
i did try with another way in Paramiko which helps in opening a remote window by creating an instance of SSHClient and invoking the method invoke_shell(with some parametes) but remote shell window is not opening. the method specifies starts a terminal shell window in SSH server. i am forwarding the code
On Fri, Oct 3, 2008 at 9:13 PM, shweta mani shweta3230@gmail.com wrote:
hi all it really helped.
On Thu, Oct 2, 2008 at 9:28 PM, George Pauly <george@ringdevelopment.com
wrote:
Would 'ls -l -R' do the trick?
-George
On Thu, 2008-10-02 at 11:40 +0530, shweta mani wrote:
hi all i got the solution of listing all ls -l files in a listbox . Next problem is to use recurrsively ls -l command again if a user selects a directory. Can we use multiple in Linux commands within a program withoy using pipe. i am forwarding the code. i need to use ls -l command dependng on whether he chooses a directory.
..........
d = self.conn.sendRequest(self, 'exec', common.NS('ls -l'),
wantReply = 1)
-- George Pauly Ring Development www.ringdevelopment.com
Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
participants (6)
-
George Pauly
-
Jack Whitham
-
Jean-Paul Calderone
-
Kevin Horn
-
paul
-
shweta mani