Dan*_*ats 5 security bash shell sh
我希望我的脚本提示输入密码,但我只希望它每次执行一次天会议(假设是半小时)。是否可以在脚本执行之间安全地保存用户的登录凭据?我需要这是一个 bash 脚本,因为它必须在几种不同类型的 UNIX 上运行,而我无权在这些 UNIX 上安装任何东西。
我正在考虑加密一个文本文件,我将在其中写入登录凭据,但是我将在哪里保存该文件的密码?看来我只是重新创建了问题。
我知道运行加密脚本的实用程序,并且我非常反对使用它们,因为我不喜欢在脚本中保留主密码的想法,人们稍后可能需要调试。
编辑:这不是服务器登录脚本,而是通过我无法控制的 Web 服务器进行身份验证。
编辑 2:编辑会话持续时间
根据脚本的“多次调用”正在执行的操作,您可以使用 2 个脚本(服务器和客户端)来执行此操作,并使用命名管道进行通信。警告:这可能是不可移植的。
脚本1“服务器”:
#!/bin/bash
trigger_file=/tmp/trigger
read -s -p "Enter password: " password
echo
echo "Starting service"
mknod $trigger_file p
cmd=
while [ "$cmd" != "exit" ]; do
read cmd < $trigger_file
echo "received command: $cmd"
curl -u username:$password http://www.example.com/
done
rm $trigger_file
Run Code Online (Sandbox Code Playgroud)
脚本2“客户端”:
#!/bin/bash
trigger_file=/tmp/trigger
cmd=$1
echo "sending command: $cmd"
echo $cmd > $trigger_file
Run Code Online (Sandbox Code Playgroud)
跑步:
$ ./server
Enter password: .....
Starting service
received command: go
Run Code Online (Sandbox Code Playgroud)
其他窗口:
$ ./client go
sending command: go
Run Code Online (Sandbox Code Playgroud)
编辑:
这里是统一的自启动服务器/客户端版本。
#!/bin/bash
trigger_file=/tmp/trigger
cmd=$1
if [ -z "$cmd" ]; then
echo "usage: $0 cmd"
exit 1
fi
if [ "$cmd" = "server" ]; then
read -s password
echo "Starting service"
mknod $trigger_file p
cmd=
while [ "$cmd" != "exit" ]; do
read cmd < $trigger_file
echo "($$) received command $cmd (pass: $password)"
curl -u username:$password http://www.example.com/
done
echo exiting
rm $trigger_file
exit
elif [ ! -e $trigger_file ]; then
read -s -p "Enter password: " password
echo
echo $password | $0 server &
while [ ! -e $trigger_file ]; do
sleep 1
done
fi
echo "sending command: $cmd"
echo $cmd > $trigger_file
Run Code Online (Sandbox Code Playgroud)