1

I have two files: server.sh and client.sh, the server sends the following text to the client

client.sh

porta_cliente=5678
porta_servidor=8765

nc -l $porta_cliente | sh

server.sh

porta_cliente=5678
porta_servidor=8765

nc $ip $porta_cliente <<-EOF
        dialog --stdout --backtitle 'Bem vindo ao SEPA 0.1' --title 'Menu inicial' --menu 'Escolha uma opção:' 0 0 0 \
        Cadastrar 'Criar um novo usuário' \
        Entrar 'Fazer login com sua conta' \
        Sair 'Encerrar o SEPA' > logservidor

        # WORKING FINE UNTIL HERE!

        case `cat logservidor` in
                "Cadastrar")
                        echo "LOG: case is being executed" > /dev/tcp/"$ip"/"$porta_servidor"
                        dialog --stdout --msgbox 'CADASTRAR' 0 0 > /dev/tcp/"$ip"/"$porta_servidor"
                ;;
                "Entrar")
                        echo "LOG: case is being executed"
                        echo "LOGIN SELECIONADO" > /dev/tcp/"$ip"/"$porta_servidor"
                ;;
                "Sair")
                        echo "LOG: case is being executed"
                        echo "SAIR SELECIONADO" > /dev/tcp/"$ip"/"$porta_servidor"
                ;;
        esac
EOF

As you can see, the server sends a text (interpreted as multiple lines of commands) to the client through netcat, on these commands, i'm trying to pass the user input to a specific file, and then, use a case statement to check if that user input is Cadastrar, Entrar or Sair, the first dialog part is working fine, but the case 'cat log servidor' in is not being executed (even with bash -x server.sh i couldn´t find what is causing this problem)

  • Of course bash -x server.sh won't give any errors as the server only pipes the text to the client. Copy the part between the EOF (the here-doc) and manually insert it on the client. Run it from there and you'll most propably find the error. – Stefan M Feb 14 '18 at 12:02
3

Your dialog command, whose output is redirected to the file logservidor, is being executed at the remote end. But the line that's supposed to read its output:

case `cat logservidor` in

...is being executed locally, while constructing the here-document. That's because here-documents perform various expansions, including command substitution, at the time they're defined.

You could delay this expansion (so that it only happens when the script is executed) by quoting EOF:

nc $ip $porta_cliente <<-"EOF"

But that would also delay expansion of variables in the script such as $ip. If you want those variables to be expanded immediately, but the cat command executed later, you could escape the backticks:

case \`cat logservidor\` in

You could also debug the problem yourself by looking at the final command before you try to send and execute it. Replace the first line with

cat > tmp-doc <<-EOF

Then look in tmp-doc to see what would have been sent over nc.

  • Thank you a lot @JigglyNaga, but when i try to put the dialog insidea variable, like menu=\$(dialog --stdout --msgbox 'Hello World!' 0 0) the case still not working, any workaround? – HenriqueHBR Feb 14 '18 at 12:30
  • @HenriqueHBR "still not working" is not a useful description. You'll have to update your question/start a new one to show exactly what you did and what happened. Did you try redirecting the here-doc into a temporary file first? – JigglyNaga Feb 14 '18 at 12:39
  • Already solved, i quoted every single $ and ' with \ – HenriqueHBR Feb 14 '18 at 12:52
  • @HenriqueHBR Just like I mentioned in the SO version of this question, merely quoting the tag will prevent any expansions. – PesaThe Feb 14 '18 at 14:05
  • @PesaThe In this case, it´s better to quote only some parts of the text, i was trying to quote everything, but it gave me some errors – HenriqueHBR Feb 14 '18 at 14:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.