You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PLINK命令执行正常但未生成目标文件的问题求助

Let's figure out why your test.txt file isn't showing up and fix this step by step:

1. Check the "Hidden" File Location (Relative Path Gotcha)

Your test.sh uses echo $NAME "test" >> test.txt—that's a relative path. When you run the script via Plink, the working directory isn't automatically /var/www/test/ (where test.sh lives). Instead, it defaults to the remote user's home directory (like /home/<user>/).

First, check if the file was created there by running this on your Linux server:

ls ~/test.txt

If it exists, update test.sh to use an absolute path for the output file to ensure it lands where you expect:

# Replace relative path with absolute path in test.sh
echo $NAME "test" >> /var/www/test/test.txt

2. Verify Directory Permissions

Even with sudo, the /var/www/test/ directory might not allow writing. Check its permissions with this command on the Linux server:

ls -ld /var/www/test/

If the directory doesn't have write permissions for the root user (which sudo runs as), fix it:

sudo chmod 755 /var/www/test/
# Or add group write access if needed
sudo chmod g+w /var/www/test/

Also, make sure test.sh itself is executable:

sudo chmod +x /var/www/test/test.sh

3. Capture Error Output to Diagnose Failures

Right now, you can't see if the command is throwing errors on the Linux side. Modify your Plink command to redirect stderr to stdout so you can see exactly what's going wrong:

start C:\tools\PLINK.EXE -ssh -pw <password> -t <user>@10.111.11.111 "echo <password> | sudo -S /var/www/test/test.sh 2>&1"

This will show permission errors, path issues, or sudo failures directly in the Plink window.

4. Fix the Execution Context

Sometimes sudo -S has issues with piped passwords (especially if the remote system has requiretty enabled in /etc/sudoers). Try running the script in its own directory explicitly:

start C:\tools\PLINK.EXE -ssh -pw <password> -t <user>@10.111.11.111 "echo <password> | sudo -S sh -c 'cd /var/www/test && ./test.sh' 2>&1"

This ensures relative paths in test.sh work as expected, while capturing errors to help debug.

5. Bonus: Stop Hardcoding Passwords (Security Best Practice)

Hardcoding passwords in batch scripts is risky. Instead, set up passwordless SSH for your user, then allow passwordless sudo for the specific script by editing /etc/sudoers on the Linux server (use visudo to edit safely):

visudo
# Add this line (replace <user> with your remote username)
<user> ALL=(ALL) NOPASSWD: /var/www/test/test.sh

Your Plink command simplifies to this much safer version:

start C:\tools\PLINK.EXE -ssh -t <user>@10.111.11.111 "sudo /var/www/test/test.sh"

内容的提问来源于stack exchange,提问作者DVLanleyC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 07:09:07