1. public key 생성

from paramiko  import ecdsakey

k = ecdsakey.ECDSAKey.generate()   # key 생성

public_key = k.get_base64()

with open("key.pub", "w") as fp:

     fp.write(public_key)

 

2. private key 생성

import os

with open("key.priv", "w") as fp:

     os.chmod(0o600, "key.priv")

     k.write_private_key(fp)

 

3. example

import paramiko, time

connection = paramiko.SSHClient()

connection.set_missing_host_key_policy(paramiko.AutoAddPolicy())

connection.connect('10.10.10.10', username='test', password='test', look_for_keys=False, allow_agent=False)

new_conn = connection.invoke_shell()

output = new_conn.recv(5000)

print(output)

new_conn.send("ls")

time.sleep(3)

output = new_conn.recv(5000)

print(output)

new_conn.close()

 

 

import paramiko

key = paramiko.RSAKey.from_private_key_file('/home/test/.ssh/id_rsa')

client = paramiko.SSHClient()

client.set_missing_host_key_policy(paramiko.AutoAddPolicy())

client.connect('10.10.10.10', username='test', pkey=key)

stdin, stdout, stderr = client.exec_command('ls -al')

stdout.read()

stdin, stdout, stderr = client.exec_command('pwd')

stdout.read()

client.close()

 

 

 

'Language > python' 카테고리의 다른 글

python netmiko  (0) 2019.09.12
pip local storage  (0) 2019.09.10
python code test  (0) 2019.09.04
python 데이터 분석툴  (0) 2019.06.21
grpc with python  (0) 2019.05.09

+ Recent posts