在Windows 7中,当运行使用open
通过ssh与远程Linux PC交互的Tcl脚本时,可以看到远程PC的提示。
代码示例:
set fh [open "|./plink -ssh $user@$ip -pw $password" w+]
catch {fconfigure $fh -blocking 0 -buffering none -translation binary} output
set i 0
while {1} {
puts -nonewline stdout "iter $i. "
set data [read -nonewline $fh]
puts -nonewline $data
if {$i > 3} {
puts $fh "ls -l\r"
}
after 1000
incr i
}
具有以下输出(ls
的某些输出被修剪以提高可读性):
C:\test>tclsh openInWindows.tcl
iter 0. iter 1. Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.5.0-45-generic i686)
* Documentation: https://help.ubuntu.com/
99 packages can be updated.
62 updates are security updates.
Last login: Mon Apr 7 13:17:20 2014 from 1.2.3.4
←]0;root@MyPC: ~root@MyPC:~# iter 2. iter 3. iter 4. iter 5. ls -l
total 1876
drwxr-xr-x 2 root root 4096 Apr 3 13:34 ←[01;34mDesktop←[0m
drwxr-xr-x 2 root root 4096 Apr 3 13:34 ←[01;34mDocuments←[0m
drwxr-xr-x 3 root root 4096 Apr 3 13:34 ←[01;34mDownloads←[0m
drwxr-xr-x 2 root root 4096 Apr 3 13:34 ←[01;34mMusic←[0m
drwxr-xr-x 2 root root 4096 Apr 3 13:34 ←[01;34mPictures←[0m
drwxr-xr-x 2 root root 4096 Apr 3 13:34 ←[01;34mPublic←[0m
drwxr-xr-x 2 root root 4096 Apr 3 13:34 ←[01;34mTemplates←[0m
drwxr-xr-x 2 root root 4096 Apr 3 13:34 ←[01;34mVideos←[0m
←]0;root@MyPC: ~root@MyPC:~# ^C
C:\test>
但是当在Linux中运行相同的脚本(稍作修改)时,远程PC的提示不会出现(尽管它仍然可以执行命令并获得结果)。
Linux的代码:
set fh [open "|sshpass -p $password ssh $user@$ip" w+]
catch {fconfigure $fh -blocking 0 -buffering none -translation binary} output
set i 0
while {1} {
puts -nonewline stdout "iter $i. "
set data [read -nonewline $fh]
puts -nonewline $data
if {$i > 3} {
puts $fh "ls -l"
}
after 1000
incr i
}
具有以下输出(ls
的某些输出被修剪以提高可读性):
root@MyUbuntu:~# tclsh openInLinux.tcl
iter 0. iter 1. Welcome to Ubuntu 12.04.4 LTS (GNU/Linux 3.5.0-45-generic i686)
* Documentation: https://help.ubuntu.com/
99 packages can be updated.
62 updates are security updates.
iter 2. iter 3. iter 4. iter 5. total 1876
drwxr-xr-x 2 root root 4096 Apr 3 13:34 Desktop
drwxr-xr-x 2 root root 4096 Apr 3 13:34 Documents
drwxr-xr-x 3 root root 4096 Apr 3 13:34 Downloads
drwxr-xr-x 2 root root 4096 Apr 3 13:34 Music
drwxr-xr-x 2 root root 4096 Apr 3 13:34 Pictures
drwxr-xr-x 2 root root 4096 Apr 3 13:34 Public
drwxr-xr-x 2 root root 4096 Apr 3 13:34 Templates
drwxr-xr-x 2 root root 4096 Apr 3 13:34 Videos^C
root@MyUbuntu:~#
(我必须尽快点击CTRL + C,否则ls
命令将被执行多次)
为什么Linux和Windows之间存在这种差异? 当此代码在Linux下运行时,是否可以显示远程PC的提示?