expect使用

expect做免密

#!/bin/bash
#IP=(192.168.8.100 192.168.8.24)
#changdu=$(echo ${#IP[@]})
#${IP[$changdu]} = $tianjia
#read -p "请输入您想远程控制的电脑IP:" ip
#for i in ${IP[@]}
#do
#先创建密钥
#ssh-keygen -P "" -f /root/.ssh/id_ecdsa -t ecdsa
#ping -c 4 $i
#   if [ $? -eq 0 ]
#   then
/usr/bin/expect <<-EOF
    set timeout 50
    spawn ssh-copy-id -i /root/.ssh/id_ecdsa.pub cka@192.168.8.25
#   spawn ssh-copy-id  -i /root/.ssh/id_rsa.pub cka@192.168.8.25
    expect  {
        "yes/no" { send "yes\r";exp_continue }
        "password" { send "123456\r" }
    }
    expect eof
EOF
#   scp /root/chuangjian.sh root@$i:/root/
#   ssh root@$i bash /root/chuangjian.sh
#   ssh root@192.168.8.100 echo "ifconfig" >> /root/.bash_profile
#   fi
#done
ssh-keygen -t dsa -f /root/.ssh/id_dsa  -P "" 

expect手动输入账号密码

#!/bin/bash

read -p "请输入IP:" ip
read -p "请输入密码:" passwd

/usr/bin/expect <<-EOF
    set timeout 60
    spawn scp zabbix-4.2.3.tar.gz zabbix-agentd.sh yum.sh root@$ip:/data/
    expect {
        "yes/no" { send "yes\r";exp_continue }
        "password" { send "$passwd\r" }
    }
    expect eof
EOF
/usr/bin/expect <<-EOF
        set timeout 60
        spawn ssh root@$ip bash /data/zabbix-agentd.sh
        expect {
                "yes/no" { send "yes\r";exp_continue }
                "password" { send "$passwd\r" }
        }
        expect eof
EOF

expect备份交换机配置脚本

#!/bin/bash
IP=192.168.1.200
username=admin
password=ampthon
date=`date +%F`


###所有回显信息都会保存到这个文件内
/usr/bin/expect > /opt/backup/SW/H3C-SW/$date.cfg <<-EOF     
    set timeout 60
    spawn telnet $IP
    expect "Password:"
    send "$password\r"
    expect "*>"
    send "display  current-configuration \r"
    while (1) {
        expect {
            "*--- More ----" { send " " }
            "return" { break }
        }
    }
    expect "*>"
    send "quit\r"
EOF

expect登录FTP上传文件

安装ftp以及expect 之后登陆ftp,创建对应目录并删除之前上传文件,传入新文件

#!/bin/bash
###配置yum源
function repo() {
num=`cat /etc/redhat-release | awk 'print $7'`
cd /etc/yum.repos.d/
[ -d bak ] || mkdir bak
[ -f local.repo ] || mv *.repo bak/
[ -f local.repo ] || cat > local.repo << EOF
[local]
name=local
baseurl=ftp://192.168.0.225/rhel$num
enabled=1
gpgcheck=1
EOF
yum clean all
[ $? -eq 0 ] && echo "------yum源配置完成------"
}

###安装ftp以及expect
##判断是不是redhat系统
cat /proc/version | grep -i "red hat"
if [ $? -eq 0 ]
then
        repo
        expect -version
        [ $? -eq 0 ] || yum -y install expect
        rpm -qa | grep -w "ftp"
        [ $? -eq 0 ] || yum -y install ftp
else
         expect -version
        [ $? -eq 0 ] || apt -y install expect
        dpkg -s ftp
        [ $? -eq 0 ] || apt -y install ftp
fi

###登陆ftp
/usr/bin/expect <<-EOF
    spawn ftp 192.168.0.225
    ###设置永不超时,否则到时间会自动退出
    set timeout -1
    expect "Name*"
        send "anonymous"
        expect "Password:"
        send "\r"
        expect "ftp>*"
        send "mkdir $1\r"
        expect "ftp>*"
        send "cd $1\r"
        expect {
                    "*successfully*" { send_user "进入$1目录\r";send "lcd /opt/linux_galaxy\r" }
                    "*Failed*" { send_user "没有$1目录\r";send "quit\r" }
        }
        expect "ftp>*"
        send "delete linux_galaxy\r"
        expect "ftp>*"
        send "put linux_galaxy\r"
        expect {
                    "*sent*" { send_user "$1文件上传完成\r";send "quit\r" }
        }
EOF
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容