Python ssh 작업 진행
paramiko 다운로드
pip install paramiko
paramiko 사용 코드 예제 는 다음 과 같다.
import paramiko
# ssh ,
ssh = paramiko.SSHClient()
# ssh
know_host = paramiko.AutoAddPolicy()
#
ssh.set_missing_host_key_policy(know_host)
#
ssh.connect(
hostname = "10.10.21.177",
port = 22,
username = "root",
password = "123"
)
#
stdin,stdout,stderr = ssh.exec_command("rm -rf /root/Desktop/mv")
#stdin ,
#stdout ,
#stderr ,
print(stdout.read().decode())
ssh.close()
paramiko 결합 다 중 스 레 드 사용 코드 예제 다음 과 같 습 니 다.
import threading
import paramiko
class paramikoThreading(threading.Thread):
def __init__(self,command,host,username,password,port=22):
self.command = command
self.host = host
self.username = username
self.password = password
self.port = port
super(paramikoThreading,self).__init__()
def run(self):
ssh = paramiko.SSHClient()
# ssh
know_host = paramiko.AutoAddPolicy()
#
ssh.set_missing_host_key_policy(know_host)
#
ssh.connect(
hostname=self.host,
port=self.port,
username=self.username,
password=self.password,
)
stdin, stdout, stderr = ssh.exec_command(self.command)
print("*"*60)
print("ip:%s,
command:%s,
"%(self.host,self.command))
print(stdout.read().decode())
print("*"*60)
ssh.close()
if __name__ == '__main__':
from settings import pool # settings.py
command = "ls"
t_pool = []
for host in pool:
t = paramikoThreading(
host=host.get("host","localhost"),
username=host.get("username","root"),
password=host.get("password","123"),
command=command
)
t_pool.append(t)
for t in t_pool:
t.start()
for t in t_pool:
t.join()
settings.py 파일 은 다음 과 같 습 니 다.
pool = [
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
dict(host="10.10.21.177", username="root", password="123"),
]
paramiko 의 Shell 상호작용 연결
paramiko 의 Shell 대화 식 연결 을 사용 하면 로 컬 결 과 를 명령 행 인터페이스 로 되 돌려 주 고 로 컬 코드 의 반환 결과 에서 명령 행 을 직접 조작 할 수 있 습 니 다.
코드 는 다음 과 같 습 니 다:
import paramiko
# ssh
ssh = paramiko.SSHClient()
# ssh
know_host = paramiko.AutoAddPolicy()
#
ssh.set_missing_host_key_policy(know_host)
#
ssh.connect(
hostname = "10.10.21.177",
port = 22,
username = "root",
password = "12345"
)
shell = ssh.invoke_shell()
shell.settimeout(1)
command = input(">>>"+"
")
shell.send(command)
while True:
try:
recv = shell.recv(512).decode()
if recv:
print(recv)
else:
continue
except:
command = input(">>>") + "
"
shell.send(command)
ssh.close() #
파일 업로드 및 다운로드
import paramiko
trans = paramiko.Transport(
sock=("10.10.21.177",22)
)
trans.connect(
username="root",
password="12345"
)
sftp = paramiko.SFTPClient.from_transport(trans)
#
# settings.py, /root/Desktop/settings.py
sftp.put("settings.py","/root/Desktop/settings.py")
#
# /root/Desktop/hh.py hh.py
# sftp.get("/root/Desktop/hh.py","hh.py")
sftp.close()
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
바이너리 파일cat 또는tail, 터미널 디코딩 시 처리 방법cat으로 바이너리 파일을 보려고 할 때 코드가 엉망이 되어 식은땀이 났다. 웹에서 스크롤된 정보의 처리 방법과alias의 설정을 요약합니다. reset 명령을 사용하여 터미널을 재설정합니다.이렇게 하면 고치지 못하...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.