我已经通过执行以下操作创建了自定义 MOTD,以便在通过 SSH 登录时显示:
- 创建一个
/etc/usermotd/<username>
包含消息的文本文件 - 编辑用户的.bashrc文件并输入以下代码:
if [ -f /etc/usermotd/`whoami` ]; then cat /etc/usermotd/<username>; fi
它按照我想要的方式工作。然而...
我现在发现,当我尝试 SCP 某些东西时,它不起作用。我执行 scp 命令,它退出时没有给出任何进度条或说它已传输。当我执行 scp -vvv 时,我得到
debug1: Authentication succeeded (publickey).
debug2: fd 4 setting O_NONBLOCK
debug2: fd 5 setting O_NONBLOCK
debug1: channel 0: new [client-session]
debug3: ssh_session2_open: channel_new: 0
debug2: channel 0: send open
debug1: Requesting [email protected]
debug1: Entering interactive session.
debug2: callback start
debug2: client_session2_setup: id 0
debug1: Sending command: scp -v -t ~
debug2: channel 0: request exec confirm 1
debug2: fd 3 setting TCP_NODELAY
debug2: callback done
debug2: channel 0: open confirm rwindow 0 rmax 32768
debug2: channel 0: rcvd adjust 2097152
debug2: channel_input_status_confirm: type 99 id 0
debug2: exec request accepted on channel 0
WELCOME
debug2: channel 0: read<=0 rfd 4 len 0
debug2: channel 0: read failed
debug2: channel 0: close_read
debug2: channel 0: input open -> drain
debug2: channel 0: ibuf empty
debug2: channel 0: send eof
debug2: channel 0: input drain -> closed
BigBoss ~/.ssh:-$ debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
debug2: channel 0: rcvd eof
debug2: channel 0: output open -> drain
debug2: channel 0: obuf empty
debug2: channel 0: close_write
debug2: channel 0: output drain -> closed
debug2: channel 0: rcvd close
debug3: channel 0: will not send data after close
debug2: channel 0: almost dead
debug2: channel 0: gc: notify user
debug2: channel 0: gc: user detached
debug2: channel 0: send close
debug2: channel 0: is dead
debug2: channel 0: garbage collecting
debug1: channel 0: free: client-session, nchannels 1
debug3: channel 0: status: The following connections are open:
#0 client-session (t4 r0 i3/0 o3/0 fd -1/-1 cfd -1)
debug3: channel 0: close_fds r -1 w -1 e 6 c -1
debug1: fd 0 clearing O_NONBLOCK
debug1: fd 1 clearing O_NONBLOCK
Transferred: sent 2208, received 3352 bytes, in 0.2 seconds
Bytes per second: sent 13207.5, received 20050.5
debug1: Exit status 0
知道为什么发送失败吗?或者还有其他方法可以做到这一点吗?
答案1
它之所以无法工作,是因为 scp 期望远程端的行为完全“如此”,而不是尖叫着 WELCOME 回来。(它会如果它能给出错误信息就好了,但是……)。
使用sftp
(根本不会生成登录 shell)而不是scp
或者让你.bashrc
检测它是否处于交互式会话中。 似乎有几种方法可以做到这一点:
if [ -n "$PS1" ]; then
echo WELCOME
fi;
那里给出的另一种查看“$-”是否包含“i”的方法似乎有问题,因为在字符串比较中[
不起作用*
。但这应该有效(它检查以确保设置了 $-,然后检查删除“i”后的 $- 是否与 $- 相同。可能有更好的方法,但我一时想不起来)
if [ -n "$-" -a "${-/i}" != "$-" ]; then
echo WELCOME
fi;
答案2
如果希望 scp 正常工作,则不能生成终端输出。您需要做的是将任何输出生成内容(在我的例子中为 .cshrc)括起来,如下所示:
if ($?prompt != 0) then
cat /etc/motd
endif
因此,只有当有终端接收输出(即 ssh 登录)时才会生成输出。