当我们尝试以下面的方式访问远程主机服务器时,它要求我输入密码。
]# ssh remotehost
Enter passphrase for key '/root/.ssh/id_rsa':
Enter passphrase for key '/root/.ssh/id_rsa':
Enter passphrase for key '/root/.ssh/id_rsa':
Password:
Last login: Tue Apr 25 05:54:32 2017 from pm.remothost
Copyright (c) 2001-2014 EMC Corporation. All Rights Reserved.
Copyright (c) 1992-2011 The FreeBSD Project.
Copyright (c) 1979, 1980, 1983, 1986, 1988, 1989, 1991, 1992, 1993, 1994
The Regents of the University of California. All rights reserved.
remotehost-1#
有没有办法在sshpass
或expect
等ssh命令中提供密码。
在连接remotehost之前,有一个.ssh / config文件可以访问2个jumphost。
我们可以在提示时执行可以预期/提供密码的内容,然后我们会登录。
以下是我们正在尝试但不起作用的内容。它给出了密码短语3次,但是当输入最后一个密码时,它就会出现,并且本地机器中没有创建文件。
#!/usr/bin/env expect
set timeout 10
spawn ssh ip < ./ai.py >> stinfo_2017.txt
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Password:"
send "54321\r"
interact
运行脚本时,输出就是这样。
[root@centseven ~]# ./directconnect
spawn ssh ip < ./ai.py >> stinfo_2017.txt
Enter passphrase for key '/root/.ssh/id_rsa':
Enter passphrase for key '/root/.ssh/id_rsa':
Enter passphrase for key '/root/.ssh/id_rsa':
Password:
zsh: no such file or directory: ./ai.py
Killed by signal 1.
Killed by signal 1.
[root@centseven ~]#
脚本未在远程服务器上执行,因此没有创建文件stinfo_2017.txt。
当我们手动运行它时,它会创建一个文件。
答案 0 :(得分:0)
产卵线出现问题。
#!/usr/bin/env expect
set timeout 10
spawn sc -h "ssh ip < ./ai.py >> stinfo_2017.txt"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Enter passphrase for key '/root/.ssh/id_rsa':"
send "12345\r"
expect "Password:"
send "54321\r"
interact