0

I want to create a file on a remote server using ssh but I am not able to do so. I have 3 remote server and I am writing a bash script to create files on each of them.

The command I am using is:

`ssh $1@$2 > /etc/kafka/kafka_2.11-0.9.0.0/config/test.sh <<EOL
broker.id=$3
listeners=$4
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
log.cleaner.enable=false
zookeeper.connection.timeout.ms=6000
delete.topic.enable=true
ssl.client.auth=required
security.inter.broker.protocol=SSL

EOL`

but it does not seem to work.

Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56
Vishesh
  • 3,599
  • 4
  • 22
  • 36
  • as an alternative, consider creating a file on your machine and copy it with `scp myfile username@remotehost:/the/place/to/copy` to the remote host – selyunin Mar 07 '16 at 13:44
  • ya i thought of that option but was wondering why this is not working. And actually it is a properties file which is used to start the program so didn't wanted to keep changing it and then scp it – Vishesh Mar 07 '16 at 13:45
  • 1
    echo "stuff" | ssh user@host "cat - >> file" – aet Mar 07 '16 at 13:47
  • Do you use your snippet somewhere in your script? Do you have a password-free access to your remote host (i.e. `id_rsa.pub` copied to the remote server). Otherwise you probably need to authorize on the remote host and that's probably why you – selyunin Mar 07 '16 at 13:48
  • selyunin , yes i have id_rsa.pub. Actully all VM are in a cloud space so provisioning is done – Vishesh Mar 07 '16 at 13:50
  • @aet what aboout multiline stuff, it throws error – Vishesh Mar 07 '16 at 13:51
  • You could cat instead of echo, or anything really that feeds your text into stdin of the ssh process – aet Mar 07 '16 at 13:52

1 Answers1

2

The following code should work for you:

cat <<EOL | ssh $1@$2 'cat > /etc/kafka/kafka_2.11-0.9.0.0/config/test.sh'
broker.id=$3
listeners=$4
num.network.threads=3
num.io.threads=8
socket.send.buffer.bytes=102400
socket.receive.buffer.bytes=102400
socket.request.max.bytes=104857600
log.dirs=/tmp/kafka-logs
num.partitions=1
num.recovery.threads.per.data.dir=1
log.retention.hours=168
log.segment.bytes=1073741824
log.retention.check.interval.ms=300000
log.cleaner.enable=false
zookeeper.connection.timeout.ms=6000
delete.topic.enable=true
ssl.client.auth=required
security.inter.broker.protocol=SSL

EOL

The first cat command takes its input from the here document (provided by the shell which expands the positional parameters, $3 and $4) and its output is piped into the ssh command.

The cat > /etc/kafka/kafka_2.11-0.9.0.0/config/test.sh command is single-quoted as it should all be parsed as a single argument for the ssh command. If it’s not quoted, only the cat command by itself would be passed to the ssh command and the shell would parse the > as an instruction to redirect the output of the ssh command to a local file. This cat command runs on the remote server and gets its input from the ssh connection (if a filename isn’t specified as an operand for a cat command, it uses standard input instead).


The above command is actually a useless use of cat but I thought it might be easier for a shell-scripting newcomer to follow. You can actually shorten the first line by redirecting input for the ssh command directly from the here document:

ssh $1@$2 'cat > /etc/kafka/kafka_2.11-0.9.0.0/config/test.sh' <<EOL

This will work for Bash and other POSIX (standards-compliant) shells.

Community
  • 1
  • 1
Anthony Geoghegan
  • 11,533
  • 5
  • 49
  • 56