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 ...
随机推荐
- linux之定时任务调度
crond:任务调度 任务调度:是指系统在某个时间执行特定的程序或命令 任务调度分类: (1)系统工作,有些重要的工作必须周而复始地进行,如病毒扫描等 (2)个别用户工作:个别用户可能希望执行某些程序 ...
- 【Spring】bean的作用域(@Scope) - singleton、prototype
已知spring 3+已拥有多种不同的作用域: singleton(默认).prototype.request.session.global session.(参考: spring中scope作用域( ...
- 当课堂因监控技术变“囚笼”,存在争议的AI使用场景该被抵制吗?
当马云和马斯克高谈阔论AI是否会影响人类社会时,尚无"感情"的AI已在校园中"作恶".近日,一张AI监控课堂的GIF在网上迅速刷屏.这张GIF中记录了课堂中所有 ...
- 框架里增加.env文件的作用
在实际开发中我们常常遇到这样的问题,就是开发地点不固定,这就造成了我们需要频繁的更改数据库配置,给开发工作造成了麻烦,.env环境文件的出现解决了这个麻烦,我们只需要在不同的工作地点配置好.env文件 ...
- SpringCloud(一)
什么是SpringCloud? Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均 ...
- Pikachu-Unsafe Filedownload(不安全的文件下载)
不安全的文件下载概述 文件下载功能在很多web系统上都会出现,一般我们当点击下载链接,便会向后台发送一个下载请求,一般这个请求会包含一个需要下载的文件名称,后台在收到请求后 会开始执行下载代码,将该文 ...
- StyleLint 使用指南
StyleLint 是『一个强大的.现代化的 CSS 检测工具』, 与 ESLint 类似, 是通过定义一系列的编码风格规则帮助我们避免在样式表中出现错误. 安装stylelint npm insta ...
- VSCode常用插件汇总
vscode常用插件汇总: 点击插件名字,查看使用文档 vscode-fileheader : 添加注释到文件头,并支持自动更新文件修改时间. EditorConfig for vs code : ...
- Linux X_window与文本模式的切换
用x_window启动的情况下的切换方法: [Ctrl] + [Alt] + [F1] ~ [F6] :文字接口登陆 tty1 ~ tty6 终端机: [Ctrl] + [Alt] + [F7] ...
- 【android】Parcelable的相关技术总结
关于Parcelable的相关知识学习 进行Android开发的时候,无法将对象的引用传给Activities或者Fragments,我们需要将这些对象放到一个Intent或者Bundle里面,然 ...