LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 06-29-2013, 10:06 AM   #1
basusimply
LQ Newbie
 
Registered: Jun 2013
Posts: 7

Rep: Reputation: Disabled
Smile To write script that will do remote login to machineB from machineA


I wanna write a script which will do remote login to a machine,
If I use ssh command to do that, It asks something like 'do you wanna continue (yes/no)' and then remote machine's password, so
there we need to input yes and next input is remote machine's password. So my doubt is,
how to give those two input(yes and remote machine's password) to the system? Can I use the below command

echo 'yes abcd' | ssh --stdin user25@192.168.1.1

where 'abcd' is the password of 'user25'(user25 is an user on remote machine 192.168.1.1')

or I need to give input from file????

Last edited by basusimply; 06-29-2013 at 10:08 AM.
 
Old 06-29-2013, 11:28 AM   #2
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,505

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
read the HOWTO create ssh keys and possibly even the SSH Config

http://wiki.linuxquestions.org/wiki/...Administration

that will get you going down the correct way to script unattended ssh connections.
 
1 members found this post helpful.
Old 06-29-2013, 02:02 PM   #3
basusimply
LQ Newbie
 
Registered: Jun 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
Smile

Hiii....Thanks a lot for your reply.....I got some knowledge on 'ssh key'..... but

Suppose a command needs two argument to be inputted for its execution. so how to input those two arguments????
CAN I USE echo along with two arguments before pipe symbol like below example????
ex:- echo 'argument1 argument2' | command

If I can't, Is there any way to input 2 arguments(for a command) in a single line???????
 
Old 06-29-2013, 03:17 PM   #4
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,391

Rep: Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529Reputation: 2529
Quote:
Originally Posted by basusimply View Post
Hiii....Thanks a lot for your reply.....I got some knowledge on 'ssh key'..... but

Suppose a command needs two argument to be inputted for its execution. so how to input those two arguments????
CAN I USE echo along with two arguments before pipe symbol like below example????
ex:- echo 'argument1 argument2' | command
Why don't you just TRY IT and find out???? Any command you can type in can be run via SSH, so put as many arguments as you'd like.
Quote:
If I can't, Is there any way to input 2 arguments(for a command) in a single line???????
Yes, you can write a script to do it. You can use SSH, write an expect script to pass things one at a time, or use pure bash and echo statements. It's your choice.
 
Old 06-29-2013, 04:25 PM   #5
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,505

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
im not a huge fan of expect scripts, but if that is the only way then use it. my dislike for expect is that ive seen people use it to pass passwords between multiple systems. in other words plane text password in the wild. very dangerous and STUPID if you ask me.

TB0ne is really good as scripting as are several others around here. post what you have written inside "code" "/code" blocks (replace " " [ ] ) and we will be more then happy to help.
 
Old 09-26-2013, 07:50 AM   #6
basusimply
LQ Newbie
 
Registered: Jun 2013
Posts: 7

Original Poster
Rep: Reputation: Disabled
I got a solution for remotely logging in.

#!/usr/bin/expect -f
# ./sshlogin.exp uptime
# set Variables
set user username
set password yourpassword
set ipaddr serveripaddress
set arg1 [lrange $argv 0 0]
spawn ssh $user@$ipaddr $arg1
match_max 100000
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
expect eof

The above script will do remote login.But I'm not able to understand,
what is the need of setting of 'arg1' and can I write fifth line as
'spawn ssh $user@$ipaddr'. And whats the need of 6th line(match_max 100000).
 
Old 09-26-2013, 06:17 PM   #7
lleb
Senior Member
 
Registered: Dec 2005
Location: Florida
Distribution: CentOS/Fedora
Posts: 2,505

Rep: Reputation: 446Reputation: 446Reputation: 446Reputation: 446Reputation: 446
Quote:
Originally Posted by basusimply View Post
I got a solution for remotely logging in.

#!/usr/bin/expect -f
# ./sshlogin.exp uptime
# set Variables
set user username
set password yourpassword
set ipaddr serveripaddress
set arg1 [lrange $argv 0 0]
spawn ssh $user@$ipaddr $arg1
match_max 100000
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
expect eof

The above script will do remote login.But I'm not able to understand,
what is the need of setting of 'arg1' and can I write fifth line as
'spawn ssh $user@$ipaddr'. And whats the need of 6th line(match_max 100000).

http://wiki.linuxquestions.org/wiki/Scripting

read up on that.

in short $foo tells bash to read the value set earlier in the script by the following command: foo=value

also again please use the code flags. see my above post.
 
Old 09-26-2013, 07:23 PM   #8
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,721

Rep: Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975Reputation: 975
Quote:
Originally Posted by basusimply View Post
I got a solution for remotely logging in.

#!/usr/bin/expect -f
# ./sshlogin.exp uptime
# set Variables
set user username
set password yourpassword
set ipaddr serveripaddress
set arg1 [lrange $argv 0 0]
spawn ssh $user@$ipaddr $arg1
match_max 100000
expect "*?assword:*"
send -- "$password\r"
send -- "\r"
expect eof

The above script will do remote login.But I'm not able to understand,
what is the need of setting of 'arg1' and can I write fifth line as
'spawn ssh $user@$ipaddr'. And whats the need of 6th line(match_max 100000).
The second line of that script shows what those lines do. The script was written with the intention of passing in a command line argument to ssh. The argv and arg1 variables are grabbing the command line argument ("uptime", as the code was intended) and passing it into the ssh command.

Keep in mind that using an expect script with the password in plain-text is VERY insecure. I'm not going to say it's the wrong approach to password-less login, since it does have its place, but it should only be used for very unique situations. In 99% of cases you should be using SSH keys instead.

Last edited by suicidaleggroll; 09-26-2013 at 07:26 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Trying to write script that tests mysql login drigby Programming 3 04-09-2012 10:43 AM
How can I create a remote login script w/ options jsullivan44 Programming 1 08-11-2010 10:40 AM
How to write a script with auto login ssh and then run commands? keber314 Linux - Server 13 02-16-2009 03:37 PM
Is It Possible to write a script which login a remote server senthilvael Linux - Newbie 4 11-21-2007 06:23 PM
shell script for remote ssh login sefaklc Programming 5 08-28-2007 09:03 PM


All times are GMT -5. The time now is 04:32 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration