一、简单概要
这是我在生产环境用的一个脚本,根据实际运维情况编写,说白了就是人比较懒,不想打杂。哈哈!不明白的同学可以@我哦!,感谢!
二、代码
#!/usr/bin/env python
from pexpect import pxssh
import os
try:
for i in range(64,65): #在这个位置定义起始和结束的数字用于IP的主机号
s = pxssh.pxssh()
ipaddr = '192.168.1.%s' % i #生成一个完整的IP地址
os.environ['ip']=str(ipaddr) #变量呼唤,python变量可以在shell里拿
print 'ssh connection host:%s' % ipaddr
s.login(ipaddr,'root','123456') #登录主机(ip,用户,密码)
print 'login ok!' #提示登录ok而已,仅此而已
print 'Create (/etc/ceph/) directory...' #登录进去就得干活了,比如创建目录
s.sendline('mkdir -p /etc/ceph')
print 'create ok!'
print 'Issued by the configuration file...'
os.system('sshpass -p 123456 scp /etc/ceph/* root@$ip:/etc/ceph') #这个文件是下发文件,这里跟pxssh没关系
print 'file issued ok!'
print 'Issued by the script...'
s.sendline('mkdir -p /openstack') #这里也是在远程主机执行命令,其他的都类似
os.system('sshpass -p 123456 scp /root/osd.sh root@$ip:/openstack')
os.system('sshpass -p 123456 scp /root/com.txt root@$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)
转载于:https://blog.51cto.com/freshair/1866032