Python_Study

python pexpect, paramiko 라이브러리

뭉탁거림 2016. 10. 7. 19:35

python pexpect, paramiko 라이브러리를 이용한 원격지 서버의 명령어 수행


1) pip 설치

2) pip install pexepect, paramiko 


#!/usr/bin/python


####################################


import pexpect

import paramiko

import time

import sys


def ssh_conn(host,passwd,command):

result=""

ssh = paramiko.SSHClient()  

ssh.load_system_host_keys()

ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())

ssh.connect(host,username='root',password=passwd)

stdin, stdout, stderr = ssh.exec_command(command)

for i in stdout.readlines():

result+=i

print result


ssh.close()


return result


if __name__ == "__main__":

try:

num = 1

mgmt = ""

networks = ["172.16.193.", "172.16.194.", "172.16.196.", "172.16.197."]

for i in networks:

print i + "X MGMT Network POD " + "\n"

for j in range(1,9):

mgmt = i + str(j)

ssh_conn(mgmt, "password", sys.argv[1])

time.sleep(1)

except Exception, e:

print str(e)