gossh怎么连接远程服务器并执行命令
本篇内容介绍了“go ssh怎么连接远程服务器并执行命令”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
南充网站建设公司创新互联建站,南充网站设计制作,有大型网站制作公司丰富经验。已为南充数千家提供企业网站建设服务。企业网站搭建\外贸营销网站建设要多少钱,请找那个售后服务好的南充做网站的公司定做!
golang 用公钥key登陆远程服务器
下面的代码展现了公钥在可执行文件同目录或者直接以字符串形式定义在文件中两种形式
log.Fatal触发后会引发程序退出 打印输出内容 退出应用程序 defer函数不会执行
package main import ( "fmt" "log" "os" "time" "github.com/mitchellh/go-homedir" "golang.org/x/crypto/ssh" ) var serverKey string = `-----BEGIN RSA PRIVATE KEY----- LIIEowIBAAKCAQEAqq+P9RB3l6qFTNKwh+cgR3jbBQWt1oFZ+4BoutKxEtHM/bOK ADiSoHVr4+7L8Q+I9GgVyW1GR/H8UzhW5TAUvNwIklyX7zmqdZ8MXoTsZhenmolQ z9gMhXC3TLKXtnu6666666666666666666lcZk6DHdZXE+BBRYDkVbaaMpq+5 xEmbwJQZqX1L9YLM6666666666666666666JHJQ+g3t5qCZ85eVFpt3gEt/IiY2X PVvPBUaS5xn1N8KDlp121+vBVzuqYBa7TOqK6xz9V2/86yj4kD+mldgXrQBAvn3g C6cMiLqt9XPMjZF+3ML6666666666666666666wIDAQABAoIBAQCJj2iD5zubHjDE U2E2hk17jKlniMYrhKBSk56666666666666666666bUQAtSnkI9MKtwAoEEt+HEZx g4x0taVg+CxoWPuSL666666666666666666666rgDsbVrcFRMCNVkuR/JQXnZB E4b0+5eFL4FLe16FI6lVC0GqPRdKAIkXloMpVsJ/ptNMx8QI8C6zSy68NRzV7oJJ VsBbr4W1Ptez28zBzmfj66666666666666666669jWKM11EzvjpWHPNnAi LWQX9NQcSmMGTmw46666666666666666666sCgt8Vva474lVgEWyVsB6FBUOOz KzF13CuhAoGBANyuLwalH6666666666666666666wjGveIcrwM/opza+g3YprO+uoKIj 5O+lVbPOtXQGKxE/gagPMWHIdqbjZYWQ+Sq17GjJeU6kP9Rzwg04G5xi3FNG6/2f 1g7tyMiyrZdcdUIcnx5+6666666666666666666eqPuwDJotBasjkAcyPfAoGBAMYA +hb/Dyrj3PSOBiznqAc66666666666666666660WgtVkc+ZOdDR7PL2VZU sLltu3GjXm6am5aeu66666666666666666664i0Uj8cQI19bLSDqiSLXus5zn X7/QP6777777777777777LFahJd8WqVdTnSb7nZAoGAcXymNG2fKH4Amo5SXWty zvrTy0do3WKz0nfb3jxV6666666666666666666+8vcRA97txixFY9I7YRNxkWK zdsmy7Co0LeJF9G7/p5XHLJ+idkULacQJzdDpWq1+PwUVT5w2fbIGUrQmjBwzuPv niZP6P+ndL09HJc3xja6666666666666666666D3ARBgr4JoU5JqbNxHtfZaEV t/0A9b+aw0aXNmU4t70ARHtDDHwFAZBqsT+UNGjgN2XIuabXlT7JBgzkt/Gh6ff6 EzGx1C86Z80+sEU/wJ+nrjNfwdicRmJwMonPbdzd7vhzUymeDsPSBKVldH8rjwQ3 uHMwmQKBgAYAD+6666666666666666666Zy+eopHoOk5I//U8Rj3YbvX/Nu/748 oNwAaRaaSOjTJAdqfzAQHmJGVAJVjK7zkWyFPnbVPRT46cXV4j7LvbJivQ55eXdf 8VhofmV+sItXTmnB66666666666666666667t1nBoEtStJ6/ -----END RSA PRIVATE KEY-----` // ssh配置类型 type sshConfig struct { sshHost string sshUser string sshPasswrod string sshType string // password或者key sshKeyPath string // ssh id_rsa.id路径 sshPort int } // 公钥在代码中是string func publicKeyStrAuth(key string) ssh.AuthMethod { keyStr, err := homedir.Expand(key) if err != nil { log.Fatal("find key's home dir failed", err) } var keyByte []byte = []byte(keyStr) signer, err := ssh.ParsePrivateKey(keyByte) if err != nil { log.Fatal("ssh key signer failed", err) } return ssh.PublicKeys(signer) } // 公钥在文件中 func publicKeyFileAuth(keyPath string) ssh.AuthMethod { keyExpandPath, err := homedir.Expand(keyPath) if err != nil { log.Fatal("find key's home dir failed", err) } key, err := os.ReadFile(keyExpandPath) if err != nil { log.Fatal("ssh key file read failed", err) } signer, err := ssh.ParsePrivateKey(key) if err != nil { log.Fatal("ssh key signer failed", err) } return ssh.PublicKeys(signer) } func sshRemoteExcute(sshCfg sshConfig, cmd string) { // 创建ssh登录配置 config := &ssh.ClientConfig{ Timeout: time.Second, // ssh连接time out时间一秒钟,如果ssh验证错误会在一秒钟返回 User: sshCfg.sshUser, HostKeyCallback: ssh.InsecureIgnoreHostKey(), // 这个可以,但是不够安全 } if sshCfg.sshType == "password" { config.Auth = []ssh.AuthMethod{ssh.Password(sshCfg.sshPasswrod)} } else { // config.Auth = []ssh.AuthMethod{publicKeyFileAuth(sshCfg.sshKeyPath)} config.Auth = []ssh.AuthMethod{publicKeyStrAuth(sshCfg.sshKeyPath)} // return } // dial 获取ssh client addr := fmt.Sprintf("%s:%d", sshCfg.sshHost, sshCfg.sshPort) sshClient, err := ssh.Dial("tcp", addr, config) if err != nil { log.Fatal("genarate ssh client fail", err) } defer sshClient.Close() // 创建ssh-session session, err := sshClient.NewSession() if err != nil { log.Fatal("genarate ssh client fail", err) } defer session.Close() // 执行远程命令 combo, err := session.CombinedOutput(cmd) if err != nil { log.Fatal("remote excute cmd fail", err) } log.Println("cmd output:", string(combo)) log.Println("@@@Excute remote cmd successfully ~~") } func main() { var svnConfig sshConfig svnConfig.sshHost = "192.168.xx.xxx" svnConfig.sshUser = "root" svnConfig.sshPasswrod = "" svnConfig.sshType = "key" // password或者key svnConfig.sshKeyPath = "./key在同目录下" // ssh id_rsa.id路径 svnConfig.sshKeyPath = serverKey svnConfig.sshPort = 22 var newSvnName string fmt.Println("请输入新用户用户名:") fmt.Scanln(&newSvnName) testCmd := "whoami" sshRemoteExcute(svnConfig, testCmd) }
python 公钥密码访问服务器代码留存
import paramiko def ssh_private_key(rsa_file, hostname, port, username, cmd): '''私钥ssh连接cetnos方法 ''' ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) private_key = paramiko.RSAKey.from_private_key_file( rsa_file) # 指定私钥所在文件 # 连接服务器 ssh.connect( hostname=hostname, port=port, username=username, pkey=private_key, timeout=5 ) stdin, stdout, stderr = ssh.exec_command(cmd) result = stdout.read().decode('utf-8') ssh.close() return result def ssh_pwd(hostname, port, username, pwd, cmd): '''账号密码连接centos方法 ''' ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) ssh.connect( hostname=hostname, port=port, username=username, password=pwd, timeout=10 ) stdin, stdout, stderr = ssh.exec_command(cmd, get_pty=True) result = stdout.read().decode('utf-8') ssh.close() return result def ssh_transfer_file(local_file, remote_file): '''上传文件到服务器 ''' transport = paramiko.Transport('192.168.xxx.xxx', 22) transport.connect(username='root', password='1234') sftp = paramiko.SFTPClient.from_transport(transport) sftp.put(local_file, remote_file) # 从本地上传文件到远程主机 transport.close() if __name__ == "__main__": # SVN创建用户 res = ssh_pwd(hostname='192.168.xxx.xx', port=22, username='root', pwd='', cmd='/home/svn/useradd.sh xxx') print(res)
“go ssh怎么连接远程服务器并执行命令”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!
当前标题:gossh怎么连接远程服务器并执行命令
转载来于:http://azwzsj.com/article/jicggd.html