0

I have a local windows machine, where I connect to a local linux box through putty, and I will have to login to multiple linux servers using ssh, through sudo.

I was wondering if there was a windows .bat script, that would help me in getting this done.

I normally login to server as follows:

#ssh [email protected] -v -p2345
#sudo su -

And I was not able to find any links from ServerFault, to get this automated. Not sure if had overlooked or missed some .. :-)

Please let me know if there is anybody who would have done this , and if so how (would appreciate if someone could provide me with the .bat script).

Cheers

3 Answers 3

1

If there's something you need to do on every system you log into, you could configure and distribute a .bashrc that would automate your login tasks.

That said, don't automate sudo-ing to root. You're applying godlike powers to yourself; you should at least be typing a password to remind you to be careful. It's a best practice, and in our shop a policy, that you never sudo su to root; everything you need to do should be handled through individual sudo commands. That way you only access privilege when you need it; and all your privileged actions are logged, courtesy of sudo, improving security and occasionally answering the question "What the hell happened on this system?"

2
  • As stated above, I will have only the following info, because this is NOT my server.Hostname, IP address, username and password. I do have a .bat script that opens my putty connecting to my local linux box, as follows : "start C:putty -ssh [email protected] 22 -pw xxxpassxxx". So , basically I would like to have some thing like the above mentioned .bat script created in on my Windows PC.
    – Ajov Crowe
    Mar 25, 2011 at 5:04
  • Well, at the risk of sounding trite, you could just make a batch file which contains a line like you've described for each system you want to log into. You won't be able to script the sudo command, though; once you're into an interactive shell, you can't pass additional commands. Mar 25, 2011 at 5:19
0

Do you want to run the same groups of operations repetitively? You might want to check out Fabric, which is a cool Python library to go the extra distance and iteratively run crap on computers after SSH'ing into them. Otherwise, if you just want to pull up a ton of prompts with ssh logins, I would suggest the following.

  1. Setup SSH keys (I assume this is accessible to you). This is the easiest tutorial I know that explains this with PuTTY.

  2. Set up a batch file like so. You can use the PuTTY and cut out the middle man if you are cool with that. (WARNING: this is just a rough guess; I do not have a Windows computer in front of me, but have done things like this in the past.)

    SET PAGEANT=C:\path\to\pageant.exe

    SET PUTTY=C:\path\to\putty.exe

    SET PUTTYPRIVKEY=C:\path\to\priv\key.ppk

    SET USER=someuser

    SET RPORT=2345

    SET HOSTS=141.161.1.1 141.161.1.2 141.161.1.3 host1.example.com host2.example.com

    REM # Activate the private key, this assumes you password-protected it wisely.

    "%PAGEANT%" "%PUTTYPRIVKEY%"

    FOR %%H in (%HOSTS%) do (

    ECHO.

    ECHO Attempting to SSH to host %%H . . .

    ECHO.

    START "%PUTTY%" -ssh %%H:%RPORT% -2 -l %USER% -i "%PUTTYPRIVKEY%"

    )

Also a possibility is the -m parameter of PuTTY, which according to the docs, runs a script against the remote host once authenticated. With the right keys and agent forwarding (-a or -A; ask if you don't know), this might get you down to a button click or too.

2
  • As stated above, I will have only the following info, because this is NOT my server.Hostname, IP address, username and password. I do have a .bat script that opens my putty connecting to my local linux box, as follows : "start C:putty -ssh [email protected] 22 -pw xxxpassxxx". So , basically I would like to have some thing like the above mentioned .bat script created in on my Windows PC.
    – Ajov Crowe
    Mar 25, 2011 at 5:15
  • Sorry, my bad. Was kind of loopy apparently; I try to re-read the question after finishing my answer. Apparently, not all the time. So, have you copied the script? Put it into a file, then change the variables with the paths to putty (or do you use a different ssh binary? I am confused.)
    – songei2f
    Mar 28, 2011 at 18:11
0

This isn't exactly what you're asking for, but gnu screen is incredibly useful for managing multiple servers: http://www.gnu.org/software/screen/

To start or reattach to a screen session when you ssh somewhere you can do this:

ssh user@host screen -RD

Or run "screen -RD" after you connect. Do whatever you need to do, then instead of logging out you "detach" (Ctr-a d) from the screen session, or just close the terminal window. Next time you ssh in with screen, you'll be right back where you left off.

Some more info on screen, which admittedly can be a little confusing to get started with:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .