预期交互

拷贝密钥

#!/bin/bash

read -p "server ip:" ip
read -p "server pwd:" pwd

expect << EOF
spawn ssh-copy-id root@$ip 
expect "(yes/no)" {send "yes\r"}
expect "password:" {send "$pwd\r"}
expect "#" {send "exit\r"}
EOF
echo ''

为新安装的mysql设置密码策略及密码

#!/bin/bash

read -p 'set pwd:' pwd
temp=`awk -F: '/localhost/{print $4}' /var/log/mysqld.log  | awk  '{print $1}'`


expect << EOF
spawn mysql -uroot -p
expect ":" {send "$temp\r"}
expect "> " {send "set global validate_password_policy=0;\r"}
expect "> " {send "set global validate_password_length=6;\r"}
expect "> " {send 'alter user user() identified by "$pwd";\r'}
expect "#" {send "exit\r"}
EOF
echo ''

 

为新建的虚拟设置ip及主机名

#!/bin/bash
read -p "请输入hostname:" name
read -p "请输入ip :" ip
read -p "请输入要连接的虚拟机:" vm

expect <<  EOF
spawn virsh console $vm
expect " " {send "\r"}
expect ":" {send "root\r"}
expect "" {send "123456\r"}
expect "#" {send "hostnamectl set-hostname $name\r"}
expect "#" {send "hostname $name\r"}
expect "#" {send "nmcli connection modify eth0 ipv4.method manual ipv4.addresses $ip/24 connection.autoconnect yes\r"}
expect "#" {send "nmcli connection up eth0\r"}
expect "#" {send "exit\r"}
EOF
echo ''

 

posted @ 2019-01-28 10:51  Ray_chen  阅读(258)  评论(0编辑  收藏  举报