1

It will be helpful if somebody could tell me how to connect to a unix server using username and password as arguments.my username and password is "anitha".

How can i create a shell script which automatically connect to my unix server with this username and password?

1
  • 2
    Connect? How do you want to connect? http? ssh? ...? If you want to open a ssh connection don't use name/passwd but ssh keys instead. Commented May 24, 2011 at 9:20

4 Answers 4

5

I guess you want to remotely connect to your *nix server from network. Base on my guess, to:

  • connect to remote *nix server, everybody is using SSH

    ssh anitha@anitha ip-to-unix-server

  • automatically connect, write simple bash shell wrap around your ssh connect command and do something, not suggested, you should use ssh password less login (aka public/private key)

    #!/usr/bin/env bash
    ip=172.16.0.1   #replace 172.16.0.1 with your unix server's ip
    username=anitha #your ssh username
    password=anitha #your ssh password
    command=who     #what do you want to do with remote server
    arguments=      #arguments for your command
    expect -c 'spawn ssh $username@$ip ; expect password ; send "$password\n" ; interact'
  • connect without typing password, you may need to use SSH password less login

Sign up to request clarification or add additional context in comments.

2 Comments

ssh $username@$password $ip $command $arguments won't work (using standard openssh at least) - the password will be interpreted as a hostname, the ip as a command, and the command as the first argument. passwordless login is the way to go.
@RobinGreen But even with passwordless login you still need to type the passphrase to use the private key, no ?
1

Use sshpass if you really need to use non-interactive keyboard-interactive authentication (pun intended) or better switch to using pubkey-based authentication.

Note that passing the password in clear to the ssh client is very lame as the password gets exposed in the publicly-readable process list where it can be read by anyone. sshpass works around this problem by creating a pseudo-terminal and communicating with the ssh client using it, so at least the password is not exposed at runtime.

Comments

1

Step 1:

jsmith@local-host$ [Note: You are on local-host here]

jsmith@local-host$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/jsmith/.ssh/id_rsa):[Enter key]
Enter passphrase (empty for no passphrase): [Press enter key]
Enter same passphrase again: [Pess enter key]
Your identification has been saved in /home/jsmith/.ssh/id_rsa.
Your public key has been saved in /home/jsmith/.ssh/id_rsa.pub.
The key fingerprint is:
33:b3:fe:af:95:95:18:11:31:d5:de:96:2f:f2:35:f9 jsmith@local-host

Step 2:
From Local-host, run this One liner for password less ssh connectivity.

cat ~/.ssh/id_dsa.pub | ssh useronanotherserver@anotherservername  'cat >> ~/.ssh/authorized_keys'

1 Comment

step2 : use ssh-copy-id anotherservername
0

You should use expect, which is an extension of tcl that was made specifically for automating login tasks.

4 Comments

No! Using passwords like that is significantly less secure than using RSA keys. Of course, once you've done that then you hardly need to use the complexity of expect unless you're automating something nasty on the remote end (which ls definitely isn't…)
@Donal, remember that not everyone is a system administrator; automation is often required in lab environments where password security is not relevant. Furthermore some devices (like those running Cisco IOS) cannot use RSA key authentication. This is a simple example meant to illustrate a concept. Add key authentication or whatever you like... then again, I know you are intimately familiar with Tcl, so "you" is whatever reader follows.
On most systems, adding an RSA key does not require any system privileges, just placing a public key in the right place (a one-time setup). Given that the result allows an admin to close off all password-based logins (so absolutely defending against distributed cracks) I'm surprised that so many still resist it.
Cisco routers though… well, Cisco are a bunch of unhung scum in this respect; they charge a fortune for allowing you to log in securely, as its only supported on their high-end kit. I could rant rather a lot on the topic, but I don't think that really belongs on SO. It's more of a thing for slashdot…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.