Newer
Older
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
import socket
from cryptography.hazmat.primitives import serialization
from cryptography.hazmat.backends import default_backend
import sys
import os
# ajouter le chemin vers le répertoire libs à PYTHONPATH
libs_path = os.path.join(os.getcwd(), 'libs')
if libs_path not in sys.path:
sys.path.append(libs_path)
# maintenant, vous pouvez importer votre bibliothèque
import paramiko
from paramiko.auth_handler import AuthHandler
import logging
#
# logging.basicConfig(level=logging.DEBUG)
# logging.basicConfig(level=logging.ERROR)
algorithm = "rsa-sha2-512"
# Variables pour la connexion SSH
hostname = '172.20.10.4'
username = 'parallels'
port = 22
# Variables pour la connexion au deuxième dispositif
signer_hostname = 'localhost'
signer_port = 12340
# Variables pour la clé publique
public_key_file = 'ssh-keys/test.pub'
# Lire la clé publique au format OpenSSH
with open(public_key_file, 'rb') as key_file:
public_key = serialization.load_ssh_public_key(key_file.read(), backend=default_backend())
public_key_pem = public_key.public_bytes(
encoding=serialization.Encoding.OpenSSH,
format=serialization.PublicFormat.OpenSSH
)
print("Contenu de la clé publique :\n", public_key_pem.decode())
print("Clé publique chargée :", public_key)
# Créer une nouvelle instance de transport
transport = paramiko.Transport((hostname, port))
# Créer une nouvelle instance de client de signature
clePriv = paramiko.RSAKey.from_private_key_file('ssh-keys/test')
# Essayer d'ouvrir la session SSH
def handler(title, instructions, prompt_list):
print("*" * 80)
print("Handeling interactive authentication")
print("*" * 80)
signer_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
signer_client.connect((signer_hostname, signer_port))
resp = []
print("Prompt list :", prompt_list)
print("Instructions :", instructions)
print("Title :", title)
for pr in prompt_list:
print(pr)
if pr[0].startswith('Password'):
# Transmettre le défi au deuxième dispositif
print("Envoi du défi : ", pr[0].encode())
signer_client.sendall(pr[0].encode())
# Attendre la réponse du deuxième dispositif
signed_challenge = signer_client.recv(1024)
print("Défi signé :", signed_challenge)
# resp.append("Jdlm1209")
resp.append(signed_challenge)
else:
resp.append('')
return resp
print("Connexion au serveur SSH...")
transport.connect()
print("Connexion réussie !")
print("Authentification for username {}...".format(username))
# transport.auth_publickey(username, public_key, handler)
try:
transport.auth_publickey(username, public_key)
except paramiko.ssh_exception.AuthenticationException:
print("Échec de l'authentification")
else:
print("Authentification réussie !")