ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [ Python ] paramiko를 이용한 pem-key SSH 접속 (자동화 작업)
    AWS Infra 2021. 11. 30. 18:18

     

    key 인증을 이용하여 접속시 parakimo 사용

     

    import paramiko
    
    key = paramiko.RSAKey.from_private_key_file("pemfilepath")
    conn = paramiko.SSHClient()
    conn.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    conn.connect(hostname = " ", username = "", pkey = key )
    
    stdin, stdout, stderr = conn.exec_command('echo "hello"')
    stdin.close()
    
    for line in stdout.read().splitlines():
        print(line.decode())
                    
    conn.close()
    
    --------------------------------------------------------------
    
    hello

     

     

     

     

    예제로 yum을 이용하여 zabbix-agent를 업데이트 해본다.

     

    stdin, stdout, stderr = conn.exec_command('sudo yum update zabbix-agent.x86_64 -y')
    stdin.close()
    
    for line in stdout.read().splitlines():
        print(line.decode())
        
       
      -------------------------------------------------------------------------------------------
    Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
    Resolving Dependencies
    --> Running transaction check
    ---> Package zabbix-agent.x86_64 0:6.2.4-release1.el7 will be updated
    ---> Package zabbix-agent.x86_64 0:6.2.9-release1.el7 will be an update
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    ================================================================================
     Package            Arch         Version                     Repository    Size
    ================================================================================
    Updating:
     zabbix-agent       x86_64       6.2.9-release1.el7          zabbix       547 k
    
    Transaction Summary
    ================================================================================
    Upgrade  1 Package
    
    Total download size: 547 k
    Downloading packages:
    Delta RPMs disabled because /usr/bin/applydeltarpm not installed.
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Updating   : zabbix-agent-6.2.9-release1.el7.x86_64                       1/2 
      Cleanup    : zabbix-agent-6.2.4-release1.el7.x86_64                       2/2 
      Verifying  : zabbix-agent-6.2.9-release1.el7.x86_64                       1/2 
      Verifying  : zabbix-agent-6.2.4-release1.el7.x86_64                       2/2 
    
    Updated:
      zabbix-agent.x86_64 0:6.2.9-release1.el7                                      
    
    Complete!

     

     

    $ sudo yum list zabbix-agent
    Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
    Installed Packages
    zabbix-agent.x86_64      6.2.9-release1.el7    @zabbix

     

    정상적으로 업데이트가 되었다.

     

     

     

     

    다수의 ec2에 일괄작업을 위해서 아래와 같이 접속정보를 딕셔너리로 저장한 후 loop를 활용하면 될 것이다.

     

    ec2s = {'host':'keyfile'}

     

     

Designed by Tistory.