PHPSeclib使用写法的其他换行符

时间:2018-07-19 18:22:14

标签: php phpseclib

对于通过SSH编辑文件的特定用例,我已经将其简单地移回PERL了。看来不管我做什么,都会在文件中放置其他换行符。示例变量具有以下内容:

req_extensions = v3_req
prompt = no
[req_distinguished_name]
C = US

使用以下命令,我得到下面的输出

$ssh->write("echo '$file' > /directory/test.txt\n");

结果:

req_extensions = v3_req

prompt = no

[req_distinguished_name]

C = US

编辑添加相关的调试日志:

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0022, network: 0s)
00000000  00:00:00:02:00:00:00:01:65                       ........e

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0005, network: 0.0001s)
00000000  00:00:00:02:00:00:00:01:63                       ........c

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0005, network: 0s)
00000000  00:00:00:02:00:00:00:01:68                       ........h

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0003, network: 0s)
00000000  00:00:00:02:00:00:00:01:6f                       ........o

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0005, network: 0.0003s)
00000000  00:00:00:02:00:00:00:01:20                       ........ 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0004, network: 0s)
00000000  00:00:00:02:00:00:00:01:27                       ........'

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0.0001s)
00000000  00:00:00:02:00:00:00:01:72                       ........r

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:65                       ........e

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0003, network: 0.0002s)
00000000  00:00:00:02:00:00:00:01:71                       ........q

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:5f                       ........_

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:65                       ........e

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0007, network: 0.0006s)
00000000  00:00:00:02:00:00:00:01:78                       ........x

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0003, network: 0s)
00000000  00:00:00:02:00:00:00:01:74                       ........t

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0.0001s)
00000000  00:00:00:02:00:00:00:01:65                       ........e

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0003, network: 0.0002s)
00000000  00:00:00:02:00:00:00:01:6e                       ........n

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0s)
00000000  00:00:00:02:00:00:00:01:73                       ........s

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:69                       ........i

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0005, network: 0.0002s)
00000000  00:00:00:02:00:00:00:01:6f                       ........o

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0005, network: 0s)
00000000  00:00:00:02:00:00:00:01:6e                       ........n

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:73                       ........s

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:20                       ........ 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0004, network: 0s)
00000000  00:00:00:02:00:00:00:01:3d                       ........=

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:20                       ........ 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0s)
00000000  00:00:00:02:00:00:00:01:76                       ........v

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:33                       ........3

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0s)
00000000  00:00:00:02:00:00:00:01:5f                       ........_

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0s)
00000000  00:00:00:02:00:00:00:01:72                       ........r

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:65                       ........e

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0.0001s)
00000000  00:00:00:02:00:00:00:01:71                       ........q

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:02:0d:0a                    ..........

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:02:3e:20                    ........> 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0005, network: 0.0004s)
00000000  00:00:00:02:00:00:00:02:0d:0a                    ..........

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:02:3e:20                    ........> 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:70                       ........p

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:72                       ........r

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:6f                       ........o

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:6d                       ........m

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0.0001s)
00000000  00:00:00:02:00:00:00:01:70                       ........p

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0s)
00000000  00:00:00:02:00:00:00:01:74                       ........t

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:20                       ........ 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0s)
00000000  00:00:00:02:00:00:00:01:3d                       ........=

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:20                       ........ 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0.0002s)
00000000  00:00:00:02:00:00:00:01:6e                       ........n

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:01:6f                       ........o

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0002, network: 0.0002s)
00000000  00:00:00:02:00:00:00:02:0d:0a                    ..........

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:02:3e:20                    ........> 

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:02:0d:0a                    ..........

<- NET_SSH2_MSG_CHANNEL_DATA (since last: 0.0001, network: 0s)
00000000  00:00:00:02:00:00:00:02:3e:20                    ........> 

TIA

2 个答案:

答案 0 :(得分:1)

只需通过 $ssh->write("echo -n '$file' > /directory/test.txt\n"); ,它就可以解决此问题。

-n

{{1}}告诉不要添加新行。

答案 1 :(得分:0)

我不确定write()为什么要发送其他换行符。我确定我是否一行一行地添加到本来可以工作的文件上。最后,我只使用了SFTP::put(),就可以放置@不能与SSH::exec()一起使用的内容。感谢您的所有输入-这与我想要的方法有点不同,但是可以。