简体   繁体   English

密码是纯文本文件。 如何在linux中使用它们期望脚本?

[英]Passwords are in plain text file. How to use them in linux expect script?

I want to ssh to remote server using expect script. 我想使用expect脚本ssh到远程服务器。 Passwords are stored in a plain text. 密码以纯文本格式存储。 I want to send the password reading from the text file. 我想从文本文件中发送密码读取。 It is working fine in all other cases except where the password contains special characters like $ etc,. 它在所有其他情况下工作正常,除非密码包含像$ etc这样的特殊字符。 Using ssh keys is not an option now. 现在不能使用ssh键。 Hence, how can I make the password string acceptable before sending to a remote server. 因此,如何在发送到远程服务器之前使密码字符串可接受。 It is to be done within the expect script. 它将在期望脚本中完成。

To read a value from a plain text file that is stored in a file like this: 要从存储在如下文件中的纯文本文件中读取值:

# Here is a comment
Password: hunter2
# Here might be some other random stuff

Use something like this: 使用这样的东西:

set f [open ~/securedDirectory/theFile.txt]
while {[gets $f line] >= 0} {
    if {[regexp {^Password: (.*)$} $line -> password]} {
        break
    }
}
close $f

In the simplest case, you could just do set password [gets $f] but that would require that the password be the first entire line of the file. 在最简单的情况下,您可以set password [gets $f]但这需要密码是文件的第一行。 That's usually a poor configuration file! 这通常是一个糟糕的配置文件! Instead, when parsing text files it is best to define a simple format and use that. 相反,在解析文本文件时,最好定义一个简单的格式并使用它。

You then can use the password with: 然后,您可以使用密码:

send "$password\r"

at the right point. 在正确的点。 You should not need any extra quoting than that. 你不应该需要任何额外的报价。


Be aware that storing passwords in files is inherently insecure. 请注意,将密码存储在文件中本身就是不安全的。 (Not quite as bad as passing them with command line arguments or environment variables, but still not good.) Make sure you take steps to keep the file and the directory it contains as protected from outside interference as possible (turning off access by other users, etc.) Storing them encrypted in a file where the script can decrypt it when needed is an option, but not as secure as you might think as you have to have the code to do the decryption available where it can be read. (不像使用命令行参数或环境变量传递它们那么糟糕,但仍然不好。)确保您采取措施保护文件及其包含的目录尽可能免受外部干扰(关闭其他用户的访问权限)等等。将它们加密存储在脚本可以在需要时解密的文件中是一个选项,但不像你想象的那样安全,因为你必须让代码在可以读取的地方进行解密。 (This can be mitigated by making the script itself only readable by you, but then having an external file holding the password is no longer a big win.) (这可以通过使脚本本身只能被你读取来减轻,但是拥有一个保存密码的外部文件不再是一个大赢家。)

Using an SSH key is far better in practice. 在实践中使用SSH密钥要好得多。

Storing passwords in text files is not a good idea. 在文本文件中存储密码不是一个好主意。 They are not even stored on the server. 它们甚至没有存储在服务器上。 Storing them on the client is extremely vulnerable. 将它们存储在客户端是非常容易受到攻击的。

If you are using ssh to login, you may use range of other techniques: 如果您使用ssh登录,您可以使用其他一系列技术:

  1. Login using authorized private key with an empty passphrase. 使用具有空密码的授权私钥登录。 This is a security weakness of course, if anyone reaches the private key he can do the same. 当然,这是一个安全漏洞,如果有人到达私钥,他也可以这样做。 Good thing is that you can limit the use of the key to run only certain commands on the remote server . 好的是, 您可以限制密钥的使用,以便仅在远程服务器上运行某些命令

  2. Use ssh-agent . 使用ssh-agent This way you only enter the password once in a shell session, and subsequent calls will use it so you may call scripts without entering the password. 这样,您只需在shell会话中输入一次密码,后续调用将使用它,这样您就可以在不输入密码的情况下调用脚本。

Both these techniques are vulnerable in the sense that if someone reaches these scripts, he can do as these scripts do, but it will never be possible to get to know your password. 这些技术都是易受攻击的,因为如果有人到达这些脚本,他可以像这些脚本那样做,但是永远不可能知道你的密码。

You may want to start with some ssh tutorial . 您可能想要从一些ssh教程开始。

Encrypt your password using openssl. 使用openssl加密密码。

openssl enc -e -nosalt -out "OutputFilePath" -aes-256-cbc -pass pass:mySecretPass

then decrypt Output file and pass to your expect script at run time. 然后解密输出文件并在运行时传递给您的expect脚本。

cat OutputFilePath |    openssl enc -d -nosalt  -aes-256-cbc -pass pass:mySecretPass

Using this your password is encrypted stored in your Output File and in script you are decrypting output file. 使用此密码将加密存储在输出文件中,并在脚本中解密输出文件。

*It is not proper way because as key(mySecretPass) to decrypt your encrypted file is still stored in your script. *这不是正确的方法,因为解密加密文件的密钥(mySecretPass)仍然存储在您的脚本中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM