I have had a question for a while whether it possible for a Perl or Bash script to:
- log into another machine
- enter password
- run a few commands
- save output in a local file
How can I do it?
Thanks for the great site!
It's possible, but The Right Tool is expect. There are also expect-like libraries available for most scripting languages.
your question is tagged with perl, so i'll assume you're using that.
i would create ssh key pair* between your local and remote machine, this enables you to set up a secure connection w/o having to type in a password (very helpful for automated sessions). the link is to Google, because there are so many tutorials, and they vary slightly, so you should find one for your version of operating system.
once you're able to this, you can execute commands remotely
the output will be displayed on your local machine. you can then shell out from perl and execute this command, then save the output to a file, just as you would if you were executing a script on the local machine.
Here is a sample expect script which will automate your ssh (assuming) login Source : http://bash.cyberciti.biz/security/expect-ssh-login-script/
I tried this with expect, but it didn't work: it closed the connection at the end.
Can we run a script via ssh which will log into remote machines, run a command, and not disconnect?
So ssh in a machine, cd to such and such a directory, and then run a command, and stay logged in.