Bash Script That Inputs Strings Into a Command After Executing it
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.
linux bash script shell-script
add a comment |
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.
linux bash script shell-script
add a comment |
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.
linux bash script shell-script
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.
linux bash script shell-script
linux bash script shell-script
asked Dec 30 '18 at 9:19
Dash ConroyDash Conroy
228
228
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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 '%sn' "your username" "your password" | servicex
If it reads credentials from stdin only when told to do so, something like:
printf '%sn' "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 usernamen"
expect "Service X: Password:"
send "your passwordn"
interact
'
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "3"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1388921%2fbash-script-that-inputs-strings-into-a-command-after-executing-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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 '%sn' "your username" "your password" | servicex
If it reads credentials from stdin only when told to do so, something like:
printf '%sn' "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 usernamen"
expect "Service X: Password:"
send "your passwordn"
interact
'
add a comment |
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 '%sn' "your username" "your password" | servicex
If it reads credentials from stdin only when told to do so, something like:
printf '%sn' "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 usernamen"
expect "Service X: Password:"
send "your passwordn"
interact
'
add a comment |
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 '%sn' "your username" "your password" | servicex
If it reads credentials from stdin only when told to do so, something like:
printf '%sn' "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 usernamen"
expect "Service X: Password:"
send "your passwordn"
interact
'
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 '%sn' "your username" "your password" | servicex
If it reads credentials from stdin only when told to do so, something like:
printf '%sn' "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 usernamen"
expect "Service X: Password:"
send "your passwordn"
interact
'
answered Dec 30 '18 at 11:03
Kamil MaciorowskiKamil Maciorowski
26.1k155680
26.1k155680
add a comment |
add a comment |
Thanks for contributing an answer to Super User!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fsuperuser.com%2fquestions%2f1388921%2fbash-script-that-inputs-strings-into-a-command-after-executing-it%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown