paramiko远程登录与sftp文件传输

(一)基于用户名密码的远程登录加命令执行


  
  
  1. import paramiko
  2. #创建ssh链接实例
  3. ssh = paramiko.SSHClient()
  4. #创建白名单,默认不询问, 即允许连接不在known_hosts文件中的主机
  5. known_hosts = paramiko.AutoAddPolicy()
  6. ssh.set_missing_host_key_policy(known_hosts)
  7. #链接服务器
  8. ssh.connect(
  9. hostname = "10.10.65.65", # inux主机
  10. port = 22, #端口,默认22
  11. username = "chen", #用户名 ,也可登录到root
  12. password = "123" #密码
  13. )
  14. #执行命令
  15. stdin,stdout,stderr = ssh.exec_command( "ls /")
  16. #ssh.exec_command() 返回三个类文件对象
  17. #stdin是远程主机接收我们写入到数据,stdout是远程主机向我们输出信息
  18. #stdin 是写权限的文件对象,标准输入
  19. #stdout 是读权限的文件对象,标准输出
  20. #stderr 是读权限的文件对象,标准错误,特殊的输出
  21. #查看执行结果
  22. result = stdout.read().decode() # 得到远程主机的输出
  23. print(result)
  24. #关闭链接,释放内存
  25. ssh.close()

(二)基于多线程的远程登录


  
  
  1. import threading
  2. def Exec(command,ip,user,passwd,port = 22):
  3. """
  4. 编写执行命令的模块
  5. """
  6. ssh = paramiko.SSHClient()
  7. know_hosts = paramiko.AutoAddPolicy()
  8. ssh.set_missing_host_key_policy(know_hosts)
  9. ssh.connect(
  10. hostname = ip,
  11. port = port,
  12. username = user,
  13. password = passwd
  14. )
  15. stdin, stdout, stderr = ssh.exec_command(command)
  16. result = stdout.read().decode()
  17. print( "+++++++++++++++++%s+++++++++++++++++++++" % ip)
  18. print(result)
  19. print( "+++++++++++++++++%s+++++++++++++++++++++" % ip)
  20. ssh.close()
  21. def main(command,pool):
  22. lenth = len(pool)
  23. thread_pool = []
  24. for i in range(lenth):
  25. t = threading.Thread(target = Exec,args = (command,)+pool[i])
  26. thread_pool.append(t)
  27. for t in thread_pool:
  28. t.start()
  29. for t in thread_pool:
  30. t.join() # join见讲解
  31. #https://blog.csdn.net/sinat_38068807/article/details/88742964
  32. if __name__ == "__main__":
  33. command = input( ">>>")
  34. pool = [
  35. ( "10.10.65.95", "root", "123", 22),
  36. ( "10.10.65.71", "root", "123",)
  37. ]
  38. main(command,pool)

(三)基于面向对象的多线程

Pyhton 多线程模块threading通过Thread类构建新的线程

Thread类当做提供了run方法用于子类继承重写

当run方法被修改,在线程调用start的时候,会调用run的功能作为线程的功能。


  
  
  1. import random
  2. import threading
  3. from time import sleep
  4. class ExecThread(threading.Thread):
  5. def __init__(self):
  6. super(ExecThread,self).__init__()
  7. def run(self):
  8. time = random.randint( 1, 3)
  9. sleep(time)
  10. print( "hello world,I will sleep %s second"%time)
  11. pool = []
  12. for i in range( 10):
  13. t = ExecThread()
  14. pool.append(t)
  15. for i in pool:
  16. i.start()
  17. for i in pool:
  18. i.join()

(四)多线程面向对象


  
  
  1. import threading
  2. class ExecThread(threading.Thread):
  3. def __init__(self,command,ip,user,passwd,port = 22):
  4. self.command = command
  5. self.ip = ip
  6. self.user = user
  7. self.password = passwd
  8. self.port = port
  9. super(ExecThread,self).__init__()
  10. def run(self):
  11. ssh = paramiko.SSHClient()
  12. know_hosts = paramiko.AutoAddPolicy()
  13. ssh.set_missing_host_key_policy(know_hosts)
  14. ssh.connect(
  15. hostname = self.ip,
  16. port = self.port,
  17. username = self.user,
  18. password = self.password
  19. )
  20. stdin, stdout, stderr = ssh.exec_command(self.command)
  21. result = stdout.read().decode()
  22. print( "+++++++++++++++++%s+++++++++++++++++++++" % self.ip)
  23. print(result)
  24. print( "+++++++++++++++++%s+++++++++++++++++++++" % self.ip)
  25. ssh.close()
  26. def main(command,pool):
  27. lenth = len(pool)
  28. thread_pool = []
  29. for i in range(lenth):
  30. ip = pool[i][ 0]
  31. user = pool[i][ 1]
  32. passwd = pool[i][ 2]
  33. if len(pool[i]) > 3:
  34. port = pool[i][ 0]
  35. t = ExecThread(command, ip, user, passwd, port)
  36. else:
  37. t = ExecThread(command, ip, user, passwd)
  38. thread_pool.append(t)
  39. for t in thread_pool:
  40. t.start()
  41. for t in thread_pool:
  42. t.join()
  43. if __name__ == "__main__":
  44. command = input( ">>>")
  45. pool = [
  46. ( "10.10.65.95", "root", "123"),
  47. ( "10.10.65.71", "root", "123")
  48. ]
  49. main(command,pool)

(五)交互式编程


  
  
  1. import paramiko
  2. ssh = paramiko.SSHClient()
  3. know_hosts = paramiko.AutoAddPolicy()
  4. ssh.set_missing_host_key_policy(know_hosts)
  5. ssh.connect(
  6. hostname = "10.10.65.95", #主机
  7. port = 22, #端口,默认22
  8. username = "root", #用户名
  9. password = "123" #密码
  10. )
  11. shell = ssh.invoke_shell() #实例化一个terminal
  12. shell.settimeout( 1) #命令执行的等待时间
  13. command = input( ">>>")+ "\n" #\n回车
  14. shell.send(command) #发送命令
  15. while True:
  16. try:
  17. recv = shell.recv( 512).decode() #接受返回
  18. if recv:
  19. print(recv)
  20. else:
  21. continue
  22. except:
  23. command = input( ">>>")+ "\n"
  24. shell.send(command)
  25. ssh.close()

(六)sftp文件上传下载


  
  
  1. import paramiko
  2. trans = paramiko.Transport(
  3. sock=(
  4. "10.10.65.65", 22
  5. )
  6. )
  7. trans.connect(
  8. username= "root",
  9. password= "123"
  10. )
  11. sftp = paramiko.SFTPClient.from_transport(trans)
  12. sftp.put( "ssh2.py", "/opt/test.py") # 将本机ssh2.py放到远程机的/opt下并命名test.py
  13. # sftp.get("/opt/main.py", "hello.py")
  14. sftp.close()

发布了4 篇原创文章 · 获赞 4 · 访问量 2226

(一)基于用户名密码的远程登录加命令执行

猜你喜欢

转载自blog.csdn.net/bluewhu/article/details/104087093