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.

7
  • 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 Commented Mar 7, 2016 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 Commented Mar 7, 2016 at 13:45
  • 1
    echo "stuff" | ssh user@host "cat - >> file" Commented Mar 7, 2016 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 Commented Mar 7, 2016 at 13:48
  • selyunin , yes i have id_rsa.pub. Actully all VM are in a cloud space so provisioning is done Commented Mar 7, 2016 at 13:50

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.