一、Pycharm SSH Session
在tool中点击Start SSH Session
二、Pycharm Remote Host
在其中的Type选中SFTP。
在SSH configuration配置齐全。
修改Web server URL
然后可以测试一下连接
最后点击OK
最后可以在侧栏的Remote Host中查看到连接的虚拟机文件信息
三、代码连接
import paramiko
# 连接并执行命令的函数
def connetAndExec(hostname,username,password,order):
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# 连接虚拟机
ssh.connect(hostname,22,username,password)
# 按行执行命令
for line in order:
ssh.exec_command(line)
# 连接关闭
ssh.close()
def myCommand():
# 开启你想执行的命令行文件
file = open("command.txt", "r")
# 读取命令
order = file.read()
# connetAndExec("192.168.10.130", "root", "hjfhjf0501", order)
connetAndExec("你的地址", "你的登录名", "你的密码", order)
if __name__ == '__main__':
myCommand()
command.txt的文件格式如下(示例,应需要改变):
sync;
/etc/init.d/postgresql stop;
echo 1 > /proc/sys/vm/drop_caches;
/etc/init.d/postgresql start;