how to make command "ps" don't show password in expect script?

Candice

I have make an example as below. The password(mingps)is the shell variable. When execute the shell script, in the mean while, execute command "ps -ef", I found the result of "ps" showed the password(mingps). For security reason, I don't want to show the password when execute command "ps -ef". So how to hide it? Thanks in advance.

#!/bin/sh
MalbanIP="XXX.XXX.XXX.XXX"
MalbanLogin="ming"
MalbanPwd="mingps"
MalbanCmd="netstat"
firstTime="true"

/usr/bin/expect <<EOF
set timeout 10
log_user 0
spawn /usr/bin/ssh $MalbanIP -l $MalbanLogin
expect {
    -nocase "continue connecting (yes/no)?" {
        send "yes\r"
        expect "password:" {
            send "$MalbanPwd\r"; set firstTime "false"; exp_continue
        }
    }
    "password" {
        if {$firstTime == "true"} {
            send "$MalbanPwd\r"; set firstTime "false"
        } else {
            log_user 1; puts stdout "password is wrong"; log_user 0;
            exit 1
            }
    }
}
expect "0-0-3"
log_user 1
send "$MalbanCmd \r"
set results \$expect_out(buffer)
expect "0-0-3" { send "exit\r" }
expect eof
EOF
exit 0
Donal Fellows

Option 1

The best way is to switch to using RSA keys to log in, as this will enable you to significantly strengthen your overall system security substantially. With that, you can probably avoid using Expect entirely.

Option 2

However, if you can't do that, the key to fixing things is to not pass it as either an argument or an environment variable (since ps can see both with the right options). Instead, you pass the password by writing it into a file and giving the name of that file to the Expect script. The file needs to be in a directory that only the current user can read; chmod go-rx will help there.

MalbanPwdFile=/home/malban/.securedDirectory/examplefile.txt
# Put this just before the spawn
set f [open $MalbanPwdFile]
set MalbanPwd [gets $f]
close $f

You might also need to put a backslash in front of the use of $MalbanPwd so that it doesn't get substituted by the shell script part too early.

Option 3

Or you could stop using that shell wrapper and do everything directly in Tcl/Expect.

#!/usr/bin/expect
set MalbanIP "XXX.XXX.XXX.XXX"
set MalbanLogin "ming"
set MalbanPwd "mingps"
set MalbanCmd "netstat"

set firstTime true
set timeout 10
log_user 0
spawn /usr/bin/ssh $MalbanIP -l $MalbanLogin
expect {
    -nocase "continue connecting (yes/no)?" {
        send "yes\r"
        expect "password:" {
            send "$MalbanPwd\r"
            set firstTime false
            exp_continue
        }
    }
    "password" {
        if {$firstTime} {
            send "$MalbanPwd\r"
            set firstTime false
        } else {
            log_user 1
            puts stdout "password is wrong"
            log_user 0
            exit 1
        }
    }
}
expect "0-0-3"
log_user 1
send "$MalbanCmd \r"
set results \$expect_out(buffer)
expect "0-0-3" { send "exit\r" }
expect eof

I suspect that this last option will work best for you in the longer term. It's definitely the simplest one (other than switching to RSA keys, which is what I've got deployed on my own infrastructure) and I think it is going to avoid some subtle bugs that you've got in your current code (due to substitution of variables at the wrong time).

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to make a script that launches bash command but don't open it?

From Dev

How to make ps command show memory in mb instead of kb?

From Dev

How to make ps command to show user, PID, terminal, CMD

From Dev

Not able to get output of "ps" command properly in expect script

From Dev

Expect script if command won't run

From Dev

Using 'expect' command to pass password to SSH running script remotely

From Dev

Expect Script - Auto Password

From Dev

How to make an expect script to expect either of multiple texts

From Dev

How to make an expect script to input commands into GDB?

From Dev

How to make an expect script to input commands into GDB?

From Dev

How to make a string as an optional to wait for it in an expect script?

From Dev

Don't show logs with PHP command and cURL

From Dev

Make LDAP search don't show DN

From Dev

How to show full command field with sort option in ps command?

From Dev

How to make password show asterisks as I type it when running `sudo` command?

From Dev

How to make password show asterisks as I type it when running `sudo` command?

From Dev

don't understand atquery command script

From Dev

Hide password using expect script

From Dev

Expect Script Special Character Password?

From Dev

Don't show password screen after using remote desktop

From Dev

How to make Ruby methods show what types they expect

From Dev

how to get exit status from command in expect script

From Dev

How to execute command under if statement in Shell Script under expect condition?

From Dev

FilterAggregation don't work as expect

From Dev

How to make kill <tab> show the processes that are shown by ps

From Dev

How to make kill <tab> show the processes that are shown by ps

From Dev

expect command in bash script failing

From Dev

Expect script to run shell command

From Dev

expect command in bash script failing

Related Related

  1. 1

    How to make a script that launches bash command but don't open it?

  2. 2

    How to make ps command show memory in mb instead of kb?

  3. 3

    How to make ps command to show user, PID, terminal, CMD

  4. 4

    Not able to get output of "ps" command properly in expect script

  5. 5

    Expect script if command won't run

  6. 6

    Using 'expect' command to pass password to SSH running script remotely

  7. 7

    Expect Script - Auto Password

  8. 8

    How to make an expect script to expect either of multiple texts

  9. 9

    How to make an expect script to input commands into GDB?

  10. 10

    How to make an expect script to input commands into GDB?

  11. 11

    How to make a string as an optional to wait for it in an expect script?

  12. 12

    Don't show logs with PHP command and cURL

  13. 13

    Make LDAP search don't show DN

  14. 14

    How to show full command field with sort option in ps command?

  15. 15

    How to make password show asterisks as I type it when running `sudo` command?

  16. 16

    How to make password show asterisks as I type it when running `sudo` command?

  17. 17

    don't understand atquery command script

  18. 18

    Hide password using expect script

  19. 19

    Expect Script Special Character Password?

  20. 20

    Don't show password screen after using remote desktop

  21. 21

    How to make Ruby methods show what types they expect

  22. 22

    how to get exit status from command in expect script

  23. 23

    How to execute command under if statement in Shell Script under expect condition?

  24. 24

    FilterAggregation don't work as expect

  25. 25

    How to make kill <tab> show the processes that are shown by ps

  26. 26

    How to make kill <tab> show the processes that are shown by ps

  27. 27

    expect command in bash script failing

  28. 28

    Expect script to run shell command

  29. 29

    expect command in bash script failing

HotTag

Archive