暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

expect 脚本

天道酬勤 2022-06-23
311

1、ftp登录远程服务器

#!/usr/bin/expect -f 

spawn ftp 192.168.250.12

expect "Name (192.168.250.12:root):" { send "root\r"}

expect "Password:" { send "cmcc\r" } 

expect "ftp>" {send "binary\r"}

expect "ftp>" {send "get anaconda-ks.cfg\r"}

expect "ftp>" {send "exit\r"}


2、ssh到远程机器并交互式输入命令后退出

#!/usr/bin/expect -f 

if { $argc != 2 } {

   puts "args error!for example:expect ssh.expect user ip"

   exit

}

set user [lindex $argv 0] 

set ip [lindex $argv 1]

set passwd [lindex $argv 2]

spawn ssh -l $user $ip 

expect { 

"*yes/no" {send "yes\r";exp_continue}

"*password:" {send "cmcc\r";exp_continue}

"#*" {send "mkdir -p /opt/test1\r" }

}

send "exit \r"

#interact    ##交互式命令,如果有此参数,脚本退出,但是窗口不退出,可以直接在远程机上输入命令

expect eof


3、匹配任意条件后输入对应的结果

#!/usr/bin/expect -f

expect {

"hi" { send "you said hi\n"}

"hello" {send "you said hello\n"}

"word" {send "you said word\n"}

}

「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论