expect做免密
#!/bin/bash
#IP=(192.168.8.100 192.168.8.24)
#changdu=$(echo ${#IP[@]})
#${IP[$changdu]} = $tianjia
#read -p "請(qǐng)輸入您想遠(yuǎn)程控制的電腦IP:" ip
#for i in ${IP[@]}
#do
#先創(chuàng)建密鑰
#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手動(dòng)輸入賬號(hào)密碼
#!/bin/bash
read -p "請(qǐng)輸入IP:" ip
read -p "請(qǐng)輸入密碼:" 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備份交換機(jī)配置腳本
#!/bin/bash
IP=192.168.1.200
username=admin
password=ampthon
date=`date +%F`
###所有回顯信息都會(huì)保存到這個(gè)文件內(nèi)
/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压语,創(chuàng)建對(duì)應(yīng)目錄并刪除之前上傳文件学辱,傳入新文件
#!/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系統(tǒng)
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
###設(shè)置永不超時(shí)仰冠,否則到時(shí)間會(huì)自動(dòng)退出
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 "進(jìn)入$1目錄\r";send "lcd /opt/linux_galaxy\r" }
"*Failed*" { send_user "沒(méi)有$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