在shell脚本中利用expect实现自己主动应答
測试脚本(已验证,来自于http://forum.ubuntu.org.cn/ntopic21611.html):
要交互的脚本(talk.sh)例如以下:
#!/bin/bash
echo "Who are you?"
read who
echo "Hello,$who"
echo "Are you happy?"
read answer
echo "why?"
read answer
实现自己主动应答的脚本auto.sh例如以下:
#!/bin/bash
expect<<- END
spawn ./talk.sh
expect "who"
send "firefly "
expect "happy?"
send "Yes,I am happy. "
expect "why?"
send "Because it worked! "
expect eof
exit
END
运行auto.sh后能够看到自己主动交互例如以下:
spawn ./talk.sh
Who are you?
firefly
Hello,firefly
Are you happy?
Yes,I am happy.
why?
Because it worked!
眼下仅仅用到了expect最主要的使用方法,只是对用脚本实现自己主动化已经非常实用了
-------------------------------------------------------------------------------------------------
expect 实现su root:
#!/usr/bin/expect
#created by neilzhao of linpus corp.
set passwd 111111
spawn su
expect "Password:"
send "$passwd "
interact
自己主动ssh登录的几种方法
自己主动ssh登录的几种方法 1. 自己主动ssh/scp方法==
A为本地主机(即用于控制其它主机的机器) ;
B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;
A和B的系统都是Linux
在A上执行命令:
# ssh-keygen -t rsa (连续三次回车,即在本地生成了公钥和私钥,不设置password)
# ssh "mkdir .ssh" (须要输入password)
# scp ~/.ssh/id_rsa.pub :.ssh/id_rsa.pub (须要输入password)
在B上的命令:
# touch /root/.ssh/authorized_keys (假设已经存在这个文件, 跳过这条)
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys (将id_rsa.pub的内容追加到authorized_keys 中)
回到A机器:
# ssh (不须要password, 登录成功)
2. 控制n个机器如上所述自己主动登录
那就须要n对钥匙(密钥和公钥), ssh-keygen 命令能够任意更改钥匙对的名字, 比方:
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_192.168.60.110
这样私钥和公钥的名字分别就是: id_rsa_192.168.60.110和 id_rsa_192.168.60.110.pub;然后将 id_rsa_192.168.60.110.pub 文件的内容, 追加到sever的 ~/.ssh/authorized_keys文件里,最后, 在本地用ssh命令的 -i 參数指定本地密钥, 并登录:
# ssh -i /root/.ssh/id_rsa_192.168.60.110
scp也是一样的
# scp -i /root/.ssh/id_rsa_192.168.60.110 filename :/home/someone
在文件.bashrc中加下两行,每次做相同的操作就不用敲入这样长的命令了:
alias sshcell='ssh -i /root/.ssh/id_rsa_192.168.60.110 '
alias scpcell='scp -i /root/.ssh/id_rsa_192.168.60.110 filename :/home/someone'
这样,直接键入一下指令实现ssh和scp自己主动登录:
# sshcell
# scpcell
3. 自己主动ssh/scp脚本
假设须要从A,到B,然后才可以到C,那么须要ssh和scp两次,是比較麻烦的。
ssh自己主动登录:
#!/usr/bin/expect -f
set timeout 30
spawn ssh weiqiong@B
expect "password:"
send "pppppp "
expect "]*"
send "ssh weiqiong@C "
expect "password:"
send "pppppp "
interact
scp从A复制文件到C:
#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn scp $file weiqiong@B:/home/weiqiong
expect "password:"
send "pppppp "
expect "]*"
spawn ssh weiqiong@B
expect "password:"
send "pppppp "
expect "]*"
send "scp $file weiqiong@C:/home/weiqiong "
expect "password:"
send "pppppp "
expect "]*"
exit
interact
scp从C复制文件到A:
#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn ssh weiqiong@B
expect "password:"
send "pppppp "
expect "]*"
send "scp weiqiong@C:/home/weiqiong/$file . "
expect "password:"
send "pppppp "
expect "]*"
send "exit "
expect "]*"
spawn scp weiqiong@B:/home/weiqiong/$file .
expect "password:"
send "pppppp "
interact
4. 建立ssh/scp通道
比方说我的机器是A,中间server为B,目标server是C<br>
从A能够ssh到B,从B能够ssh到C,可是A不能直接ssh到C<br>
如今展示利用ssh通道技术从A直接传输文件到C<br>
1. ssh -L1234:C:22 userid@B<br>
input B's password<br>
(1234是本机A的空暇port,该指令须要A机器上的root用户权限,实际上是在本机1234port建立了一个通道)<br>
2. 打开一个新的console,键入:<br>
scp -P1234 filename userid@localhost:<br>
input C's password
要交互的脚本(talk.sh)例如以下:
#!/bin/bash
echo "Who are you?"
read who
echo "Hello,$who"
echo "Are you happy?"
read answer
echo "why?"
read answer
实现自己主动应答的脚本auto.sh例如以下:
#!/bin/bash
expect<<- END
spawn ./talk.sh
expect "who"
send "firefly "
expect "happy?"
send "Yes,I am happy. "
expect "why?"
send "Because it worked! "
expect eof
exit
END
运行auto.sh后能够看到自己主动交互例如以下:
spawn ./talk.sh
Who are you?
firefly
Hello,firefly
Are you happy?
Yes,I am happy.
why?
Because it worked!
眼下仅仅用到了expect最主要的使用方法,只是对用脚本实现自己主动化已经非常实用了
-------------------------------------------------------------------------------------------------
expect 实现su root:
#!/usr/bin/expect
#created by neilzhao of linpus corp.
set passwd 111111
spawn su
expect "Password:"
send "$passwd "
interact
自己主动ssh登录的几种方法
自己主动ssh登录的几种方法 1. 自己主动ssh/scp方法==
A为本地主机(即用于控制其它主机的机器) ;
B为远程主机(即被控制的机器Server), 假如ip为192.168.60.110;
A和B的系统都是Linux
在A上执行命令:
# ssh-keygen -t rsa (连续三次回车,即在本地生成了公钥和私钥,不设置password)
# ssh "mkdir .ssh" (须要输入password)
# scp ~/.ssh/id_rsa.pub :.ssh/id_rsa.pub (须要输入password)
在B上的命令:
# touch /root/.ssh/authorized_keys (假设已经存在这个文件, 跳过这条)
# cat /root/.ssh/id_rsa.pub >> /root/.ssh/authorized_keys (将id_rsa.pub的内容追加到authorized_keys 中)
回到A机器:
# ssh (不须要password, 登录成功)
2. 控制n个机器如上所述自己主动登录
那就须要n对钥匙(密钥和公钥), ssh-keygen 命令能够任意更改钥匙对的名字, 比方:
# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa_192.168.60.110
这样私钥和公钥的名字分别就是: id_rsa_192.168.60.110和 id_rsa_192.168.60.110.pub;然后将 id_rsa_192.168.60.110.pub 文件的内容, 追加到sever的 ~/.ssh/authorized_keys文件里,最后, 在本地用ssh命令的 -i 參数指定本地密钥, 并登录:
# ssh -i /root/.ssh/id_rsa_192.168.60.110
scp也是一样的
# scp -i /root/.ssh/id_rsa_192.168.60.110 filename :/home/someone
在文件.bashrc中加下两行,每次做相同的操作就不用敲入这样长的命令了:
alias sshcell='ssh -i /root/.ssh/id_rsa_192.168.60.110 '
alias scpcell='scp -i /root/.ssh/id_rsa_192.168.60.110 filename :/home/someone'
这样,直接键入一下指令实现ssh和scp自己主动登录:
# sshcell
# scpcell
3. 自己主动ssh/scp脚本
假设须要从A,到B,然后才可以到C,那么须要ssh和scp两次,是比較麻烦的。
ssh自己主动登录:
#!/usr/bin/expect -f
set timeout 30
spawn ssh weiqiong@B
expect "password:"
send "pppppp "
expect "]*"
send "ssh weiqiong@C "
expect "password:"
send "pppppp "
interact
scp从A复制文件到C:
#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn scp $file weiqiong@B:/home/weiqiong
expect "password:"
send "pppppp "
expect "]*"
spawn ssh weiqiong@B
expect "password:"
send "pppppp "
expect "]*"
send "scp $file weiqiong@C:/home/weiqiong "
expect "password:"
send "pppppp "
expect "]*"
exit
interact
scp从C复制文件到A:
#!/usr/bin/expect -f
set timeout 300
set file [lindex $argv 0]
spawn ssh weiqiong@B
expect "password:"
send "pppppp "
expect "]*"
send "scp weiqiong@C:/home/weiqiong/$file . "
expect "password:"
send "pppppp "
expect "]*"
send "exit "
expect "]*"
spawn scp weiqiong@B:/home/weiqiong/$file .
expect "password:"
send "pppppp "
interact
4. 建立ssh/scp通道
比方说我的机器是A,中间server为B,目标server是C<br>
从A能够ssh到B,从B能够ssh到C,可是A不能直接ssh到C<br>
如今展示利用ssh通道技术从A直接传输文件到C<br>
1. ssh -L1234:C:22 userid@B<br>
input B's password<br>
(1234是本机A的空暇port,该指令须要A机器上的root用户权限,实际上是在本机1234port建立了一个通道)<br>
2. 打开一个新的console,键入:<br>
scp -P1234 filename userid@localhost:<br>
input C's password
原文地址 http://bbs.linuxpk.com/viewthread.php?tid=9327