Skip to content

Commit

Permalink
expect
Browse files Browse the repository at this point in the history
  • Loading branch information
ppabc committed Jul 6, 2016
1 parent 572935f commit 1bdcf19
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 0 deletions.
19 changes: 19 additions & 0 deletions expect/expect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
##yum install expect -y
for i in `awk '{print $1}' passwd.txt`
do
j=`awk -v I="$i" '{if(I==$1)print $2}' passwd.txt`
k=`awk -v I="$i" '{if(I==$1)print $3}' passwd.txt`

expect login.exp $i $j $k
#~/.ssh/known_hosts存在重复KEY处理
#echo $i
#check1=`cat ~/.ssh/known_hosts|grep "$i ssh"`
#echo $check1
#if [ "$check1" == "" ];then
#expect login.exp $i $j $k
#else
#sed -i "/$i ssh/d" ~/.ssh/known_hosts
#expect login.exp $i $j $k
#fi
done
22 changes: 22 additions & 0 deletions expect/login.exp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/usr/bin/expect
set timeout 10
set ipaddress [lindex $argv 0]
set username [lindex $argv 1]
set passwd [lindex $argv 2]
spawn ssh-copy-id -i /root/.ssh/id_rsa.pub $username@$ipaddress
expect {
#first connect, no public key in ~/.ssh/known_hosts
"Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
expect "password:"
send "$passwd\r"
}
#already has public key in ~/.ssh/known_hosts
"password:" {
send "$passwd\r"
}
"Now try logging into the machine" {
#it has authorized, do nothing!
}
}
expect eof
21 changes: 21 additions & 0 deletions expect/login.exp1
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/expect -f
set ipaddress [lindex $argv 0]
set username [lindex $argv 1]
set passwd [lindex $argv 2]
if { $argc != 3 } {
puts stderr $passwd
puts stderr $username
exit 1
}
set timeout 30
spawn ssh $username@$ipaddress
expect {
"yes/no" { send "yes\r";exp_continue }
"password:" { send "$passwd\r" }
}
expect "]*"
send "date \r"
send "df -h\r"
send "exit\r "
send "exit\r "
expect eof {exit 1}
2 changes: 2 additions & 0 deletions expect/passwd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
192.168.10.11 root qqqwww123
192.168.10.12 root qqqwww123

0 comments on commit 1bdcf19

Please sign in to comment.