1

I am a bit of a beginner here. I am making an experimental back door for Linux that uses telnet to handle sockets. How can I use a variable in a string, I am used to python so I often would do something like this:

     var0 = "asdf"
     var1 = "I like "+var0+" movies"

But in C I am puzzled, because if I use this:

     system("telnet %i %p | bash | telnet %i %p", IP, PORT);

I get this when executed:

     telnet: could not resolve %i/%p: Servname not supported for ai_socktype

%i/%p??? Can somebody please explain this to me.

3
  • The dublicate is not what i am looking for. I will edit in another example. Commented Dec 8, 2013 at 23:11
  • Good stuff - just make sure to keep the question as specific as possible ;) Commented Dec 8, 2013 at 23:13
  • @user3081016 In order to make my answer more correct I need to know the types of IP and PORT can you show their declaration / definition? Commented Dec 8, 2013 at 23:21

1 Answer 1

3

You'll want to use snprintf:

char cmd[512];
snprintf(cmd, sizeof cmd, "telnet %i %p | bash | telnet %i %p", IP, PORT, IP, PORT);
system(cmd);

Though %i expects an int and %p will print the given parameter as an implementation-defined pointer-representation (and thus will expect a pointer, but will not print what it points to). You need the correct format-specifiers depending on the types of IP and PORT.

Note that the %i and %p & other %-modifiers don't work in every C-string, they are only processed by the *f functions (the 'f'-suffix stands for format(ted)). As you can see, system doesn't process them and interprets them literally.

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

1 Comment

@user3081016 Probably not though as telnet expects a host-name (not a mere number) and a (numeric) port (which the printed-pointer is not likely to form properly). You need to specify the correct %-modifiers that correspond to the types of IP and PORT.

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.