3

We have a command-line interface to logon to a system like so:

$ cli -u username -p password

In case if the wrong password is provided the message we get is " Credentials incorrect" and then it waits for the right password to be entered and doesn't quit. If the right password is provided, it doesn't provide any message and logs on directly.

I am looking to terminate the command in case the password is wrong, rather than waiting for the user input.

The idea is to look for the keyword Credentials and if it is found then kill the command:

$ cli -u username -p password | grep Credentials

How would I do that?

closed as unclear what you're asking by Sparhawk, msp9011, Shadur, schily, Jesse_b Aug 10 '18 at 15:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • first, it is better to ask who provides this UI.but if you lunch this UI from regular Linux terminal, there are general keyword binding is Ctrl+D, Ctrl+C. – Hossein Vatani Jul 24 '18 at 16:36
  • Are you trying to automate this, or do it manually? – Nick ODell Jul 24 '18 at 16:56
  • Are username and password static strings, or uniquely entered by the user? – Jeff Schaller Jul 24 '18 at 17:34
  • 1
    Are you looking for a wrapper around the cli command, or as Nick asked: something to clean these up after-the-fact? – Jeff Schaller Jul 24 '18 at 17:40
  • 1
    FYI, providing a password as an argument on the command line is a very bad idea. – Wildcard Jul 24 '18 at 18:05
0

Rather than killing the process, you can check something like tries-before-disconnect in the config file. Which I usually do to restrict ssh login attempts.

0

Background

I modeled your cli utility like so:

$ cat cli
#!/bin/bash

while IFS= read -r line; do
  if [ "$line" == "password" ]; then
    break
  else
    echo 'Credentials incorrect'
  fi
done

Using grep

You can then use grep -m1 ... to break out after the first match is found. The -m1 means stop after 1st match.

bad password
$ ./cli | grep -m 1 "Credential"
blah
Credentials incorrect

$
good password
$ ./cli | grep -m 1 "Credential"
password
$

Using sed

A similar approach use sed:

bad password
$ ./cli | sed '/Credential/ q'
blah
Credentials incorrect

$
good password
$ ./cli | sed '/Credential/ q'
password
$
  • The utility may be using standard error for messages to the user. – Kusalananda Jul 25 '18 at 6:53
  • @Kusalananda - true, this can be adapted to that scenario by changing the | to a |&. – slm Jul 25 '18 at 6:54

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