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是一个自动交互功能的工具 ...
随机推荐
- fiddler扩展模拟弱网络环境设置
今天在qq群中有人问到怎么模拟app弱网络环境,我查了下资料,记得之前做测试的时候是设置fiddler断点,app请求后止于fiddler断点,app一直拿不到响应结果就应该要给出网络请求失败的提示, ...
- 深入理解sudo
[root@cairui ~]# cat /etc/sudoers ## Sudoers allows particular users to run various commands as ## t ...
- linux网络编程中的基本概念
int close(int fd)(假设是服务器端) close 关闭了自身数据传输的两个方向.close一个TCP套接字的默认行为是把该套接字标记成已关闭,然后立即返回到调用进程.该套接字描述符不能 ...
- 在Mvc中,ExpandoObjec类的使用
创建一个基本mvc项目 1.向Models目录下添加一个类文件MyModel.cs文件,代码如下: using System; using System.Collections.Generic; us ...
- PHP 实现实现多线程
前言 前些天帮同事查一个问题,第一次接触到了 PHP 的多线程,原以为 PHP 普遍都是单线程模型,并不适合多线程领域,花些时间翻了几个多线程的项目源码之后,发现 PHP 的多线程也颇有可取之处,活用 ...
- vue.js使用elemnetUi
安装: npm 安装 推荐使用 npm 的方式安装,它能更好地和 webpack 打包工具配合使用. npm i element-ui -S 引入 Element 你可以引入整个 Element,或是 ...
- html表单笔记
1.下面是 <form> 属性的列表: accept-charset 规定在被提交表单中使用的字符集(默认:页面字符集). action 规定向何处提交表单的地址(URL)(提交页面). ...
- [Node.jS]shelljs
shelljs : https://www.npmjs.org/package/shelljs 要给可以替代Unix下shell脚本的库. require('shelljs/global'); if ...
- python中各种转义字符
转义字符 描述 \(在行尾时) 续行符 \\ 反斜杠符号 \’ 单引号 \” 双引号 \a 响铃 \b 退格(Backspace) \e 转义 \000 空 \n 换行 \v 纵向制表符 \t 横向制 ...
- js 多张爆炸效果轮播图
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...