加入收藏 | 设为首页 | 会员中心 | 我要投稿 厦门网 (https://www.xiamenwang.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 建站 > 正文

详解Python远程控制模块:Paramiko概念、方法及七大案例

发布时间:2019-10-23 22:05:19 所属栏目:建站 来源:波波说运维
导读:概述 ssh是一个协议,OpenSSH是其中一个开源实现,paramiko是Python的一个库,实现了SSHv2协议(底层使用cryptography)。 有了Paramiko以后,我们就可以在Python代码中直接使用SSH协议对远程服务器执行操作,而不是通过ssh命令对远程服务器进行操作。今天主

4. 批量远程密码连接

  1. from paramiko.ssh_exception import NoValidConnectionsError 
  2. from paramiko.ssh_exception import AuthenticationException 
  3. def connect(cmd,hostname,port=22,username='root',passwd='westos'): 
  4.  import paramiko 
  5.  ##1.创建一个ssh对象 
  6.  client = paramiko.SSHClient() 
  7.  #2.解决问题:如果之前没有,连接过的ip,会出现选择yes或者no的操作, 
  8.  ##自动选择yes 
  9.  client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
  10.  #3.连接服务器 
  11.  try: 
  12.  client.connect(hostnamehostname=hostname, 
  13.  portport=port, 
  14.  usernameusername=username, 
  15.  password=passwd) 
  16.  print('正在连接主机%s......'%(hostname)) 
  17.  except NoValidConnectionsError as e: ###用户不存在时的报错 
  18.  print("连接失败") 
  19.  except AuthenticationException as t: ##密码错误的报错 
  20.  print('密码错误') 
  21.  else: 
  22.  #4.执行操作 
  23.  stdin,stdout, stderr = client.exec_command(cmd) 
  24.  #5.获取命令执行的结果 
  25.  result=stdout.read().decode('utf-8') 
  26.  print(result) 
  27.  #6.关闭连接 
  28.  finally: 
  29.  client.close() 
  30. with open('ip.txt') as f: #ip.txt为本地局域网内的一些用户信息 
  31.  for line in f: 
  32.  lineline = line.strip() ##去掉换行符 
  33.  hostname,port,username,passwd= line.split(':') 
  34.  print(hostname.center(50,'*')) 
  35.  connect('uname', hostname, port,username,passwd) 

5. paramiko基于公钥密钥连接

  1. import paramiko 
  2. from paramiko.ssh_exception import NoValidConnectionsError, AuthenticationException 
  3. def connect(cmd, hostname, port=22, user='root'): 
  4.  client = paramiko.SSHClient()  
  5.  private_key = paramiko.RSAKey.from_private_key_file('id_rsa') 
  6.  ###id_rsa为本地局域网密钥文件 
  7.  client.set_missing_host_key_policy(paramiko.AutoAddPolicy()) 
  8.  try: 
  9.  client.connect(hostnamehostname=hostname, 
  10.  portport=port, 
  11.  userusername=user, 
  12.  pkey=private_key 
  13.  ) 
  14.  stdin, stdout, stderr = client.exec_command(cmd) 
  15.  except NoValidConnectionsError as e: 
  16.  print("连接失败") 
  17.  except AuthenticationException as e: 
  18.  print("密码错误") 
  19.  else: 
  20.  result = stdout.read().decode('utf-8') 
  21.  print(result) 
  22.  finally: 
  23.  client.close() 
  24. for count in range(254): 
  25.  host = '172.25.254.%s' %(count+1) 
  26.  print(host.center(50, '*')) 
  27.  connect('uname', host) 

6. 基于密钥的上传和下载

  1. import paramiko 
  2. private_key = paramiko.RSAKey.from_private_key_file('id_rsa') 
  3. tran = paramiko.Transport('172.25.254.31',22) 
  4. tran.connect(username='root',password='westos') 
  5. #获取SFTP实例 
  6. sftp = paramiko.SFTPClient.from_transport(tran) 
  7. remotepath='/home/kiosk/Desktop/fish8' 
  8. localpath='/home/kiosk/Desktop/fish1' 
  9. sftp.put(localpath,remotepath) 
  10. sftp.get(remotepath, localpath) 

(编辑:厦门网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读