shell通过telnet调用http和smtp协议
来源:互联网 发布:淘宝开零食店无法上架 编辑:程序博客网 时间:2024/06/05 14:12
expect命令简介
Expect是一个免费的编程工具语言,用来实现自动和交互式任务进行通信,而无需人的干预。常用语ssh和telnet等需要进行人机交互的命令。
以前写过一个expect实现ssh跳转登录的例子:http://blog.csdn.net/koastal/article/details/52434314
基本结构
## 如果expect文件不存在,需要安装 yum install expect#!/usr/bin/expect## 设置脚本超时时间set timeout 30## 执行spawn之后的命令spawn ssh -l root 192.168.238.130## 如果执行结果包含expect之后的内容,则继续执行,否则退出expect "password:"## 模拟人工发送数据send "root\r"## 执行完成后保持交互状态,把控制权交给控制台,可以进一步人工操作interact
HTTP和SMTP的报文格式
具体报文格式参照,php通过sock实现http和smtp的文章:http://blog.csdn.net/koastal/article/details/53149689
shell发送HTTP请求
expect发送GET请求
基本格式
#!/usr/bin/expectset timeout 30spawn telnet 127.0.0.1 80expect "]"send "GET /demo.php?name=zhezhao&age=23 HTTP/1.1\r"send "Host: 127.0.0.1\r"send "Connection: close\r"send "\r\r"interact
expect发送POST请求
基本格式
#!/usr/bin/expectset timeout 30set data "name=zhezhao&age=23"set length [string length $data]spawn telnet 127.0.0.1 80expect "]"send "POST /demo.php HTTP/1.1\r"send "Host: 127.0.0.1\r"send "Content-type:application/x-www-form-urlencoded\r"send "Content-length:$length\r"send "Connection: close\r\r"send "$data\r"interact
shell模拟curl发送HTTP请求
## 发送GET请求./curl.sh "http://127.0.0.1/demo.php?name=zhezhao&age=23"## 发送POST请求./curl.sh -d "name=zhezhao&age=23" "http://127.0.0.1/demo.php"
因为使用except了执行脚本,所以需要使用Tcl语法编写代码(因为except基于Tcl)。
Tcl语法相关资料可以参考:
http://www.yiibai.com/tcl/
http://blog.chinaunix.net/uid-25324849-id-3191069.html
http://blog.sina.com.cn/s/blog_4b3c1f950102e4n5.html
http://blog.csdn.net/dulixin/article/details/2203908
#!/usr/bin/expectset timeout 30set tag [lindex $argv 0]set option "-d"if {$tag == $option} { set method POST set data [lindex $argv 1] set url [lindex $argv 2] set length [string length $data]} else { set method GET set url [lindex $argv 0] }set port 80set path /set tag [string last : $url]if {$tag != -1 && $tag != 4} { ## 用户手动给出了端口号 ## 通过: 进行分割 set arr [split $url ":"] if {[llength $arr] == 3} { set host [lindex $arr 1] set tmp [lindex $arr 2] } else { set host [lindex $arr 0] set tmp [lindex $arr 1] } set host [string trimleft $host "//"] ## 通过/ 进行分割 set arr [split $tmp "/"] for {set i 0} {$i < [llength $arr]} {incr i} { if {$i == 0} { set port [lindex $arr $i] } else { append path [lindex $arr $i] append path / } }} else { ## 通过: 进行分割 set tmp $url set arr [split $url ":"] if {[llength $arr] == 2} { set tmp [lindex $arr 1] } ## 通过/ 进行分割 set tmp [string trimleft $tmp "//"] set arr [split $tmp "/"] for {set i 0} {$i < [llength $arr]} {incr i} { if {$i == 0} { set host [lindex $arr $i] } else { append path [lindex $arr $i] append path / } }}## 校正pathif {[string length $path] > 1} { set path [string trimright $path "/"]} spawn telnet $host $portexpect "]"send "$method $path HTTP/1.1\r"send "Host: $host:$port\r"if {$method == "POST"} { send "Content-type:application/x-www-form-urlencoded\r" send "Content-length:$length\r"}send "Connection: close\r\r"if {$method == "POST"} { send "$data\r"}interact
shell通过实现smtp协议发送邮件
基本格式
#!/usr/bin/expectset timeout 60spawn telnet smtp.163.com 25expect "220"#和服务器打招呼send "HELO localhost\r"expect "250"#验证用户和密码send "AUTH LOGIN 发件人邮箱base64编码\r"expect "334"send "发件人邮箱密码base64编码\r"expect "235"#书写信封send "MAIL FROM:<发件人邮箱>\r"expect "250"send "RCPT TO:<收人邮箱>\r"expect "250"#书写新建内容send "DATA\r"expect "354"send "from:发件人邮箱\r"send "to:收人邮箱\r"send "subject:server error\r"send "MIME-Version:1.0\r"send "content-type:text/plain\r\r"send "let us become MI boy\r"send ".\r"expect "250"#邮件发送成功,断开连接send "qiut"
通用脚本
./mail.sh 12345@qq.com "代码如诗" "好事好事~"
#!/usr/bin/expectset mailto [lindex $argv 0] set subject [lindex $argv 1] set body [lindex $argv 2] set timeout 60spawn telnet smtp.163.com 25expect "220"#和服务器打招呼send "HELO localhost\r"expect "250"#验证用户和密码send "AUTH LOGIN 发件人邮箱base64编码\r"expect "334"send "发件人邮箱密码base64编码\r"expect "235"#书写信封send "MAIL FROM:<发件人邮箱>\r"expect "250"send "RCPT TO:<$mailto>\r"expect "250"#书写新建内容send "DATA\r"expect "354"send "from:发件人邮箱\r"send "to:$mailto\r"send "subject:$subject\r"send "MIME-Version:1.0\r"send "content-type:text/plain\r\r"send "$body\r"send ".\r"expect "250"#邮件发送成功,断开连接send "qiut"
python通过smtp发送邮件
#!/usr/bin/python# -*- coding: utf-8 -*-from email import encodersfrom email.header import Headerfrom email.mime.text import MIMETextimport smtplibfrom_addr = "XXX@163.com";password = "XXXX";to_addr = "XXXX@qq.com";smtp_server = "smtp.163.com";msg = MIMEText('<html><body><h1>Hello</h1>' + '<p>send by <a href="http://www.python.org">Python</a>...</p>' + '</body></html>', 'html', 'utf-8');msg['From'] = from_addr;msg['To'] = to_addr;msg['Subject'] = Header(u'来自SMTP的问候……', 'utf-8').encode();server = smtplib.SMTP(smtp_server, 25)server.set_debuglevel(1)server.login(from_addr, password)server.sendmail(from_addr, [to_addr], msg.as_string())server.quit()
参考文章:
http://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/001386832745198026a685614e7462fb57dbf733cc9f3ad000
0 0
- shell通过telnet调用http和smtp协议
- php通过socket调用http和smtp协议
- telnet调试SMTP协议
- 使用telnet协议来收发邮件(SMTP和POP3)
- Telnet通过Smtp和Pop3进行邮件操作
- telnet通过smtp发送邮件
- javaWeb笔记--Http协议和Telnet
- Telnet演示Http协议
- telnet 实现HTTP协议
- telnet 实现HTTP协议
- HTTP协议-telnet,httpwatch
- Telnet操作HTTP协议
- 邮件协议 (SMTP,POP3,HTTP)
- 协议http pop3 ftp SMTP
- 用telnet 测试Http协议
- 通过telnet手动输入HTTP
- http协议与SMTP协议的区别?
- Android--通过Http协议向web服务器发送XML数据和调用webService
- vue笔记。Demo 1
- Unity3D开发小贴士(九)序列化和反序列化Dictionary
- Poj 2388 Who's in the Middle 寻找第k大数
- 数据结构学习笔记1
- crack me160题 第十四题(序号130)phox.3 分析
- shell通过telnet调用http和smtp协议
- Vivado HLs中任意精度数据类型的处理方法
- onStartCommand返回值
- IE兼容实现HTML5的placeholder
- 子集生成算法 之 位向量法和增量构造法
- 413. Arithmetic Slices 【M】
- dwz框架dialog提交并刷新列表
- ue4学习记录-1-旋转门
- 【搜索】【NOIP2004】虫食算