shell脚本基础
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了shell脚本基础相关的知识,希望对你有一定的参考价值。
一、分发系统介绍
当业务越做越大,服务器需求越来越多,几台服务器的话还好一点;当十几、几十台的时候,工作量就非常大!并且不规范,需要一个模板机分发到各个机器上去。
可以用开源的软件,expect脚本语言,进行实现分发系统的功能。
二、expect脚本远程登录
[[email protected] ~]# yum install -y expect //安装
[[email protected] sbin]# vim 01.expect //自动远程登录,并执行命令
#!/usr/bin/expect
set host "192.168.242.129"
#远程机器IP
set passwd "rootroot"
spawn ssh [email protected]$host
expect {
"yes/no" {send "yes\r"; exp_continue}
"assword:" {send "$passwd\r"}
}
interact #表示停留在机器上
#如果需要退出 可以expect eof
[[email protected] sbin]# chmod a+x 01.expect //授予执行权限
您在 /var/spool/mail/root 中有新邮件
[[email protected] sbin]# ./01.expect //执行
spawn ssh [email protected]
The authenticity of host ‘192.168.242.129 (192.168.242.129)‘ can‘t be established.
ECDSA key fingerprint is 22:fb:63:5d:8c:78:4e:74:99:f7:b1:b3:a3:70:8d:d3.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘192.168.242.129‘ (ECDSA) to the list of known hosts.
[email protected]‘s password:
Last login: Wed Apr 25 21:37:48 2018 from 192.168.242.1
三、expect自动远程登录后执行命令并退出
[[email protected] sbin]# vim 02.expect //建立脚本
#!/usr/bin/expect
set user "root"
set passwd "rootroot"
spawn ssh [email protected]
expect {
"yes/no" { send "yes\r"; exp_continue}
"password:" { send "$passwd\r" }
}
expect "]*"
send "touch /tmp/12.txt\r"
expect "]*"
send "echo 1212 > /tmp/12.txt\r"
expect "]*"
send "exit\r"
[[email protected] sbin]# chmod a+x 02.expect
[[email protected] sbin]# ./02.expect
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 21:43:13 2018 from 192.168.242.128
[[email protected] ~]# touch /tmp/12.txt
[[email protected] ~]# echo 1212 > /tmp/12.txt
[[email protected] ~]# [[email protected] sbin]#
#远程客户端上查看是否成功创建文件
[[email protected] ~]# ls /tmp/
12.txt mysql.sock systemd-private-152865e96f164fe4b06027db92130672-vmtoolsd.service-sxTKDs
[[email protected] ~]# cat /tmp/12.txt
1212
四、expect脚本传递参数
[[email protected] sbin]# vim 03.expect
#!/usr/bin/expect
set user [lindex $argv 0]
set host [lindex $argv 1]
set passwd "rootroot"
set cm [lindex $argv 2]
spawn ssh [email protected]$host
expect {
"yes/no" { send "yes\r"}
"password:" { send "$passwd\r" }
}
expect "]*"
send "$cm\r"
expect "]*"
send "exit\r"
[[email protected] sbin]# chmod a+x 03.expect
[[email protected] sbin]# ./03.expect root 192.168.242.129 ls
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 23:31:11 2018 from 192.168.242.128
[[email protected] ~]# ls
anaconda-ks.cfg
[[email protected] ~]# [[email protected] sbin]# ./03.expect root 192.168.242.129 "ls;ps aux | grep mysql"
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 23:32:12 2018 from 192.168.242.128
[[email protected] ~]# ls;ps aux | grep mysql
anaconda-ks.cfg
root 798 0.0 0.0 115380 1704 ? S 14:00 0:00 /bin/sh /usr/local/mysql/bin/mysqld_safe --datadir=/data/mysql --pid-file=/data/mysql/zlinux-02.pid
mysql 1227 0.0 24.4 1300788 456724 ? Sl 14:00 0:18 /usr/local/mysql/bin/mysqld --basedir=/usr/local/mysql --datadir=/data/mysql --plugin-dir=/usr/local/mysql/lib/plugin --user=mysql --log-error=/data/mysql/zlinux-02.err --pid-file=/data/mysql/zlinux-02.pid --socket=/tmp/mysql.sock --port=3306
root 2588 0.0 0.0 112664 972 pts/1 S+ 23:32 0:00 grep --color=auto mysql
[[email protected] ~]# [[email protected] sbin]# ./03.expect root 192.168.242.129 "ls;ps aux | grep nginx"
spawn ssh [email protected]
[email protected]‘s password:
Last login: Wed Apr 25 23:32:47 2018 from 192.168.242.128
[[email protected] ~]# ls;ps aux | grep nginx
anaconda-ks.cfg
root 2609 0.0 0.0 112664 972 pts/1 S+ 23:33 0:00 grep --color=auto nginx
以上是关于shell脚本基础的主要内容,如果未能解决你的问题,请参考以下文章