LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   ssh login using a script (https://www.linuxquestions.org/questions/programming-9/ssh-login-using-a-script-4175606671/)

gsr_kashyap 05-25-2017 06:15 PM

ssh login using a script
 
Hi All,
I am trying to run the below command in a bash script.

ssh -t username0@hostname < a.txt ls >> ./$now.txt

where a.txt contains the password in plain text
I was getting below error:
Pseudo-terminal will not be allocated because stdin is not a terminal. error
after a bit of googling I have added -t still it is asking for password.


Need help in resolving this.

Thanks in advance

TB0ne 05-25-2017 06:27 PM

Quote:

Originally Posted by gsr_kashyap (Post 5715229)
Hi All,
I am trying to run the below command in a bash script.

ssh -t username0@hostname < a.txt ls >> ./$now.txt

where a.txt contains the password in plain text
I was getting below error:
Pseudo-terminal will not be allocated because stdin is not a terminal. error
after a bit of googling I have added -t still it is asking for password.

Read the man page on SSH, and pay particular attention to the "SSH_ASKPASS" environment variable. And is that your entire 'script'?? Also, I bolded a line...are you seriously putting a password in a clear-text file for SSH? Why on earth would you do that???

A far better way to go would be to do a keyswap between the two systems, and just be able to run "ssh user@host", and have it function properly, with decent security.

gsr_kashyap 05-25-2017 06:34 PM

Thanks TNOne for the quick reply
will check on "SSH_ASKPASS" thanks for the hint.
thats not all on the script there were more things though I was stuck on this.

Turbocapitalist 05-25-2017 11:43 PM

Quote:

Originally Posted by TB0ne (Post 5715231)
A far better way to go would be to do a keyswap between the two systems, and just be able to run "ssh user@host", and have it function properly, with decent security.

I'll comment too since this method cannot be emphasized enough.

gsr_kashyap, take a look at how to use key-based authentication. You choice at the moment is really either RSA or Ed25519 algorithms for the key pair. The steps, minus some details, are:
  1. generate the key pair using either algorithm, pay attention to the options -f and -C in the helper application ssh-keygen
  2. copy the public key to the remote machine you will log in to
  3. keep the private key on the client machine you will log in from
  4. look at the -i option for the SSH client
  5. look closely at the manual pages for the helper applications ssh-agent and ssh-add, they will allow you to automate your activities while still keeping a strong passphrase on the private key

Xeratul 05-26-2017 02:20 AM

Quote:

Originally Posted by gsr_kashyap (Post 5715229)
Hi All,
I am trying to run the below command in a bash script.

ssh -t username0@hostname < a.txt ls >> ./$now.txt

where a.txt contains the password in plain text
I was getting below error:
Pseudo-terminal will not be allocated because stdin is not a terminal. error
after a bit of googling I have added -t still it is asking for password.


Need help in resolving this.

Thanks in advance

there are several website to store the key + password into the config of ssh.
storing key is important prior

TB0ne 05-26-2017 08:15 AM

Quote:

Originally Posted by gsr_kashyap (Post 5715234)
Thanks TNOne for the quick reply
will check on "SSH_ASKPASS" thanks for the hint.
thats not all on the script there were more things though I was stuck on this.

So then post your entire script if you'd like help. Hard to offer any good advice based on a single line, without knowing the context or your actual goal. Storing a password ANYWHERE (in a script, a file called by a script, etc.), is just a horrible thing to do. And in this day and age, totally pointless, ESPECIALLY since you're using SSH. There is even a command called ssh-copy-id which works like this:
Code:

ssh-copy-id user@remotehost
That's it. If you're logged in as the user you want to have passwordless access, that's all you have to do. It'll prompt you for a password once, and never again. You store no passwords anywhere, and things are secure.

gsr_kashyap 05-26-2017 04:27 PM

Thanks guys for your worthful advise and suggestions.
I have used ssh-keygen and ssh-copy-id remotehost into all my servers
the script is working fine

Hence marking it as solved

Laserbeak 05-28-2017 02:26 PM

You can hardcode the password in, but it much safer to setup keys to allow ssh to connect to that machine/username without a password. Then it only works with that one machine/username, no one can grab your password and use it from anywhere.

Kefijoo 05-29-2017 05:28 AM

Quote:

Originally Posted by Laserbeak (Post 5716275)
You can hardcode the password in, but it much safer to setup keys to allow ssh to connect to that machine/username without a password. Then it only works with that one machine/username, no one can grab your password and use it from anywhere.

I have a additional questions regarding SSH keys:
If you connect with SSH keys and run commands which require sudo privileges, how do you do this? With a password, you would run the sudo command and enter a pwd for more privileges, but how to do this in a script?

pan64 05-29-2017 08:02 AM

please open a new thread, do not hide earlier issues. As you see noone interested, because this thread is actually closed.
Also you may click on REPORT and asks moderators to help you to.

Kefijoo 05-29-2017 08:17 AM

Quote:

Originally Posted by pan64 (Post 5716503)
please open a new thread, do not hide earlier issues. As you see noone interested, because this thread is actually closed.
Also you may click on REPORT and asks moderators to help you to.

OK thanks!


All times are GMT -5. The time now is 11:25 PM.