gpt4 book ai didi

linux - Bash 脚本 - Crontab 完成 bash 脚本的一部分但不是全部

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:21:18 24 4
gpt4 key购买 nike

下面的 bash 脚本在 L1 上每 10 分钟运行一次,脚本中的目的是生成一个随 secret 码,然后将其打印到一个文件中,然后将其复制到 L2。

#!/bin/bash
targetFile="testfilegen2.log"
address="192.168.1.1"
netmask="255.255.255.0"
channel="1"
essid="GreRPi"
random=$(pwgen 13 -1)
sudo echo "source-directory /etc/network/interfaces.d" > "$targetFile"
sudo echo "auto lo" >> "$targetFile"
sudo echo "auto wlan0" >> "$targetFile"
sudo echo "allow-hotplug wlan0" >> "$targetFile"
sudo echo "iface wlan0 inet static" >> "$targetFile"
sudo echo "address $address" >> "$targetFile"
sudo echo "netmask $netmask" >> "$targetFile"
sudo echo "wireless-channel $channel" >> "$targetFile"
sudo echo "wireless-essid $essid" >> "$targetFile"
sudo echo "wireless-mode ad-hoc" >> "$targetFile"
sudo echo "wireless-key s:$random" >> "$targetFile"
sleep 1
scp "$targetFile" pi@192.168.1.1:~/.ssh/
sudo service networking restart

Unfornaltey L1 似乎没有收到新密码,但 L2 收到了。

L1输出

pi@raspberrypi:/etc/greenwich $ cat testfilegen2.log
source-directory /etc/network/interfaces.d
auto lo
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
wireless-channel 1
wireless-essid GreRPi
wireless-mode ad-hoc
wireless-key s:she5Bie1ojoon

L2输出

pi@raspberrypi:~/.ssh $ cat testfilegen2.log
source-directory /etc/network/interfaces.d
auto lo
auto wlan0
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.1.1
netmask 255.255.255.0
wireless-channel 1
wireless-essid GreRPi
wireless-mode ad-hoc
wireless-key s:ez2eeCeich0oc

有什么建议吗?

谢谢

最佳答案

正如 Vikas Tiwari 所指出的,重定向发生在当前用户 before sudo 运行时。您需要使用 sudo 打开文件。这可以通过 tee 程序来完成,您只需运行该程序一次

targetFile="testfilegen2.log"
address="192.168.1.1"
netmask="255.255.255.0"
channel="1"
essid="GreRPi"
random=$(pwgen 13 -1)
{
echo "source-directory /etc/network/interfaces.d"
echo "auto lo"
echo "auto wlan0"
echo "allow-hotplug wlan0"
echo "iface wlan0 inet static"
echo "address $address"
echo "netmask $netmask"
echo "wireless-channel $channel"
echo "wireless-essid $essid"
echo "wireless-mode ad-hoc"
echo "wireless-key s:$random"
} | sudo tee "$targetFile" > /dev/null
sleep 1
scp "$targetFile" pi@192.168.1.1:~/.ssh/
sudo service networking restart

关于linux - Bash 脚本 - Crontab 完成 bash 脚本的一部分但不是全部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42469438/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com