Bonjour, j'essaye de pouvoir communiquer avec un serveur grâce au module paramiko. voici les codes correspondants au serveur et au client:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import socket
import paramiko
import threading
import sys
 
host_key = paramiko.RSAKey(filename='C:/Users/axel.ch/Desktop/paramiko-master/demos/test_rsa.key')
 
class Server (paramiko.ServerInterface):
   def _init_(self):
       self.event = threading.Event()
   def check_channel_request(self, kind, chanid):
       if kind == 'session':
           return paramiko.OPEN_SUCCEEDED
       return paramiko.OPEN_FAILED_ADMINISTRATIVELY_PROHIBITED
   def check_auth_password(self, username, password):
       if (username == 'root') and (password == 'toor'):
           return paramiko.AUTH_SUCCESSFUL
       return paramiko.AUTH_FAILED
 
try:
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    sock.bind(('192.168.1.114', 22))
    sock.listen(100)
    print '[+] Listening for connection ...'
    client, addr = sock.accept()
except Exception, e:
    print '[-] Listen/bind/accept failed: ' + str(e)
    sys.exit(1)
print '[+] Got a connection!'
 
try:
    t = paramiko.Transport(client)
    try:
        t.load_server_moduli()
    except:
        print '[-] (Failed to load moduli -- gex will be unsupported.)'
        raise
    t.add_server_key(host_key)
    server = Server()
    try:
        t.start_server(server=server)
    except paramiko.SSHException, x:
        print '[-] SSH negotiation failed.'
 
    chan = t.accept(20)
    print '[+] Authenticated!'
    print chan.recv(1024)
    chan.send('Yeah i can see this')
    while True:
       command= raw_input("Enter command: ").strip('\n')
       chan.send(command)
       print chan.recv(1024) + '\n'
 
except Exception, e:
    print '[-] Caught exception: ' + str(e) + ': ' + str(e)
    try:
        t.close()
    except:
        pass
    sys.exit(1)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
import paramiko
import threading
import subprocess
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect('192.168.1.114', username='root', password='toor')
chan = client.get_transport().open_session()
chan.send('Hey i am connected :) ')
print chan.recv(1024)
 
def sftp(local_path,name):
    try:
        transport = paramiko.Transport(('192.168.1.114', 22))
        transport.connect(username = 'root', password = 'toor')
        sftp = paramiko.SFTPClient.from_transport(transport)
        sftp.put(local_path, 'C:/Users/axel.ch/Desktop'+name)
        sftp.close()
        transport.close()
        return '[+] Done'
    except Exception,e:
        return str(e)
 
 
while True:
 
    command = chan.recv(1024)
    if 'grab' in command:
        grab,name,path = command.split('*')
        chan.send( sftp(path,name) )
 
client.close
Le problème arrive lorsque je souhaite transférer un fichier, (Tous mes tests sont en local) j'obtiens ce message d'erreur:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
[+] Listening for connection ...
[+] Got a connection!
[+] Authenticated!
Hey i am connected :) 
Enter command: grab*nb*C:/Users/axel.ch/Downloads/nb.txt
Error reading SSH protocol banner
J'ai recherché sur internet, et j'ai vu à certains endroits que le problème venait d'une mauvaise bannière SSH ne commencant pas par 'SSH-'. Cependant je ne sais pas comment la modifier.
Merci de votre aide