0

I want to make a script (in bash language) that allows me to automatically log in to service X. Service X will ask for my username and password, and I want my script to input them. Here's the code:

Service X
#input username
#input password

Here is what I have to do manually:

root@loacalhost~$ Service X  
Service X: Username:  
Service X: Password:  
You have successfully logged in to service X.  
root@loacalhost~$

Here's what I want it to do:

root@loacalhost~$ ./script.sh
You have successfully logged in to service X.
root@loacalhost~$ 

How can I do this? I looked at redirection, STDIN, STDOUT, etc, but I wasn't really able to understand how to do this? I also looked at using echo to do it, but no luck either.

0

The question is vague, your Service X is a mystery, we know nothing about it.

Usually tools that read credentials from the terminal don't use stdin for this. The service may or may not use stdin. It may provide an option to read credentials from stdin. It may provide an option to read credentials from a file.

Note: from now on I will use servicex as the command because X in your Service X is an operand, so with options it should weirdly look like Service -a -b --optionc X. I doubt such syntax was your intention.

If servicex reads credentials from stdin by default, this should work:

printf '%s\n' "your username" "your password" | servicex

If it reads credentials from stdin only when told to do so, something like:

printf '%s\n' "your username" "your password" | servicex --credentials-from-stdin

In both cases it may be better to read credentials from a file that no other user can read:

<secret_file servicex --credentials-from-stdin

Or the service may provide an option to read credentials from a file (leaving stdin for another purpose or unused):

servicex --credentials-from-file secret_file

But most likely the service uses the terminal device directly to ask for credentials and to read them. In this case expect is the right tool. Compare this answer of mine or this one. A vague sketch of a solution to your vague problem may be:

expect -c '
 log_user 0
 spawn servicex
 expect "Service X: Username:"
 send "your username\n"
 expect "Service X: Password:"
 send "your password\n"
 interact
'

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.