开发者

script to ssh to a unix server

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开发者_如何学C username and password?


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


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.


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'


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

  • Basic ssh login question: could not able to spawn(ssh) using expect
  • How to interact with the server programattically after you have established the session: Expect Script to Send Different String Outputs
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜