From 1bdcf1969832f3241e835e74fb21f86977cb6366 Mon Sep 17 00:00:00 2001 From: robert Date: Wed, 6 Jul 2016 14:22:42 +0800 Subject: [PATCH] expect --- expect/expect.sh | 19 +++++++++++++++++++ expect/login.exp | 22 ++++++++++++++++++++++ expect/login.exp1 | 21 +++++++++++++++++++++ expect/passwd.txt | 2 ++ 4 files changed, 64 insertions(+) create mode 100644 expect/expect.sh create mode 100644 expect/login.exp create mode 100644 expect/login.exp1 create mode 100644 expect/passwd.txt diff --git a/expect/expect.sh b/expect/expect.sh new file mode 100644 index 0000000..474b248 --- /dev/null +++ b/expect/expect.sh @@ -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 diff --git a/expect/login.exp b/expect/login.exp new file mode 100644 index 0000000..18f53b5 --- /dev/null +++ b/expect/login.exp @@ -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 diff --git a/expect/login.exp1 b/expect/login.exp1 new file mode 100644 index 0000000..6c0a578 --- /dev/null +++ b/expect/login.exp1 @@ -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} diff --git a/expect/passwd.txt b/expect/passwd.txt new file mode 100644 index 0000000..d831ab8 --- /dev/null +++ b/expect/passwd.txt @@ -0,0 +1,2 @@ +192.168.10.11 root qqqwww123 +192.168.10.12 root qqqwww123