-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsshClient.py
More file actions
executable file
·135 lines (105 loc) · 4.15 KB
/
Copy pathsshClient.py
File metadata and controls
executable file
·135 lines (105 loc) · 4.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python
# Copyright (c) 2009 Twisted Matrix Laboratories.
# See LICENSE for details.
from twisted.conch.ssh import transport, userauth, connection, common, keys, channel
from twisted.internet import defer, protocol, reactor
from twisted.internet import reactor
from twisted.python import log
import struct, sys, getpass, os
#USER = 'cesar' # replace this with a valid username
HOST = 'localhost' # and a valid host
debug = 1
class SimpleTransport(transport.SSHClientTransport):
'''
SSHClientTransport handles the negotiation of encryption
and the verification of keys for you
'''
def verifyHostKey(self, hostKey, fingerprint):
'''
This method is called with two strings:
the public key sent by the server and its fingerprint
'''
if debug: print 'At verifyHostKey'
print 'host key fingerprint: %s' % fingerprint
return defer.succeed(1)
def connectionSecure(self):
'''
'''
if debug: print 'At connectionSecute'
self.requestService(
SimpleUserAuth(os.getlogin(),
SimpleConnection()))
class SimpleUserAuth(userauth.SSHUserAuthClient):
def getPassword(self):
if debug: print 'At getPassword'
return defer.succeed(getpass.getpass("%s@%s's password: " % (os.getlogin(), HOST)))
def getGenericAnswers(self, name, instruction, questions):
if debug: print 'At getGenericAnswers'
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):
if debug: print 'At getPublicKey'
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):
if debug: print 'At getPrivateKey'
path = os.path.expanduser('~/.ssh/id_dsa')
return defer.succeed(keys.getPrivateKeyObject(path))
class SimpleConnection(connection.SSHConnection):
def serviceStarted(self):
if debug: print 'At serviceStarted'
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('cat'), 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 += data
def closed(self):
print 'got data from cat: %s' % repr(self.data)
self.loseConnection()
reactor.stop()
protocol.ClientCreator(reactor, SimpleTransport).connectTCP(HOST, 22)
reactor.run()