linux交互执行命令,expect
转载 http://donex.blog.51cto.com/2005970/834467
原文比较乱,只能参考
本地交互执行:
1. 修改shell
#!/usr/bin/expect
set USER [lindex $argv 0]
set SHELL [lindex $argv 1]
set timeout 3
spawn chsh $USER
expect "*]:*" { send "$SHELL\r" }
expect eof
# ./chsh.sh user1 /bin/tcsh
2. 修改密码
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASS "1q2w#E\$R"
set timeout 3
spawn passwd $USER
expect "*Password:*" { send "$PASS\r" }
expect "*Password:*" { send "$PASS\r" }
expect eof
# ./pass.sh user1
或把用户和密码都作为参数
#!/usr/bin/expect
set USER [lindex $argv 0]
set PASS [lindex $argv 1]
set timeout 3
spawn passwd $USER
expect "*Password:*" { send "$PASS\r" }
expect "*Password:*" { send "$PASS\r" }
expect eof
# ./pass.sh ttt 1q2w#E$R
# ./pass.sh ttt "1q2w#E\$R"
总结:expect 必须要匹配最后一个输出字符。
远程交互ssh 登录:
1. 设置变量,执行多命令。
#!/usr/bin/expect
set IP "10.85.138.42"
set timeout 3
spawn ssh ${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "touch /tanjiyong/newfile\r" }
expect eof
#./exp.sh
2. 增加参数。
#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set timeout 3
spawn ssh $USER@${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "touch /tanjiyong/newfile\r" }
expect eof
#./exp.sh 10.85.138.42 root
3. ssh 登录,执行时间超过timeout时间,设定timeout为-1(无限制)。
#!/usr/bin/expect
set IP [lindex $argv 0]
set USER [lindex $argv 1]
set IP2 [lindex $argv 2]
set timeout 3
spawn ssh $USER@${IP}
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "root\r"}
expect "*#*" { send "ls\r" }
expect "*#*" { send "ping $IP2\r" }
set timeout -1
expect "*#*" { send "exit 1\r" }
expect eof
4. ssh 登录,使用循环,在30台机器执行相同命令。
#!/usr/bin/expect
set USER root
set PASS root
for {set i 1} {$i<=30} {incr i} {
spawn ssh -l $USER 125.1.1.$i
expect "*yes/no*" { send "yes\r" }
expect "*assword*" { send "$PASS\r"}
expect "*#*" { send "find / -name hao.txt\r" }
expect eof
}
#./exp.sh
本地远程交互执行:
1. spawn 执行scp
#!/usr/bin/expect
set PASS root
set timeout 3
spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1
expect "*#*" { send "exit 1\r" }
# ./scp.sh
使用-- send
#!/usr/bin/expect --
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL) #spawn /bin/bash
expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1 #复制的时间较长,设置为timeout无限制
expect "*#*" { send "exit 1\r" }
#!/usr/bin/expect
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL)
expect "*#*" { send -- "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "$PASS\r" }
set timeout -1 #复制的时间较长,设置为timeout无限制
expect "*#*" { send "exit 1\r" }
#!/usr/bin/expect
set timeout 3
set env(SHELL) /bin/bash
spawn \$env(SHELL)
expect -exact "# "
send -- "scp $TAR_NAME ${USERNAME}@${DESTIP}{DESTDIR}\r"
expect {
"*yes/no*" { send "yes\r";exp_continue }
"assword: " { send "hw2009\r" }
}
set timeout -1 #复制的时间较长,设置为timeout无限制
expect "# "
send "exit\r"
expect eof
2. expect 搜索块
#!/usr/bin/expect
set PASS root
set timeout 3
spawn scp /etc/passwd root@10.85.138.42:/tanjiyong
expect {
"*yes/no*" { send "yes\r";exp_continue }
"assword: " { send "$PASS\r" }
}
set timeout -1
expect "*#*" { send "exit 1\r" }
# ./scp.sh
3. 判断
#!/usr/bin/expect
set PASS [lindex $argv 0]
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn $env(SHELL)
expect "*#*" { send "/usr/bin/scp /etc/passwd $USER@$IP:/tanjiyong\r" }
expect {
"*yes/no*" { send "yes\r" }
"*Password:*" { send "$PASS\r" }
}
expect "*Password:*" { send_user "\nPasswd error!\n";exit 1 }
set timeout -1
expect "# " { send "exit 1\r" }
expect eof
# ./scp.sh
4. rsync 备份使用。
#!/usr/bin/expect --
spawn ssh backup@10.85.138.212
expect {
"(yes/no)?" {
send "yes\r"
}
"assword" {
send "123456\r"
}
}
send "rsync -avz rsync@10.85.138.212:/home/html /opt\r"
expect "total size"
expect {
"rsync error" {
exit 1
}
}
expect "# "
send "exit\r"
interact
#expect eof
脚本中使用:
#!/bin/bash
echo "Start..."
cat << EOF > /expectfile
#!/usr/bin/expect
set PASS root
set USER root
set IP 10.85.138.42
set env(SHELL) /bin/bash
set timeout 1
spawn \$env(SHELL) #必须加上\,不然会被置换为空。
expect "*#*" { send "/usr/bin/scp /etc/passwd \$USER@\$IP:/tanjiyong\r" }
expect "*yes/no*" { send "yes\r" }
expect "*Password:*" { send "\$PASS\r" }
set timeout -1
expect "# " { send "exit 1\r" }
expect eof
EOF
expect -f /expectfile
echo "$?"
echo "finished backup.."
附:修改SSH Client为非ask模式
vi /etc/ssh/ssh_config
# StrictHostKeyChecking ask
StrictHostKeyChecking no
参考:自动登录
#!/usr/bin/expect
if {$argc!=3} { # 疑问:参数个数($#)
send_user "Usage: $argv0 {Array IP} {Password} {CMD}\n\n"
exit
}
set IP [lindex $argv 0]
set Password [lindex $argv 1]
set CMD [lindex $argv 2]
spawn ssh admin@$IP
expect {
"assword:" {
exec sleep 1
send "${Password}\r"
}
"*continue connecting*" {
exec sleep 1
send "yes\r"
expect "*Password:" {
exec sleep 1
send "${Password}\r"
}
}
}
expect {
"*Password*" { send_user "\n1assword error!\n"
exit
}
"*already*" { send_user "\n2:Repeated login!\n"
exit
}
"OceanStor: admin>" { send "${CMD}\r" }
}
expect "*>"
send "exit\r"
expect "*closed*"
exit
脚本2
#!/usr/bin/expect
set ipaddress [lindex $argv 0]
set passwd [lindex $argv 1]
spawn ssh -p 22 root@$ipaddress
expect {
"want" {send -- "yes\r"; exp_continue}
"password:" {send -- "$passwd"}
"No route" { exit }
}
set timeout 5
send "\n"
expect "*justin*"
send "pwd\r"
expect "*OK*"
send "exit\r"
expect eof
相关:
exit 1/exit #停止执行,退出脚本
expect eof #脚本结束
expect -exact "# " #精确匹配
expect "# "
expect "*#*" #模糊匹配
exp_continue #在同一个expect块里,做多次匹配。
send #发送命令。
send_user #打印终端信息。用法与send一致。
linux交互执行命令,expect的更多相关文章
- Linux后台执行命令:&和nohup nohup和&后台运行,进程查看及终止
nohup和&后台运行,进程查看及终止 阅读目录 nohup和&后台运行,进程查看及终止 1.nohup 2.& 3.nohup和&的区别 &:是指在后台运 ...
- linux中执行命令权限不够怎样处理
在linux中执行命令权限不够就要增加权限,先看遇到的情况 查看权限情况 那就赋予权限 执行命令
- Android 开发进入Linux系统执行命令 2018-5-25 Fri.
/** * 进入linux cmd执行命令 * * @param command * @return */ private boolean runRootCommand(String command) ...
- [转帖]Linux后端执行命令的方法
Linux 后台执行命令的方法 http://bbs.chinaunix.net/forum.php?mod=viewthread&tid=4241330&fromuid=212883 ...
- java使用ssh连接Linux并执行命令
方式1:通过设置账号密码和链接地址 maven pom.xml配置: <dependency> <groupId>com.jcraft</groupId ...
- Linux远程ssh执行命令expect使用及几种方法
expect命令实现脚本免交互 一.Linux下SSH无密码认证远程执行命令 在客户端使用ssh-keygen生成密钥对,然后把公钥复制到服务端(authorized_keys). 实现步骤: 1.客 ...
- Linux之执行命令操作20170330
介绍一下Linux系统中的代码执行shell等命令的几种操作方式: 一.标准流管道popen 该函数的原型是FILE * popen(const char* command, const char * ...
- php 执行计划任务方式之 linux crontab 执行命令
一.crond简介 crond 是linux下用来周期性的执行某种任务或等待处理某些事件的一个守护进程,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务 工具,并且会自动启动c ...
- 【转载】在LoadRunner向远程Linux/Unix执行命令行并收集性能数据
前面介绍过在LoadRunner的Java协议实现“使用SSH连接Linux”,当然连接之后的故事由你主导. 今天要讲的,是一个非Java版本.是对“在LoadRunner中执行命令行程序之:pope ...
随机推荐
- 【IDE】IDEA - 使用问题记录
1.显示方法API:类似eclipse把鼠标放到方法上有api信息. setting/keymap中搜索 quick docs:(有说默认快捷键是ctrl+q,但我的不是待查找快捷键) 2.forea ...
- 吴裕雄--天生自然 JAVA开发学习:Java 开发环境配置
- 英语语法 ( Spoken language )
- - - -------------- 1,五个语序: 主语+谓语(中英语序一致)主语+系动词+表语 (中英语序一致)主语+谓语+宾语(中英语序一致)主语+谓语+间宾+直宾(中英语序一致)主语+谓语 ...
- 通过 Chrome浏览器 查看http请求报文
as we all know HTTP 请求报文 包含请求行.请求头和请求体三部分 请求行:(请求方式 资源路径 协议/版本) 例如:POST /test/index.html HTTP/1.1 P ...
- JS格式时间
Date.prototype.format = function(format) { var o = { "M+": this.getMonth() + 1, //month &q ...
- C# 中indexOf、lastIndexOf、subString方法的理解
一.indexOf() indexOf("\\"):返回"\\"字符在此实例中第一个出现的索引位置,实例的下标是从0开始,如果未找到则返回-1. indexOf ...
- Linux-Windows10双系统安装
在Windows10系统上搭建完深度学习环境用于无人驾驶中的目标检测后,想在Linux系统上再尝试一下.由于VMware虚拟机安装的Linux系统不支持物理硬件,所以需要一步到位安装一个双系统.本文介 ...
- JS获取标签内容的方法
JS获取标签内容的方法 测试代码 <!DOCTYPE html> <html lang="en"> <head> <meta charse ...
- AGC001 E - BBQ Hard [组合数]
这题就是要求 \(\sum_{i=1}^{n} \sum_{j=i+1}^{n} C(a_i+a_j+b_i+b_j,a_i+a_j)\) 考虑搞一搞,\(C(a_i+a_j+b_i+b_j,a_i+ ...
- 0011 基于DRF框架开发(04 普通序列化器)
普通序列化器和模型无关,只是对针对提交字段的定义. 本文定义三个序列化器: 教师序列化器,学生序列化器,教师学生序列化器.这三个序列化器都使用普通序列化器. 1 教师序列化器 在Application ...