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 ...
随机推荐
- 解决Python3下map函数的显示问题
今天小编就为大家分享一篇解决Python3下map函数的显示问题,具有很好的参考价值,希望对大家有所帮助.一起跟随小编过来看看吧map函数是Python里面比较重要的函数,设计灵感来自于函数式编程.P ...
- 41.Python中加载静态文件
在一个网页中,不仅仅只有一个html骨架,还需要css样式文件,js执行文件以及一些图片等.因此在DTL中加载静态文件时一个必须要解决的问题.在DTL中,使用static标签来加载静态文件.要使用st ...
- checkbox 样式重写
css样式 .me-checkbox:checked { background: #1673ff } .me-checkbox { outline: none;/*轮廓*/ width: 25px; ...
- CentOS7安装postgreSQL11
1.添加PostgreSQL Yum存储库 sudo yum install https://download.postgresql.org/pub/repos/yum/11/redhat/rhel- ...
- 剑指offer-面试题55-二叉树的深度-递归
/* 题目: 二叉树的深度 */ /* 思路: 根节点高度(0或1)+左子树的深度+右子树的深度 */ #include<iostream> #include<cstring> ...
- android中常用的布局管理器(二)
接上篇博客 (3)LinearLayout 线性布局管理器 线性布局管理器是将放入其中的组件按照垂直或水平方向来布局,每一行或每一列只能放一个组件,并且不会换行,当组件排列到窗体的边缘后,后面 ...
- 【转】为什么使用length获取Java数组的长度
记得vamcily 曾问我:“为什么获取数组的长度用.length(成员变量的形式),而获取String的长度用.length()(成员方法的形式)?” 我当时一听,觉得问得很有道理.做同样一件事情, ...
- Sass环境安装-Sass sublime 编辑器插件编译方法
首先官网(http://www.ruby-lang.org/en/downloads/)下载 ruby (1)打开链接进入到下载页面,点击如下位置进行下载 (2)下载页面 (3)进入到各个版本的列表页 ...
- Java(四)输出和输入函数
介绍一下Java里简单常用的输入输出方法. Java的输出函数很简单,直接调用System类的out对象的print函数即可. 代码: System.out.print(a);//输出变量a的值 Sy ...
- ip连接mysql时报不能连接
问题:springboot项目在用localhost连接mysql时没问题,但当localhost换成ip时出现 该问题:message from server: "Host 'DESKTO ...