【问题标题】:Linux script for which needs to input password automatically and runs background (No expect and send) [closed]需要自动输入密码并在后台运行的Linux脚本(不期望和发送)[关闭]
【发布时间】:2017-12-09 01:00:58
【问题描述】:

我正在编写一个打算每天运行的脚本。脚本还包含一个命令,我需要在其中输入密码,每次执行时都说 abcd。有什么方法可以在询问“密码:”时自动输入密码 谢谢

【问题讨论】:

  • 不想用为什么还要加上expect?
  • 使用该命令是否有效:echo "my password" | ./some/command

标签: linux shell passwords


【解决方案1】:

实际上,这取决于脚本的结构。我想到了 2 个替代方案;

大多数(常用)CLI 可执行文件都有自己的参数集来提供密码/用户/凭据。让我以 MySQL 为例;

shell> mysql --host=localhost --user=myname --password=password mydb

如果您使用的命令不适合这样做;您可以考虑如下使用“期望”;

set password 123
spawn ssh "root@192.168.1.1"
expect "Password:"
send "$password\r";
interact

更多详情请见that link

【讨论】:

    【解决方案2】:

    使用xdotool 如果未安装,则首先使用 CLI 在您的系统中安装此工具

    apt-get install xdotool
    

    这里是简单的用法

    xdotool key Ctrl+Alt+t  # for opening termianl
    xdotool key Ctrl+Super+Up #super
    xdotool type "su" #typing su for root user
    xdotool key KP_Enter #pressing enter key
    

    检查您的键盘快捷键兼容性。希望有帮助。

    【讨论】:

      猜你喜欢
      • 2018-07-05
      • 1970-01-01
      • 2015-11-28
      • 2019-09-04
      • 1970-01-01
      • 2013-09-19
      • 2021-07-12
      • 1970-01-01
      • 2012-05-23
      相关资源
      最近更新 更多