Watch terminal for a particular prompt and automatically enter the response
I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?
In particular I would like to automatically populate the MFA token when prompted like below;
Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:
I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.
Requirements:
I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.
Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.
command-line zsh prompt
add a comment |
I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?
In particular I would like to automatically populate the MFA token when prompted like below;
Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:
I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.
Requirements:
I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.
Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.
command-line zsh prompt
3
Sounds like a task for expect.
– glenn jackman
Feb 27 at 16:05
Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.
– quat
Feb 28 at 22:05
add a comment |
I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?
In particular I would like to automatically populate the MFA token when prompted like below;
Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:
I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.
Requirements:
I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.
Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.
command-line zsh prompt
I am using ZSH and I am wondering whether there is a way of watching the stdout for a particular prompt and then automatically input the response?
In particular I would like to automatically populate the MFA token when prompted like below;
Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:
I can generate the token by the use of the http://soundly.me/oathplus/ tool but want to get this seamlessly working so I don't need to manually enter the token.
Requirements:
I would like to include the script (possibly using expect) in the ~/.zshrc file. This would be loaded once when starting the shell/ terminal and would then be waiting for the prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:". I will then use the terminal in a normal fashion and might eventually use a command that triggers the MFA token prompt.
Note, the MFA token prompt will not always pop up when executing the same command as it depends ono whether the previous connection is still valid. Furthermore, there might be days where I don't use the commands that would trigger the MFA token prompt but this script would still live in the background and jumps into action once there is actually a token prompt. Since the token itself would only be valid for a minute, it needs to be generated after the prompt popped up.
command-line zsh prompt
command-line zsh prompt
edited Mar 5 at 11:10
quat
asked Feb 27 at 16:03
quatquat
25638
25638
3
Sounds like a task for expect.
– glenn jackman
Feb 27 at 16:05
Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.
– quat
Feb 28 at 22:05
add a comment |
3
Sounds like a task for expect.
– glenn jackman
Feb 27 at 16:05
Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.
– quat
Feb 28 at 22:05
3
3
Sounds like a task for expect.
– glenn jackman
Feb 27 at 16:05
Sounds like a task for expect.
– glenn jackman
Feb 27 at 16:05
Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.
– quat
Feb 28 at 22:05
Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.
– quat
Feb 28 at 22:05
add a comment |
1 Answer
1
active
oldest
votes
The expect would be something like this:
expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'
That drops you into zsh, and you interact with it like a normal interactive shell.
The -o
watches for that prompt coming from the shell's output, and automatically sends the token.
Expect has a pretty narrow use case these days, but this is in the wheelhouse.
Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.
– quat
Mar 5 at 11:01
add a comment |
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%2f1409914%2fwatch-terminal-for-a-particular-prompt-and-automatically-enter-the-response%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 expect would be something like this:
expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'
That drops you into zsh, and you interact with it like a normal interactive shell.
The -o
watches for that prompt coming from the shell's output, and automatically sends the token.
Expect has a pretty narrow use case these days, but this is in the wheelhouse.
Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.
– quat
Mar 5 at 11:01
add a comment |
The expect would be something like this:
expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'
That drops you into zsh, and you interact with it like a normal interactive shell.
The -o
watches for that prompt coming from the shell's output, and automatically sends the token.
Expect has a pretty narrow use case these days, but this is in the wheelhouse.
Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.
– quat
Mar 5 at 11:01
add a comment |
The expect would be something like this:
expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'
That drops you into zsh, and you interact with it like a normal interactive shell.
The -o
watches for that prompt coming from the shell's output, and automatically sends the token.
Expect has a pretty narrow use case these days, but this is in the wheelhouse.
The expect would be something like this:
expect -c '
set mfa_token "your token here"
set mfa_prompt "Enter MFA code for arn:aws:iam::111111111111:mfa/jdoe:"
spawn zsh
interact {
-o $prompt {send_user $prompt; send "$mfa_promptr"}
}
'
That drops you into zsh, and you interact with it like a normal interactive shell.
The -o
watches for that prompt coming from the shell's output, and automatically sends the token.
Expect has a pretty narrow use case these days, but this is in the wheelhouse.
answered Feb 28 at 22:25
glenn jackmanglenn jackman
16.4k32645
16.4k32645
Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.
– quat
Mar 5 at 11:01
add a comment |
Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.
– quat
Mar 5 at 11:01
Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.
– quat
Mar 5 at 11:01
Thank you for your answer but it does not quite fit the use case. I will update the question to make it clearer.
– quat
Mar 5 at 11:01
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%2f1409914%2fwatch-terminal-for-a-particular-prompt-and-automatically-enter-the-response%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
3
Sounds like a task for expect.
– glenn jackman
Feb 27 at 16:05
Correct me if I'm wrong but I believe 'expect' is only useful if you know that you are going to execute the command and expect the prompt. My aim is to use the terminal for whichever commands and it might be days before I execute a command (without explicitly using 'expect') which produces this prompt.
– quat
Feb 28 at 22:05