LinuxQuestions.org
Help answer threads with 0 replies.
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 03-08-2014, 04:17 AM   #1
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209
Blog Entries: 1

Rep: Reputation: Disabled
SSh to some IPs in a script


I used this code to ssh some IPs so that after any ssh and doing my commands I can ssh another IP.
Code:
while read IP
        do
            ssh -n $usr@$IP
        done <IPvar
It is able to ssh but it exit immediately! and the error is shown:
stdin: is not a tty
Pseudo-terminal will not be allocated because stdin is not a terminal.
 
Old 03-08-2014, 04:21 AM   #2
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
Is it key-based authentication or password?
 
Old 03-08-2014, 04:38 AM   #3
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Smokey_justme View Post
Is it key-based authentication or password?
password
 
Old 03-08-2014, 05:56 AM   #4
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
Well, you're using -n which teels ssh to go to the background (this doesn't work with passwords).. A great way is to use -f instead.. But in your situation, I wouldn't recommend any of them..
 
Old 03-08-2014, 06:16 AM   #5
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Smokey_justme View Post
Well, you're using -n which teels ssh to go to the background (this doesn't work with passwords).. A great way is to use -f instead.. But in your situation, I wouldn't recommend any of them..
I used -f and the error was shown:
Cannot fork into background without a command to execute.
 
Old 03-08-2014, 06:51 AM   #6
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
Yes, well, reading about the -f option was implied (I guess you did know what -n does!? don't you) .. Anyway, just don't use any options and it should work..
 
Old 03-08-2014, 07:06 AM   #7
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Smokey_justme View Post
Yes, well, reading about the -f option was implied (I guess you did know what -n does!? don't you) .. Anyway, just don't use any options and it should work..
The error is shown:
stdin: is not a tty
and I can't run my commands
 
Old 03-08-2014, 08:22 AM   #8
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
What are you trying to do ?
If all you need to do is log in remotely then

ssh user@host

should do it.
e.g.:
Code:
[schneidz@hyper ~]$ hostname
hyper
[schneidz@hyper ~]$ cat massy.ksh
#!/bin/bash

echo hello
ssh schneidz@mom
echo world
[schneidz@hyper ~]$ ./massy.ksh
hello
schneidz@mom's password: 
Last login: Thu Feb 20 10:46:22 2014 from hyper
[schneidz@mom ~]$ hostname
mom
[schneidz@mom ~]$ exit
logout
Connection to mom closed.
world

Last edited by schneidz; 03-08-2014 at 08:28 AM.
 
Old 03-09-2014, 03:02 AM   #9
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
What are you trying to do ?
If all you need to do is log in remotely then

ssh user@host

should do it.
e.g.:
Code:
[schneidz@hyper ~]$ hostname
hyper
[schneidz@hyper ~]$ cat massy.ksh
#!/bin/bash

echo hello
ssh schneidz@mom
echo world
[schneidz@hyper ~]$ ./massy.ksh
hello
schneidz@mom's password: 
Last login: Thu Feb 20 10:46:22 2014 from hyper
[schneidz@mom ~]$ hostname
mom
[schneidz@mom ~]$ exit
logout
Connection to mom closed.
world
Did you understand my question?
I need to ssh some IPs in loop and do some different commands!!!
 
Old 03-09-2014, 04:25 AM   #10
Smokey_justme
Member
 
Registered: Oct 2009
Distribution: Slackware
Posts: 534

Rep: Reputation: 203Reputation: 203Reputation: 203
How do you run your script? Make sure you do it from a console (/emulator)... Since it needs to ask your password, you need an actual console (that's what tty means)
 
Old 03-09-2014, 05:14 AM   #11
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by Smokey_justme View Post
How do you run your script? Make sure you do it from a console (/emulator)... Since it needs to ask your password, you need an actual console (that's what tty means)
I run it in putty
 
Old 03-09-2014, 06:47 AM   #12
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by massy View Post
I used this code to ssh some IPs so that after any ssh and doing my commands I can ssh another IP.
Your scripting skills don't seem to trump OTS tools dealing with ^whatever.on.*multiple hosts already in terms of dependability, efficiency, versatility so unless wheel reinvention is your thing you might want to look at like ClusterSSH, Dsh, Fanout / Fanterm, Tentakel, Shocto, SwitchTower, MUC, Parallel SSH, RGANG and such.
 
Old 03-09-2014, 07:40 AM   #13
massy
Member
 
Registered: Nov 2013
Distribution: CentOS 6.4
Posts: 209

Original Poster
Blog Entries: 1

Rep: Reputation: Disabled
Quote:
Originally Posted by unSpawn View Post
Your scripting skills don't seem to trump OTS tools dealing with ^whatever.on.*multiple hosts already in terms of dependability, efficiency, versatility so unless wheel reinvention is your thing you might want to look at like ClusterSSH, Dsh, Fanout / Fanterm, Tentakel, Shocto, SwitchTower, MUC, Parallel SSH, RGANG and such.
I don't want to run the same command on them, I only need to access them one by one!
 
Old 03-18-2014, 06:18 AM   #14
Sandcrawler
LQ Newbie
 
Registered: Feb 2014
Distribution: CentOS
Posts: 9

Rep: Reputation: Disabled
Code:
usr=me
while [ $usr != "exit" ]
        do
        read -e -p " # User: " usr
        read -e -p " # IP: " IP
                ssh $usr@$IP
        done
This will do following;

# ./do_stuff
# User: root
# IP: <Some IP>
root@<Some IP>'s password:

# exit
logout
Connection to <Some IP> closed.

# User: nagios
# IP: <Some IP>
nagios@<Some IP>'s password:

# exit
logout
Connection to <Some IP> closed.

# User: exit
# IP:
ssh: Could not resolve hostname : No address associated with hostname


Gives you some nice prompts as well

So what you can do is put whatever user/IP required to login then it will prompt with pass. Access box do whatever you want and on exit will be presented with user/IP selection again.


Seems a bit pointless as you are still typing the user/IP anyway it would take just as long to type unless you are automating passing in usr/IP:
ssh root@some_ip

Could default input on the reads with -i if you always going in as root for instance.
Code:
read -e -p " # User: " -i "root" usr      #(for example)
Hope this helps

Last edited by Sandcrawler; 03-18-2014 at 06:43 AM.
 
  


Reply



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
how to ssh multiple ips federico101 Linux - Newbie 7 09-24-2012 03:12 AM
how to allow IPs for ssh in SunOS nirmit Linux - Newbie 4 04-20-2009 02:45 AM
cURL: Server has many IPs, how would I make a cURL script use those IPs to send data? guest Programming 0 04-11-2009 11:42 AM
allow certain IPs to connect to ssh bytez Linux - Security 3 05-21-2007 09:15 AM
ssh using hostnames, not IPs shanenin Linux - Networking 3 04-11-2005 12:39 PM

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

All times are GMT -5. The time now is 12:09 AM.

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