-
[ 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'}
'AWS Infra' 카테고리의 다른 글
[ AWS ] Amazon Linux 2에 python 3.8 설치 (0) 2022.02.02 [ AWS ] EC2 Linux root 로그인 허용 (0) 2022.02.02 [ AWS ] S3 Bucket policy (0) 2021.12.03 [ AWS ] IAM Policy 특정IP에서 접근시에만 권한 허용 (0) 2021.11.30 [ AWS ] S3 용량과 객체수 모니터링 (s3api와 python) (0) 2021.11.21 AWS Linux UTC 시간 한국시간으로 변경 (0) 2021.11.13 [ AWS CLI ] S3 명령어 (0) 2021.11.04 [ AWS ] IAM Policy 특정 S3만 읽기 / 쓰기 권한 부여 (0) 2021.11.04