shell 的一个小 脚本模板实现hosts添加

工作中需要指定添加hosts信息到大数据平台集群之中,为方便添加,用shell脚本实现交互添加

测试脚本:

1.addhost.ex (实现分发功能)

#!/usr/bin/expect -f
set ip [lindex $argv 0]
set user [lindex $argv 1]
set passwd [lindex $argv 2]
set username [lindex $argv 3]
set userpasswd [lindex $argv 4]
set homepath [lindex $argv 5]
send_user "\n===============================$ip=========================================\n"
set timeout 4
spawn ssh $user@$ip

set timeout 4
expect {
"(yes/no*" {send "yes\r";exp_continue}
"*assword:" {send "$passwd\r"}
}

set timeout 4
expect "$"
send "sudo su - \r"

set timeout 4
expect "*assword"
send "$passwd\r"

set timeout 4
expect "#"
send "

\r"

expect eof

2.addnode.py (生成一段字符串)

import re
port=9101

with open('iplist','r') as op:
  for i in op.readlines():
    regex = re.compile('\s+')
    ip=regex.split(i)[0]
    hostname=regex.split(i.replace('\n',''))[1]
#    print ip,hostname
    print "echo -e '"+str(ip)+" \\"+"\\t"+str(hostname)+"' >> /etc/hosts"

3.addhost.sh (命令执行)

#!/bin/bash

python addnode.py > tmphost

sed -i '28r tmphost' addhost.ex
cat addhost.ex
#for ip in `cat ip`;do expect addhost.ex ${ip}  用户  密码;done
sleep 5
###sed -i '29,$[28+`cat tmphost|wc -l`d]' addhost.ex
row=$[28+`cat tmphost|wc -l`]
echo $row
sed -i "29,$row"d addhost.ex

cat addhost.ex

 

具体脚本通过修改ip 与iplist 执行addhost.sh就搞定添加到指定IP的/etc/hosts中:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值