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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值