# -*- coding: utf-8 -*-#!/usr/bin/env pythonimport pexpect,os #导入需要用到模块def ssh_cmd (ip, shell_cmd):p asswd= ' 1qaz#edc ' print ' Host: %s is connected... ' % ipchild = pexpect.spawn (' ssh [email protected]%s ') % (IP)) fout = file (' Log.txt ', ' a ') child.logfile = fouttry:i = Child.expect (' Password: ') if i == 0:child.sendline (passwd) elif i == 1: Child.sendline (' yes\n ') child.expect (' password: ') child.sendline (passwd) print ' Host:%s login ok! ' % ipchild.expect (' # ') child.sendline (shell_cmd) #执行传过来的shell命令child. Expect (' # ') print ' host:%s command execution ok! ' % ipexcept pexpect. eof:print "command run ok!" Child.close () Except pexpect. timeout:print "Connect timeout ..." Child.close () #前面的ssh_cmd () action to establish an SSH connectionFor i in range (165,167):ipaddr = ' 192.168.122.%s ' % issh_cmd (ipaddr, ' mkdir -p /etc/ceph ') #ssh连接上远程主机后, create a directory in the remote host os.environ[' IP ']=str (ipaddr) #python变量和shell变量互用os. System (' sshpass -p 1qaz#edc scp /home/testfile*. Conf [email protected] $ip:/etc/ceph ') #文件下发
This article is from "My Ops and my original" blog, so be sure to keep this source http://trtan.blog.51cto.com/8272891/1863197
Python file batch issued with SCP implementation