1

I'm getting close to the point of simply moving this back to PERL for a specific use case of editing files via SSH. It seems no matter what I do, additional line breaks are being placed in the file. Sample variable has the following:

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

Using the following command, I get the output below

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

Resulting in:

req_extensions = v3_req

prompt = no

[req_distinguished_name]

C = US

Edit adding relevant debug log:

<- 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

networkguy858
  • 159
  • 1
  • 8

2 Answers2

1

Just pass -n and it should fix that.

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

-n tell not to append a new line.

Shaharia Azam
  • 1,948
  • 19
  • 25
  • Same result as before – networkguy858 Jul 19 '18 at 19:20
  • 1
    According to http://linuxcommand.org/lc3_man_pages/echoh.html `echo` man page. `-n` should solve this. Though it worked for me. See my gist on https://gist.github.com/shahariaazam/d1d3b2c2af76ca2124eeecca814bc947 – Shaharia Azam Jul 19 '18 at 19:38
  • 1
    Also I found the similar question's solutions here at https://stackoverflow.com/a/38021361/1431786 – Shaharia Azam Jul 19 '18 at 19:40
  • Right - echo isn't the problem here, it's whatever phpseclib is doing with the "write" method that I believe to be the issue. I can take the same text and echo no problem via a native shell. – networkguy858 Jul 19 '18 at 19:40
  • 2
    You can use `exec()` method for that if the `write` method have any problem. Also what's the OS of your remote server? – Shaharia Azam Jul 19 '18 at 19:43
  • The server I'm running the commands on is FreeBSD, no matter what I do with `exec()`, it doesn't work. – networkguy858 Jul 19 '18 at 19:55
  • Would still like to know what `$file` is. You are certain the extra line breaks are not there? – ficuscr Jul 19 '18 at 20:17
  • Of course - no additional line breaks in $file. It's as I wrote at the start. – networkguy858 Jul 19 '18 at 20:22
  • I'll... I suppose... I guess I'll take your word for it :) Do wonder though about the 4 lines shown between the `q` and `p` in the debug output you shared. – ficuscr Jul 19 '18 at 20:29
  • Sorry to prolong this - just to add,`exec()` does actually put it in correctly, but my problem is I have an @ symbol in part of the text which is why `exec()` doesn't work for me. I can always use sed to fix this I suppose, was just hoping for a better way. – networkguy858 Jul 19 '18 at 20:51
  • 2
    Too beat the horse further.. Why not use `put`? – ficuscr Jul 19 '18 at 21:08
  • @ficuscr - I wanted to chain additional commands, I'm going that route. – networkguy858 Jul 19 '18 at 23:29
0

I'm not sure why write() was sending additional line breaks. I'm sure if I went line by line and appended to the file that would have worked. In the end, I simply used SFTP::put() which allowed me to place the @ that which wouldn't work with SSH::exec(). Thanks for all the input - it's a little different method than I would have liked, but it works.

networkguy858
  • 159
  • 1
  • 8