3

I'm writing a bash script that will connect to my remote machine then run some commands, one of them is vncserver :1, but this command need to input a password. How can I do it in my shell script? (I just need to run the script only, don't need to input the password)

This is my script:

ssh -i $pem -o StrictHostKeyChecking=no -o 'IdentitiesOnly yes' admin@$ip -f '
            pkill vnc ;
            vncserver :1 ;
            '
Vi Do
  • 81
  • 1
  • 6
  • 2
    Use [`vncpasswd`](http://linux.die.net/man/1/vncpasswd) instead... No need to store password in plain text.. – anishsane Feb 06 '15 at 12:09
  • I tried, but "The vncserver script runs vncpasswd the first time you start a VNC desktop". The console continue ask me to input the password. – Vi Do Feb 07 '15 at 01:17
  • `"vncviewer can also be given a password file to use via the -passwd option"` `vncpasswd` Does not supply password to `vncviewer`. `vncpasswd` is used to create a ticket/hash of your password. Then use vncviewer with the generated password file. Again, please check the man link I provided in previous comment. – anishsane Feb 09 '15 at 03:33
  • I don't ask about vncviewer. I was asked to input when start the vncserver which called vncpassword that asked me. Vncviewer is run at client. We are asked at server. (I found the solution as below). Thank you very much! – Vi Do Feb 09 '15 at 08:31
  • Oh, sorry... I misread the problem statement... – anishsane Feb 09 '15 at 09:01

1 Answers1

2

Thanks all, It's ok now:

ssh -i $pem -o StrictHostKeyChecking=no -o 'IdentitiesOnly yes' admin@$ip -f '
            pkill vnc ;
            expect -c "
            spawn vncserver :1;
            expect -nocase \"password:\" {
                send \"$pass\r\"; 
                expect -nocase \"Verify:\" {
                    send \"$pass\r\"; 
                    expect -nocase \"Would you like to enter a view-only password \(y\/n\)\?\" {
                        send \"n\r\"; 
                        expect eof }; }; interact } ;
            "
            '
Vi Do
  • 81
  • 1
  • 6