lqc_shell批量操作linux主机
2016-11-18 17:03
351 查看
shell脚本批量操作linux主机:
一.环境介绍:
开启192.168.100.150-152(ctos1-3)和192.168.100.100(vsftpd)。
1.ssh登录:在192.168.100.100上操作
1)密码登录192.168.100.150:
ssh root@192.168.100.150 ##输入root密码123123
2)配置ssh密钥对登192.168.100.150:
ssh-keygen ##创建密钥对,提示直接回车
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.100.150 ##输入root密码上传公钥
ssh root@192.168.100.150 ##登录查看是否需要密码
ssh root@192.168.100.150 "ifconfig eth0" ##在192.168.100.150上执行命令“ifconfig eth0”查看ip地址
3)删除相关信息,为ssh脚本测试做准备:
192.168.100.100上:rm -rf /root/.ssh/known_hosts
192.168.100.150上:rm -rf /root/.ssh
二.编写脚本批量操作主机名:
1.需求描述:
1)修改主机名:
将192.168.100.150的主机名修改为“www.linuxfan.cn”
将192.168.100.151的主机名修改为“ca.linuxfan.cn”
将192.168.100.152的主机名修改为“db.linuxfan.cn”
2)复制安全优化脚本“security.sh”到每台服务器上并执行。
3)security.sh要求实现:配置yum及yum更新系统,删除不必要的用户,关闭不必要的服务,设置防火墙默认规则,优化ssh配置,创建admin用户、初始密码123123并设置下次admin登录时必须修改密码且限制只允许该用户使用su命令,通过tcp wrapper设置只有192.168.100.100等登录。
2.编写脚本:
1)编写安全优化脚本:
vi security.sh
#!/bin/bash
相关知识学习完成后完成
useradd admin
echo 123123 |passwd --stdin admin
:wq
2)编写批量操作脚本:
[root@ns bin]# cat ssh-changename.sh
#!/bin/bash
#by linuxfan.cn 2016-9-24
##set variable
export PRE="192.168.100."
export PW="123123"
export HNF="/etc/sysconfig/network"
#create and security ssh pair key for ssh connect.
for i in {150,151,152};do
/usr/bin/expect <<EOF
spawn ssh-copy-id root@$PRE$i
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$PW\r" }
}
interact
expect eof
EOF
export CMD="ssh root@$PRE$i"
##change hostname.
ping -c 2 $PRE$i &>/dev/null
SETVAL=$?
if [ $i -eq 150 ] && [ $SETVAL -eq 0 ];then
$CMD "sed -i 's/^HOST.*/HOSTNAME=www.linuxfan.cn/g' $HNF "
##create test file and make dir.
$CMD "touch /tmp/public-key-test.txt;mkdir -p /root/bin" &>/dev/null
##copy security.sh to host and exec it.
scp /root/bin/security.sh root@$PRE$i:/root/bin &>/dev/null
$CMD "source /root/bin/security.sh" &>/dev/null
elif [ $i -eq 151 ] && [ $SETVAL -eq 0 ];then
$CMD "sed -i 's/^HOST.*/HOSTNAME=ca.linuxfan.cn/g' $HNF "
##create test file and make dir.
$CMD "touch /tmp/public-key-test.txt;mkdir -p /root/bin" &>/dev/null
##copy security.sh to host and exec it.
scp /root/bin/security.sh root@$PRE$i:/root/bin &>/dev/null
$CMD "source /root/bin/security.sh" &>/dev/null
elif [ $i -eq 152 ] && [ $SETVAL -eq 0 ];then
$CMD "sed -i 's/^HOST.*/HOSTNAME=mysql.linuxfan.cn/g' $HNF "
##create test file and make dir.
$CMD "touch /tmp/public-key-test.txt;mkdir -p /root/bin" &>/dev/null
##copy security.sh to host and exec it.
scp /root/bin/security.sh root@$PRE$i:/root/bin &>/dev/null
$CMD "source /root/bin/security.sh" &>/dev/null
else
echo "$PRE$i is down, Please check and try again."
exit 1
fi
done
[root@ns bin]#
3)测试:
分别在三台主机上查看:
id admin
cat /etc/sysconfig/network
一.环境介绍:
开启192.168.100.150-152(ctos1-3)和192.168.100.100(vsftpd)。
1.ssh登录:在192.168.100.100上操作
1)密码登录192.168.100.150:
ssh root@192.168.100.150 ##输入root密码123123
2)配置ssh密钥对登192.168.100.150:
ssh-keygen ##创建密钥对,提示直接回车
ssh-copy-id -i /root/.ssh/id_rsa.pub root@192.168.100.150 ##输入root密码上传公钥
ssh root@192.168.100.150 ##登录查看是否需要密码
ssh root@192.168.100.150 "ifconfig eth0" ##在192.168.100.150上执行命令“ifconfig eth0”查看ip地址
3)删除相关信息,为ssh脚本测试做准备:
192.168.100.100上:rm -rf /root/.ssh/known_hosts
192.168.100.150上:rm -rf /root/.ssh
二.编写脚本批量操作主机名:
1.需求描述:
1)修改主机名:
将192.168.100.150的主机名修改为“www.linuxfan.cn”
将192.168.100.151的主机名修改为“ca.linuxfan.cn”
将192.168.100.152的主机名修改为“db.linuxfan.cn”
2)复制安全优化脚本“security.sh”到每台服务器上并执行。
3)security.sh要求实现:配置yum及yum更新系统,删除不必要的用户,关闭不必要的服务,设置防火墙默认规则,优化ssh配置,创建admin用户、初始密码123123并设置下次admin登录时必须修改密码且限制只允许该用户使用su命令,通过tcp wrapper设置只有192.168.100.100等登录。
2.编写脚本:
1)编写安全优化脚本:
vi security.sh
#!/bin/bash
相关知识学习完成后完成
useradd admin
echo 123123 |passwd --stdin admin
:wq
2)编写批量操作脚本:
[root@ns bin]# cat ssh-changename.sh
#!/bin/bash
#by linuxfan.cn 2016-9-24
##set variable
export PRE="192.168.100."
export PW="123123"
export HNF="/etc/sysconfig/network"
#create and security ssh pair key for ssh connect.
for i in {150,151,152};do
/usr/bin/expect <<EOF
spawn ssh-copy-id root@$PRE$i
expect {
"(yes/no)?" { send "yes\r"; exp_continue }
"password:" { send "$PW\r" }
}
interact
expect eof
EOF
export CMD="ssh root@$PRE$i"
##change hostname.
ping -c 2 $PRE$i &>/dev/null
SETVAL=$?
if [ $i -eq 150 ] && [ $SETVAL -eq 0 ];then
$CMD "sed -i 's/^HOST.*/HOSTNAME=www.linuxfan.cn/g' $HNF "
##create test file and make dir.
$CMD "touch /tmp/public-key-test.txt;mkdir -p /root/bin" &>/dev/null
##copy security.sh to host and exec it.
scp /root/bin/security.sh root@$PRE$i:/root/bin &>/dev/null
$CMD "source /root/bin/security.sh" &>/dev/null
elif [ $i -eq 151 ] && [ $SETVAL -eq 0 ];then
$CMD "sed -i 's/^HOST.*/HOSTNAME=ca.linuxfan.cn/g' $HNF "
##create test file and make dir.
$CMD "touch /tmp/public-key-test.txt;mkdir -p /root/bin" &>/dev/null
##copy security.sh to host and exec it.
scp /root/bin/security.sh root@$PRE$i:/root/bin &>/dev/null
$CMD "source /root/bin/security.sh" &>/dev/null
elif [ $i -eq 152 ] && [ $SETVAL -eq 0 ];then
$CMD "sed -i 's/^HOST.*/HOSTNAME=mysql.linuxfan.cn/g' $HNF "
##create test file and make dir.
$CMD "touch /tmp/public-key-test.txt;mkdir -p /root/bin" &>/dev/null
##copy security.sh to host and exec it.
scp /root/bin/security.sh root@$PRE$i:/root/bin &>/dev/null
$CMD "source /root/bin/security.sh" &>/dev/null
else
echo "$PRE$i is down, Please check and try again."
exit 1
fi
done
[root@ns bin]#
3)测试:
分别在三台主机上查看:
id admin
cat /etc/sysconfig/network
相关文章推荐
- shell脚本批量操作linux主机:
- linux中shell常用联合批量操作命令
- [Shell] Linux主机密码批量回收 脚本
- linux shell ssh实现自动登录,并且执行一些操作并返回到当前主机
- linux下shell读写文件优化操作总结
- linux 批量删除及修改文件shell命令
- 在Linux系统中,批量添加用户的操作流程
- Linux脚本(shell)编程(三) 文件操作
- Linux中Shell操作Mysql
- linux perl shell perl vi grep awk xargs 批量替换(转载)
- Linux下Bash的操作环境相关,issue,motd,login shell,non-login shell,source,stty,set
- 用shell脚本批量连续更改FreeBSD主机ip
- linux批量修改文件后缀名shell脚本
- linux下shell脚本read操作
- linux 批量操作 方法
- python写的批量操作远程主机脚本(命令执行,上传、下载文件)
- 在Linux系统中,批量添加用户的操作流程
- 在Linux系统中,批量添加用户的操作流程
- 批量执行linux文件夹下的各个shell文件
- linux shell + expect:批量scp脚本工具