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 ...
随机推荐
- java设计模式学习笔记--接口隔离原则
接口隔离原则简述 客户端不应该依赖它不需要的接口,即一个类对另一个类的依赖应建立在最小的接口上 应用场景 如下UML图 类A通过接口Interface1依赖类B,类C通过接口Interface1依赖类 ...
- PTA 符号配对 —— C++
请编写程序检查C语言源程序中下列符号是否配对:/*与 */.(与 ).[与].{与}. 输入格式: 输入为一个C语言源程序.当读到某一行中只有一个句点.和一个回车的时候,标志着输入结束.程序中需要检查 ...
- sklearn.metrics中的评估方法
https://www.cnblogs.com/mindy-snail/p/12445973.html 1.confusion_matrix 利用混淆矩阵进行评估 混淆矩阵说白了就是一张表格- 所有正 ...
- 关于Hosts与network的异同之处
1.hosts文件,路径:/etc/hosts,此文间是在网络上使用的,用于解析计算机名称和IP地址的映射关系,功能相当于windows下面的c:\windows\system32\drivers\e ...
- LeetCode 728. 自除数
题目链接:https://leetcode-cn.com/problems/self-dividing-numbers/ 给定上边界和下边界数字,输出一个列表,列表的元素是边界(含边界)内所有的自除数 ...
- 使用ssh localhost命令,发生异常ssh: connect to host localhost port 22: Connection refused
使用"ssh localhost"命令,失败: 问题分析如下: 出现这个问题是因为Ubuntu默认没有安装openssh-server.检查是否安装了openssh-server, ...
- exe 发布为服务
参考连接: https://www.cnblogs.com/liuxiaoji/p/8016261.html 1.有两个文件 srvany.exe,instsrv.exe 然后放到指定的文件下下: 2 ...
- 第一个,net core项目,一起入门 !!!
最近项目上开始使用.net core,新的项目,熟悉的东西比较多,现在花点时间来梳理一下,重头开始搭建一个.net core项目.哈哈,这个相对老手来说,估计会觉得小儿科,没事,也就当一次分享总结罢了 ...
- LeetCode 第二题 Add Two Numbers 大整数加法 高精度加法 链表
题意 You are given two non-empty linked lists representing two non-negative integers. The digits are s ...
- 402 WebEx会议教程二 —— 召开会议
··· WebEx会议教程二 —— 召开会议 简介:召开一个WebEx会议,并邮件邀请其他人参加会议 1. 安装快捷会议工具,将WebEx按钮集成到Outlook中. WebEx快捷会议工具- ...