Python ssh 작업 진행

python 에서 ssh 작업 을 하려 면 paramiko 모듈 을 사용 해 야 합 니 다.paramiko 는 python 의 3 자 ssh 모듈 로 socket 을 통 해 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()

좋은 웹페이지 즐겨찾기