关于ssh -n

来源:互联网 发布:工业软件有哪些 编辑:程序博客网 时间:2024/06/05 19:23
关于ssh -n,man 提示为:redirects stdin from /dev/null. This must be used when ssh is run in the background.
有人知道大概是什么意思吗?

因为我有这样一个问题:
  1. while read input
  2. do
  3. file=$(echo $input |awk '{print $1}')
  4. md5=$(echo $input | awk '{print $2}')

  5. scp /data/$file root@10.1.1.2:/data

  6. md5_curr=`ssh root@10.1.1.2 "md5sum /data/$file" | awk '{print $1}'`

  7. if [ $md5 != $md5_curr ];then
  8.   echo "MD5 Error"
  9. fi

  10. done < /data/a.txt
复制代码
  1. # cat /data/a.txt
  2. 1.txt asdfasdfasdfadsf
  3. 2.txt dafsadsfasfdagff
复制代码

这个脚本只能够scp 1.txt,但read 第二行时显示为空。

必须在ssh,上加上-n才可以read第二行。是不是stdin变了,不再是/data/a.txt??

 

------------------

因为连接没有断开
而-n会运行完毕后,自动断开连接

 

---------------------

 

ssh时,ssh从while里继承stdin为/data/a.txt,然后把值读取了,但这些值对它来说没意义,所以被忽略了,然后继续执行md5sum的命令。这时while再从/data/a.txt读时,已经为空。

-n 后设定ssh的stdin为/dev/null,这样就不会继承while的stdin???

 

-------------------

 

这就是原因,这个while重定向会先exec n<&0把标准输入copy一份存起来,然后exec 1<$servers_filename,然后开始循环,但你循环内部没有指定-n,ssh命令默认读标准输入就是把文件读了。

但我完全没发现ssh会读标准输入啊,不知道了。