Python paramiko 모듈 의 설치 및 사용
1.paramiko 설치
pip3 install paramiko
2.사용자 이름 암호 방식 으로 원 격 으로 명령 을 수행 합 니 다.
import paramiko
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # host key
ssh.connect('127.0.0.1', 22, 'username', 'password') # IP、 、 、
stdin, stdout, stderr = ssh.exec_command('df -h') #
for line in stdout:
print(line)
ssh.close() # ssh
3.사용자 이름 비밀번호 로 파일 업로드 또는 다운로드
import paramiko
t = paramiko.Transport('127.0.0.1', 22)
t.connect(username='username', password='password')
sftp = paramiko.SFTPClient.from_transport(t)
sftp.put('local_file', 'remote_folder')
t.close()
import paramiko
t = paramiko.Transport('127.0.0.1', 22)
t.connect(username='username', password='password')
sftp = paramiko.SFTPClient.from_transport(t)
sftp.get('remote_file', 'local_folder')
t.close()
4.ssh key 방식 으로 원 격 으로 명령 을 실행 합 니 다.(원 격 호스트 가 공개 키 를 받 아 들 였 을 때)
import paramiko
private_key_path = '/home/xxx/.ssh/id_rsa'
key = paramiko.RSAKey.from_private_key_file(private_key_path)
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect('127.0.0.1', 22, username='username', pkey=key)
stdin, stdout, stderr = ssh.exec_command('df')
print(stdout.read())
ssh.close()
5.scp 방식 으로 원 격 으로 명령 을 수행 합 니 다.
import paramiko
scp = paramiko.Transport(('127.0.0.1', 22))
scp.connect(username='username', password='password')
channel = scp.open_session()
channel.exec_command('touch hello/test.txt')
channel.close()
scp.close()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
python은paramiko를 이용하여 서버 명령을 실행하는 방법말이 많지 않으면 바로 코드를 찍는다. 봉인 연결 서버에서 명령 실행 서버에 파일 보내기 능력에 한계가 있으니, 만약 부적당하면 메시지를 남겨 시정해 주십시오 이상은python이paramiko를 어떻게 이용하여 서버...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.