A simple summary
This is my production environment with a script, according to the actual operation of the situation, the white is that people are lazy, do not want to do chores. Ha ha! Do not understand the classmate can @ me Oh! Thank!
Second, the Code
#!/usr/bin/env pythonfrom pexpect import pxsshimport ostry: s = pxssh.pxssh () for i in range (64,65): #在这个位置定义起始和结束的数字用于IP的主机号 ipaddr = ' 192.168.1.%s ' % i #生成一个完整的IP地址 os.environ[' IP ']=str (ipaddr) # Variable call, Python variable can be taken in Shell print ' ssh connection host:%s ' % Ipaddr s.login (ipaddr, ' root ', ' 123456 ') # Login host (IP, user, password) print ' login ok! ' #提示登录ok而已, that's all print ' create (/etc/ceph/) Directory ... ' #登录进去就得干活了, such as creating a directory s.sendline (' mkdir -p / Etc/ceph ') print ' create ok! ' print ' Issued by the configuration file ... ' os.system ( ' sshpass -p 123456 scp /etc/ceph/* [email protected] $ip:/etc/ceph ') # This file is issued file, here with Pxssh no matter print ' file issued ok! ' print ' Issued by the script ' s.sendline ( ' Mkdir -p /openstack ') #这里也是在远程主机执行命令, the others are similar to os.system (' Sshpass -p 123456 scp /root/osd.sh [email protected] $ip:/openstack ') Os.system (' sshpass -p 123456 scp /root/com.txt [email protected] $ip:/openstack ') print ' script issued ok! ' print ' Run the script ' s.sendline (' sh /openstack/osd.sh ') print ' script run ok! ' s.prompt () print ' host:%s completes, exit The ssh ' % ipaddr s.logout () print ' exit ok! ' Except pxssh. exceptionpxssh, e: print "Pxssh failed on login" print str (e)
This article is from the "it cock thread aggregation q group 223843163" blog, please be sure to keep this source http://trtan.blog.51cto.com/8272891/1866032
Python's pxssh implementation of SSH login batch "handyman"