- 一、Expect介绍
- 二、安装expect软件包
- 三、编写expect脚本
- 四、执行脚本
二、安装expect软件包Expect是一个自动交互式应用程序的工具,如telnet,ftp,passwd等。
yum -y install expect三、编写expect脚本
root@192 scripts]# cat expect_ssh.sh
#!/bin/bash
########################################
#Author:jeven
#time:Thu 12 May 2022 05:20:05 PM CST
#filename:expect_ssh.sh
#Script description:
########################################
USER=root
PASS=123456
IP=192.168.3.211
expect -c "
spawn ssh $USER@$IP
expect {
"(yes/no)" {send "yesr"; exp_continue}
"password:" {send "$PASSr"; exp_continue}
"$USER@*" {send "df -hr exitr"; exp_continue}
}"
四、执行脚本
[root@192 scripts]# ./expect_ssh.sh spawn ssh root@192.168.3.211 Activate the web console with: systemctl enable --now cockpit.socket Last login: Thu May 12 17:56:29 2022 from 192.168.3.84 [root@node1 ~]# df -h Filesystem Size Used Avail Use% Mounted on devtmpfs 970M 0 970M 0% /dev tmpfs 984M 0 984M 0% /dev/shm tmpfs 984M 9.4M 974M 1% /run tmpfs 984M 0 984M 0% /sys/fs/cgroup /dev/nvme0n1p3 18G 11G 7.3G 59% / /dev/sdb3 2.0G 47M 2.0G 3% /mnt/vdb3 /dev/mapper/vg01-lv01 7.0G 83M 7.0G 2% /mnt/lv01 /dev/mapper/vg02-lv02 575M 912K 532M 1% /mnt/lv02 /dev/nvme0n1p1 495M 140M 356M 29% /boot overlay 18G 11G 7.3G 59% /var/lib/docker/overlay2/8c7e59c24a0b2648c82f41eeddba522e58e06c6809ff702f641f6377b60e8d1f/merged overlay 18G 11G 7.3G 59% /var/lib/docker/overlay2/3851b60316c4c9b3d888c4e6133589bee2882b3e231cf2c4d9ff42eca7a4a390/merged tmpfs 197M 4.0K 197M 1% /run/user/0 [root@node1 ~]# exit logout Connection to 192.168.3.211 closed.



