ssh非交互式密码授权(一):expect自动输入密码、命令
来源:互联网 发布:万能邮件群发软件 编辑:程序博客网 时间:2024/05/16 14:29
转:https://www.chenyudong.com/archives/expect-non-interactive-ssh-login-password-authentication.html
目录 [hide]
- 1 安装expect
- 2 使用expect脚本运行
- 3 使用命令行参数运行expect
- 4 expect与scp使用
- 5 python中使用expect命令
- 6 其他的相关知识
本文有还有2篇类似的文章:
- ssh非交互式密码授权(二):sshpass让ssh支持password参数
- ssh非交互式密码授权(三):使用密钥进行认证
Linux ssh下执行ssh命令远程登录其他机器,总是需要输入密码,如果人工去登录,输入密码那还可以,但是让程序自动化登录远程ssh服务器,并执行命令着就比较麻烦了。
Linux下有个程序是expect
,它可以模拟键盘,输入文本。
安装expect
机器上一般是没有这个命令的,需要安装expect
1
2
sudo apt-get install expect
yum install expect
经常看到一些脚本有会expect、spawn、send关键字,这几个关键字都是在expect程序里面使用的。
使用expect脚本运行
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
#!/usr/bin/expect
set
timeout 5
password=ppp
spawn
ssh
user@localhost -p 22
expect {
"(yes/no)"
{ send
"yes\r"
; exp_continue }
"password:"
{ send
"$password\r"
}
}
expect user@* {send \"
ls
-l \r\" } ;
expect user@* { send
exit
\r } ;
expect 100% ;
expect eof ;
- 注意第一行使用的是
#!/usr/bin/expect
而不是普通的bash脚本那样 - expect都是使用
{}
,且{、}
使用时,前后需要留空格 - 例子使用花括号,表示使用一组并列表达式,只要其中一项符合,就会执行该项,类似switch
使用命令行参数运行expect
有时候写个expect脚本比较麻烦,直接只用命令行参数去执行命令,或者嵌套在shell脚本、python脚本中,这样可以减少expect脚本的数量
一下是一个登录并执行一个ls命令的demo:
01
02
03
04
05
06
07
08
09
10
11
expect -c "
set
timeout 1;
spawn
ssh
user@localhost -p 22 ;
expect {
yes
/no
{ send \"
yes
\r\"; exp_continue }
*assword* { send \"password\r\" }
} ;
expect user@* {send \"
ls
-l \r\" } ;
expect user@* { send
exit
\r } ;
expect eof ;
"
expect的参数-c
后面是字符串,里面就相当于脚本里面的内容了。
- 用
;分号
隔开,可以在同一行里。 - 里面的
"
引号,使用\"
来代替。 - 第9行的exit最好要有。退出ssh,这样程序不会阻塞。我如果没有使用,在这个shell命令执行完,无法立刻输入字符。
- expect脚本必须以interact或expect eof结束,执行自动化任务通常expect eof就够了
expect与scp使用
expect一般用于登录ssh服务器,除了ssh命令输入密码外,还有scp也是需要密码输入的,因此expect与scp结合起来使用的场景也是很多的。
01
02
03
04
05
06
07
08
09
10
11
12
expect -c "
set
timeout 1;
spawn
scp
-P 22 user@remoteHost:
/tmp/file
.txt ~/ ;
expect {
yes
/no
{ send \"
yes
\r\"; exp_continue }
*assword* { send \"password\r\" }
} ;
expect user@* {send \"
ls
-l \r\" } ;
expect user@* { send
exit
\r } ;
expect 100% ;
expect eof ;
"
注意:scp的制定端口使用大写的-P
。
python中使用expect命令
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
SCP_CMD_BASE
=
r
"""
expect -c "
set timeout 1;
spawn scp -P {port} {username}@{host}:/tmp/{{filename}} ./proxydownload/ ;
expect *assword* {{{{ send {password}\r }}}} ;
expect *\r
expect \r
expect eof
"
"""
.
format
(username
=
username,password
=
password,host
=
host,port
=
port)
cmd
=
SCP_CMD_BASE.
format
(filename
=
filename)
print
"cmd="
, cmd
p
=
subprocess.Popen( cmd , stdout
=
subprocess.PIPE, stderr
=
subprocess.PIPE, shell
=
True
)
(output, err)
=
p.communicate()
其他的相关知识
Python对expect也有类库支持,也就是说你可以直接使用python编写expect,而不用安装expect程序了。可以执行搜索一下python pexpect
。
用expect速度会比较慢,因为需要等待返回的数据,然后输入命令执行。没有ssh密钥登录的快速。
0 0
- ssh非交互式密码授权(一):expect自动输入密码、命令
- 自动输入密码、命令 expect
- 使用expect自动输入ssh密码
- sudo非交互式输入密码
- expect实现scp ssh自动输入密码登录
- expect实现自动输入密码
- ssh自动输入密码登录服务器/ssh免输入密码登录/非交互ssh 密码验证
- ssh自动输入密码登录服务器/ssh免输入密码登录/非交互ssh 密码验证
- ssh自动输入密码登录服务器/ssh免输入密码登录/非交互ssh 密码验证
- ssh自动输入密码登录服务器/ssh免输入密码登录/非交互ssh 密码验证
- expect实现脚本中ssh密码输入
- expect实现交互式密码修改
- 脚本ssh连接自动填密码(expect)
- ssh免密码自动登陆 expect
- 通过expect实现scp密码自动输入
- ssh 非交互免输入密码自动登陆设置
- 通过shell配合expect自动输入账号密码登陆ssh或ftp
- 用expect 输入密码
- 全排列
- c++用引用实现俩个数的交换
- 【LeetCode】3. Longest Substring Without Repeating Characters
- Python基础知识
- 二元多项式
- ssh非交互式密码授权(一):expect自动输入密码、命令
- 微信小程序参考资料
- Memcache的原子性操作
- 离线环境下使用Maven打包Storm程序
- 如何理解超键、候选键、主键、外键的含义?
- varnish cdn 推送平台搭建
- Nginx在Linux中的命令行
- RecyclerView的版本要和appcompat 的版本不一致 引发的错误
- redis常用命令及高级应用之键值的相关命令