linux - 如何编写在第一个命令后输入密码的 bash 脚本?

标签 linux bash

<分区>

Possible Duplicate:
Using expect to pass a password to ssh

我想通过 ssh 连接到远程机器,而不是使用 ssh 命令连同机器地址和密码,我只想编写一个小函数来对机器执行 ssh 命令并在服务器之后输入通行证问它。我可以编写 ssh 部分,但是我怎样才能使脚本在主机要求时也进入 pass 呢?

最佳答案

您可以使用expect 脚本。您可以从命令行传递参数。我写的示例代码:

#!/usr/bin/expect

set timeout 100
set host [lindex $argv 0]
set username [lindex $argv 1]
set password [lindex $argv 2]
set command [lindex $argv 3]

spawn ssh $username@$host $command
#puts $command
expect {
    "(yes/no)?"
        {
            send "yes\n"
            expect "*assword:" { send "$password\n"}
        }
    "*assword:"
        {
            send "$password\n"
        }
    }

关于linux - 如何编写在第一个命令后输入密码的 bash 脚本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13801513/

相关文章:

用于计算圈复杂度的Java工具

linux - 这个 Linux 汇编程序代码有什么问题?

python - 从 bash 脚本调用存储过程,ORA-01756 : quoted string not properly terminated

php - sh : ip: command not found centos

linux - 如何检查文件的查找命令列表,如果没有文件则退出

regex - bash 使用正则表达式查找不区分大小写

linux - 暂停 Linux 日期时间

python-3.x - 在 AWS 上运行 Python 脚本

c++ - 如何通过 Linux 的调度程序测量线程从繁忙的核心迁移的延迟?

linux - Bash 脚本错误