tck/tl 以及expect脚本
最近有用到,利用expcet脚本自动登录到远程服务器并提权执行脚本。
搜集的知识如下:
代码如下
#!/usr/bin/expect --
if { $argc != && $argc != } { exit }
set ip [lindex $argv ]
set port [lindex $argv ]
set proto [lindex $argv ] ;#:ssh;:telnet
set user [lindex $argv ]
set pwd [binary format H* [lindex $argv ]]
set cmds [lindex $argv ] if { $argc == } {
set root_pwd [binary format H* [lindex $argv ]]
puts "root_pwd:$root_pwd"
}
puts "ip:$ip";一些输出方便观察
puts "port:$port"
puts "proto:$proto"
puts "user:$user"
puts "pwd:$pwd"
puts "cmds:$cmds"
set timeout 30;设置超时 #set default client
set ssh "/usr/bin/ssh" #set default promptions
set login_pmt "ogin:"
set pwd_pmt "assword:"
set user_pmt "$ "
set root_pmt "# "
set login_fail_pmt "error"
set elevation_cmd "su -"
set elevation_pmt "assword:"
set elevation_ok_pmt "$root_pmt"
set elevation_failed_pmt "$user_pmt" ;把$符号转义一下
if { $user_pmt == "$" } {
set user_pmt "\$"
}
if { $root_pmt == "$" } {
set root_pmt "\$"
}
#puts "login_ont is $login_pmt"
;函数
proc handle_cmds { } {
global cmds user_pmt
set hex_cmds [split $cmds "|"]
puts "into handle_cmds"
foreach hex_cmd $hex_cmds {
set cmd [binary format H* $hex_cmd]
send -- "$cmd\r"
expect {
"$user_pmt" { }
"not found" { }
eof { exit }
timeout { exit }
}
}
} proc handle_cmds_root { } {
global cmds root_pmt
set hex_cmds [split $cmds "|"]
puts "into handle_cmds_root"
foreach hex_cmd $hex_cmds {
set cmd [binary format H* $hex_cmd]
send -- "$cmd\r"
puts "root:$cmd"
expect {
"$root_pmt" { } eof { exit }
timeout { exit }
}
}
} proc handle_pwd { } {
global pwd pwd_pmt user_pmt login_fail_pmt argc root_pwd root_pmt
puts "into handle_pwd"
puts "pwd:$pwd"
puts "pwd_pmt:$pwd_pmt"
send -- "$pwd\r" expect {
"$user_pmt" {
send -- "export LANG=en_US.UTF-8\r"
send -- "export LANGUAGE=en_US.UTF-8\r"
puts "argc $argc"
if { $argc == } {
send -- "su -\r" expect {
"$pwd_pmt" {
send -- "$root_pwd\r" expect {
"$root_pmt" handle_cmds_root eof { exit }
timeout { exit }
} }
eof { puts "-eof" ; exit }
timeout { puts "-timeout"; exit }
}
} elseif { $argc == } {
handle_cmds
}
}
timeout { puts "timeout" ; exit }
eof { puts "eof" ; exit }
}
exit
} proc handle_user { } {
global user pwd_pmt
send -- "$user\r"
expect {
"$pwd_pmt" handle_pwd
timeout { exit }
eof { exit }
}
} puts "result:$result" if { $proto == "" } {
if { $result == "CONTINUE" || $result == "ERROR" } {
spawn $ssh -p $port $user@$ip
} else {
send "$ssh -p $port $user@$ip\r"
}
} elseif { $proto == "" } {
if { $result == "CONTINUE" } {
spawn -noecho $telnet $ip $port
} else {
send -- "$telnet $ip $port\r"
}
} expect {
"$pwd_pmt" handle_pwd
"$login_pmt" handle_user
"(yes/no)?" {
puts "yes/no?"
send "yes\r"
expect {
"$pwd_pmt" { handle_pwd }
timeout { exit }
eof { exit }
}
}
eof { exit }
timeout { exit }
}
tck/tl 以及expect脚本的更多相关文章
- shell脚本嵌套expect脚本
#!/bin/sh echo "helo" password='xxxx' ###不能在下面的expect脚本段设置成 set password xxxx否则获取不到变量,单独的e ...
- 使用expect脚本语言写一键发布服务(代码发布、所有服务重启)
互联网服务有很多台服务,但是在上线的时候需要将这些服务版本都更新与个个都重启,下面的脚本语言,就是一键发布服务~ 1.在/home/weihu/deploy/ 目录下建下publish .publis ...
- Linux 下 expect 脚本语言中交互处理常用命令
Linux 下 expect 脚本语言中交互处理常用命令 1. #!/usr/bin/expect 告诉操作系统脚本里的代码使用那一个 shell 来执行.这里的 expect 其实和 Linux 下 ...
- Mac 让 iTerm2 记住用户名密码 expect 脚本
刚刚用iTerm2的时候,总是要一遍遍的敲用户名.密码. 我在想, 能不能像Windows的软件一样,可以直接让软件记住.然后只要点击一下,就直接ssh到远程服务器上面去了. 之后经过搜索,可以用ex ...
- expect脚本同步文件 expect脚本指定host和要同步的文件 构建文件分发系统 批量远程执行命令
自动同步文件 #!/usr/bin/expect set " spawn rsync -av root@.txt /tmp/ expect { "yes/no" { se ...
- 分发系统介绍 expect脚本远程登录 expect脚本远程执行命令 expect脚本传递参数
expect脚本远程登录 yum install -y expect yum install -y tcl tclx tcl-devel 自动远程登录 #! /usr/bin/expect set h ...
- 与服务器同步工程(expect脚本)
先看下我实际用的例子: #!/usr/bin/expect spawn rsync -vazu ssh-src/src wayne@192.168.5.2:~/projects/ expect &qu ...
- shell脚本通过expect脚本实现自动输入密码(使用expect)
背景:在远程文件下载时,需要输入对方的服务器密码,shell不支持交互输入内容,可以用下面两种方式实现 一.在shell脚本中嵌入expect来实现密码输入 expect是一个自动交互功能的工具. ...
- shell脚本通过expect脚本实现自动输入密码
背景:在远程文件下载时,需要输入对方的服务器密码,shell不支持交互输入内容,可以用下面两种方式实现 一.在shell脚本中嵌入expect来实现密码输入 expect是一个自动交互功能的工具 ...
随机推荐
- 怎么自动响应richTextBox超级链接单击click事件
如上图所示,怎么自动响应richTextBox超级链接单击click事件?步骤如下: 1. 增加 richTextBox1_LinkClicked 事件: 2. 编辑事件内容如下: private ...
- centos7.4版本安装nmon监控软件
一.检查安装环境 # uname –a (查看操作系统信息,所检查服务器为64位操作系统) Linux iZ94pmb2p24Z 2.6.32-431.23.3.el6.x86_64 #1 SMP T ...
- VSCode配置JAVA开发环境
VSCode配置JAVA开发环境 1:给机器安装JDK.MAVEN 下载JDK 下载路径:https://www.oracle.com/technetwork/java/javase/download ...
- 【bzoj2186】: [Sdoi2008]沙拉公主的困惑 数论-欧拉函数
[bzoj2186]: [Sdoi2008]沙拉公主的困惑 考虑当 gcd(a,b)=1 则 gcd(nb+a,b)=1 所以[1,N!]与M!互质的个数就是 筛出[1,M]所有的素数p[i] 以及逆 ...
- CentOS下安装配置SVN服务器并自动同步到web目录
一.安装 yum install subversion测试是否安装成功 /usr/bin/svnserve --version如提示以下内容,说明已安装成功 svnserve,版本 1.6.11 (r ...
- IDEA mybatis-generator 逆向工程
1.在maven工程中的resource中创建generatorConfig.xml 2.配置generatorConfig.xml <?xml version="1.0" ...
- ios对new Date() 的兼容问题
移动开发的一个小坑 我们一般这样创建一个日期变量 var d = new Date("2017-08-11 12:00:00"); 但是在ios上却是返回 invalid Date ...
- 浅谈python web框架django2.x
1.Django简介 Python下有多款不同的 Web 框架,Django是最有代表性的一种.许多成功的网站和APP都基于Django. Django是一个开源的Web应用框架,由Python写成. ...
- CF708B Recover the String 构造
For each string s consisting of characters '0' and '1' one can define four integers a00, a01, a10 an ...
- 最长子序列问题(二分+贪心nlogn算法)
[题目描述] 给定N个数,求这N个数的最长上升子序列的长度. [样例输入] 7 2 5 3 4 1 7 6 [样例输出] 4 什么是最长上升子序列? 就是给你一个序列,请你在其中求出一段不断严格上升的 ...