Thursday, July 3, 2014

SSH session automatic login and crontab usage for scheduling task based on particular time

  1. #!/usr/bin/expect -f
  2. # Expect script to supply root/admin password for remote ssh server
  3. # and execute command.
  4. # This script needs three argument to(s) connect to remote server:
  5. # password = Password of remote UNIX server, for root user.
  6. # ipaddr = IP Addreess of remote UNIX server, no hostname
  7. # scriptname = Path to remote script which will execute on remote server
  8. # For example:
  9. # ./sshlogin.exp password 192.168.1.11 who
  10. # ------------------------------------------------------------------------
  11. # Copyright (c) 2004 nixCraft project <http://cyberciti.biz/fb/>
  12. # This script is licensed under GNU GPL version 2.0 or above
  13. # -------------------------------------------------------------------------
  14. # This script is part of nixCraft shell script collection (NSSC)
  15. # Visit http://bash.cyberciti.biz/ for more information.
  16. # ----------------------------------------------------------------------
  17. # set Variables
  18. set password [lrange $argv 0 0]
  19. set ipaddr [lrange $argv 1 1]
  20. set scriptname [lrange $argv 2 2]
  21. set arg1 [lrange $argv 3 3]
  22. set timeout -1
  23. # now connect to remote UNIX box (ipaddr) with given script to execute
  24. spawn ssh root@$ipaddr $scriptname $arg1
  25. match_max 100000
  26. # Look for passwod prompt
  27. expect "*?assword:*"
  28. # Send password aka $password
  29. send -- "$password\r"
  30. # send blank line (\r) to make sure we get back to gui
  31. send -- "\r"
  32. expect eof
    With courtesy:
    http://bash.cyberciti.biz/security/expect-ssh-login-script/
     mkitab "lok:2:once:/home/lokesh/lok.sh > dev/console"

No comments:

Post a Comment