Automating SSH to windows with Ruby

后端 未结 1 437
傲寒
傲寒 2021-02-15 18:15

I have a 13 windows servers running Jenkins Slaves. For some reason (windows updates?), the Jenkins slaves periodically quit working and the Jenkins Slave service needs to be r

1条回答
  •  北荒
    北荒 (楼主)
    2021-02-15 18:55

    I tried it out in Pry and found two issues:

    1. HOST is undefined, it should be host as this is the variable being passed into the block.
    2. SSH.start expects the parameters to be STRING class, so add the .to_s as indicated below.

    Also, I switched it to the idiomatic Ruby pattern of using do...end when a block extends past 1 line.

    hosts.each do |host|
        puts "SSHing #{host} ..." 
        Net::SSH.start( host.to_s, USER, :password => PASS ) do |ssh|
            puts ssh.exec!('date')
            puts "Logging out..."
        end
    end
    

    I tested this in Pry and it's now working. I hope this helps.

    0 讨论(0)
提交回复
热议问题