
원격 대상지의 공개 키를 가지고 있지 않을 시 자동으로 등록하게 하며 SSH 접속을 시도합니다.
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect('127.0.0.1', username='user', password='user', timeout=5)
ssh_client.close()
except paramiko.BadHostKeyException: print("Invalid Host Key")
except paramiko.AuthenticationException: print("Invalid Account")
except paramiko.SSHException as e: print("SSH Error : {}".format(e))
except Exception as e: print("General Error : {}".format(e))
exec_command 함수를 사용하여 명령어를 실행시킬 수 있으며, 명령어의 출력인 2번째 반환 값(stdout)을 출력해 봅니다.
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect('127.0.0.1', username='user', password='user', timeout=5)
stdin, stdout, stderr = ssh_client.exec_command('ip addr')
for out in stdout.readlines(): print(out.rstrip())
ssh_client.close()
except paramiko.BadHostKeyException: print("Invalid Host Key")
except paramiko.AuthenticationException: print("Invalid Account")
except paramiko.SSHException as e: print("SSH Error : {}".format(e))
except Exception as e: print("General Error : {}".format(e))
백그라운드로 명령어를 실행시킬 필요가 있을 시, 다음과 같은 작업을 통해 실행시킵니다.
import paramiko
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect('127.0.0.1', username='user', password='user', timeout=5)
channel = ssh_client.get_transport().open_session()
channel.exec_command('ip addr')
channel.close()
ssh_client.close()
except paramiko.BadHostKeyException: print("Invalid Host Key")
except paramiko.AuthenticationException: print("Invalid Account")
except paramiko.SSHException as e: print("SSH Error : {}".format(e))
except Exception as e: print("General Error : {}".format(e))
SSH 채널을 통해서 현재 디렉터리의 파일을 test라는 디렉터리로 전송해 봅니다.
import paramiko
from scp import SCPClient
import os
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect('127.0.0.1', username='user', password='user', timeout=5)
with SCPClient(ssh_client.get_transport()) as scp:
scp.put(os.path.join(".", "hello.txt"), os.path.join("test", "world.txt"), preserve_times=True)
ssh_client.close()
except paramiko.BadHostKeyException: print("Invalid Host Key")
except paramiko.AuthenticationException: print("Invalid Account")
except paramiko.SSHException as e: print("SSH Error : {}".format(e))
except Exception as e: print("General Error : {}".format(e))
스크립트를 실행 후 파일을 확인해 봅니다.
(venv) $ ls -al test/world.txt
-rw-rw-r-- 1 user user 4 Oct 5 09:30 test/world.txt
SSH 채널을 통해서 test라는 디렉터리의 파일을 현재 디렉터리로 전송해 봅니다.
import paramiko
from scp import SCPClient
import os
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
ssh_client.connect('127.0.0.1', username='user', password='user', timeout=5)
with SCPClient(ssh_client.get_transport()) as scp:
scp.get(os.path.join("test", "world.txt"), os.path.join(".", "world.txt"), preserve_times=True)
ssh_client.close()
except paramiko.BadHostKeyException: print("Invalid Host Key")
except paramiko.AuthenticationException: print("Invalid Account")
except paramiko.SSHException as e: print("SSH Error : {}".format(e))
except Exception as e: print("General Error : {}".format(e))
스크립트를 실행 후 파일을 확인해 봅니다.
(venv) $ ls -al ./world.txt
-rw-rw-r-- 1 user user 4 Oct 5 09:30 ./world.txt