Expect的一些知识在这里下载和
分别解压、编译和安装
tar -zxvf ***.tar.gz
cd ***
perl MAKEFILE.PL
make
make test
make install
Windows 下的Expectexpect在linux和windows下的安装和使用Windows下使用SSH和Expect向Linux上传文件的例子
1、下载安
装SSH
Secure Shell Client 和Windows下的Expect。
2、安装后设置系统path,添加上述两个软件bin目录路径,下面是例子(请根据自己的安装路径设置):
;C:\Program Files\Expect-5.21\bin;C:\Program Files\SSH Communications Security\SSH Secure Shell
3、脚本实现
test1.bat(用于对对台主机实现批量的操作)
for /L %%i in (198,1,200) do expect test2 %%i |
test2(一些操作的例子,可以根据需要自己修改)
#!/bin/sh
#!/usr/bin/expect -f
set loginuser "root"
set loginpass {hit611}
set timeout 10
set cmd_prompt "]#|~]?"
#-------------------------------------------------- login by ssh
set ip [lindex $argv 0]
spawn ssh2.exe $loginuser@192.168.1.$ip
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
} -re "assword:" {
send "$loginpass\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}
expect {
-re $cmd_prompt {
send "mkdir /root/test\r"
}
}
expect {
-re $cmd_prompt {
send "exit\r"
}
}
spawn scp2.exe -d test/* $loginuser@$ipaddr$ip:/root/test
set timeout 10
expect {
-re "Are you sure you want to continue connecting (yes/no)?" {
send "yes\r"
} -re "assword:" {
send "$loginpass\r"
} -re "Permission denied, please try again." {
exit
} -re "Connection refused" {
exit
} timeout {
exit
} eof {
exit
}
}
expect {
-re $cmd_prompt {
eixt
}
}
#---------------------------------------------------- now,we do some commands
exit
#interact
|
4、执行test1.bat
阅读(2786) | 评论(0) | 转发(0) |