LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-25-2017, 06:15 PM   #1
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Rep: Reputation: 15
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
 
Old 05-25-2017, 06:27 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by gsr_kashyap View Post
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.
 
Old 05-25-2017, 06:34 PM   #3
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Original Poster
Rep: Reputation: 15
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.
 
Old 05-25-2017, 11:43 PM   #4
Turbocapitalist
LQ Guru
 
Registered: Apr 2005
Distribution: Linux Mint, Devuan, OpenBSD
Posts: 7,308
Blog Entries: 3

Rep: Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721Reputation: 3721
Quote:
Originally Posted by TB0ne View Post
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

Last edited by Turbocapitalist; 05-25-2017 at 11:44 PM.
 
4 members found this post helpful.
Old 05-26-2017, 02:20 AM   #5
Xeratul
Senior Member
 
Registered: Jun 2006
Location: UNIX
Distribution: FreeBSD
Posts: 2,657

Rep: Reputation: 255Reputation: 255Reputation: 255
Quote:
Originally Posted by gsr_kashyap View Post
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
 
Old 05-26-2017, 08:15 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by gsr_kashyap View Post
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.
 
2 members found this post helpful.
Old 05-26-2017, 04:27 PM   #7
gsr_kashyap
Member
 
Registered: Jul 2004
Location: india
Distribution: Ubuntu 14.04
Posts: 155

Original Poster
Rep: Reputation: 15
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
 
Old 05-28-2017, 02:26 PM   #8
Laserbeak
Member
 
Registered: Jan 2017
Location: Manhattan, NYC NY
Distribution: Mac OS X, iOS, Solaris
Posts: 508

Rep: Reputation: 143Reputation: 143
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.
 
Old 05-29-2017, 05:28 AM   #9
Kefijoo
LQ Newbie
 
Registered: May 2017
Posts: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by Laserbeak View Post
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?
 
Old 05-29-2017, 08:02 AM   #10
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,842

Rep: Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308Reputation: 7308
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.
 
1 members found this post helpful.
Old 05-29-2017, 08:17 AM   #11
Kefijoo
LQ Newbie
 
Registered: May 2017
Posts: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by pan64 View Post
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!
 
  


Reply

Tags
ssh login



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Auto SSH-login expect script creatorrr Linux - Software 7 10-26-2011 03:09 AM
ssh auto login script zerocool22 Linux - Server 4 05-13-2008 09:43 PM
ssh login script sriramsreedhars Linux - Enterprise 4 03-30-2007 09:51 AM
SSH login script codeb Linux - General 5 04-27-2006 10:11 AM
Script for SSH login CyberEE Linux - Networking 10 06-03-2002 08:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration