LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   SSh to some IPs in a script (https://www.linuxquestions.org/questions/programming-9/ssh-to-some-ips-in-a-script-4175497476/)

massy 03-08-2014 04:17 AM

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.

Smokey_justme 03-08-2014 04:21 AM

Is it key-based authentication or password?

massy 03-08-2014 04:38 AM

Quote:

Originally Posted by Smokey_justme (Post 5131007)
Is it key-based authentication or password?

password

Smokey_justme 03-08-2014 05:56 AM

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..

massy 03-08-2014 06:16 AM

Quote:

Originally Posted by Smokey_justme (Post 5131023)
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.

Smokey_justme 03-08-2014 06:51 AM

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..

massy 03-08-2014 07:06 AM

Quote:

Originally Posted by Smokey_justme (Post 5131041)
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

schneidz 03-08-2014 08:22 AM

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


massy 03-09-2014 03:02 AM

Quote:

Originally Posted by schneidz (Post 5131073)
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!!!

Smokey_justme 03-09-2014 04:25 AM

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)

massy 03-09-2014 05:14 AM

Quote:

Originally Posted by Smokey_justme (Post 5131407)
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

unSpawn 03-09-2014 06:47 AM

Quote:

Originally Posted by massy (Post 5131004)
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.

massy 03-09-2014 07:40 AM

Quote:

Originally Posted by unSpawn (Post 5131455)
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!

Sandcrawler 03-18-2014 06:18 AM

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


All times are GMT -5. The time now is 09:35 PM.